C#P/调用 [英] C# P/Invoke

查看:105
本文介绍了C#P/调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以直接在C#汇编代码中调用(我必须优化涉及一些数学计算的循环,并且我认为我应该在汇编中编写它们以加快速度).
但是,我使用了google",发现的一切都是在C中调用汇编代码,然后在C#中使用P/Invoke.
有没有一种方法可以直接从C#调用程序集?

在此先谢谢您!

I was wondering if I could invoke directly in C# assembly code (i must optimize a loop which involves some math computations and I was thinking I should write them in assembly to speed it up).
However, I''ved google''it and all I could find was some sort of invoking the assembly code in C and then using P/Invoke in C# .
Is there a way that I could invoke the assembly directly from C#?

Thanks in advance !

推荐答案

用汇编语言编写一个dll并从C#调用P/来调用它是很有可能的,但是这样会使您的生活变得异常困难.

我只是用C ++破解一个dll,然后在其中编写汇编程序.
It would be quite possible to write a dll in assembly language and P/Invoke that from C# but you would be making life inordinately difficult for yourself.

I would just hack together a dll in C++ and write the assembler stuff in there.


从汇编语言源代码创建一个库并使用P/Invoke将其链接到C#项目已经足够直接;),我想您会想到的是诸如C ++或Borland Pascal/Delphi代码中的内联汇编"之类的东西.

这样,您实际上可以混合使用C#和汇编代码,但是-不,您不能,但是您应该更好地理解为什么不这样做,并且也应该知道自己真的不想要它.

您无法执行此操作,因为.NET程序集未在任何CPU的代码中进行编译.当代码已经加载时,编译由JIT完成.并且这是逐个方法逐步完成的(例如,从未调用的方法将不会被编译).这使事情变得非常轻便.我开发的很多应用程序也可以在Linux上运行,甚至可以在UI应用程序上运行.程序集中有指定的目标CPU.它可以是某种类型的Intel CPU(x86(32位体系结构),x86-64和Itanium(两种不同的64位体系结构)),但这仅是可执行映像中的简单标记";并且默认值为任何CPU"(使用自动检测到的CPU);

如果存在内联程序集",则其代码将无法获得要编译到的目标.

(好吧,我可以幻想可以创建具有内联程序集"功能的高级.NET语言的可能性很小,但是如果链接中的本机程序集使用的是PInvoke,它所能做的就是自动化创建过程.我没有认为这种工作会有任何实际的理由.)

但是,请勿将汇编语言与IL混淆.是的,任何程序集都可以反汇编"为IL,所以IL在.NET的意义上起着汇编语言的作用,因为它是低级的. IL还以.NET字节码为目标(实际上是类似于传统汇编语言的字节码的人类可读文本表示形式),而不是CPU指令代码.

您还可以优化IL代码并将其重新编译.很少有有趣而有用的工作,人们可以在幕后的构建步骤中自动反汇编程序集,调整IL代码并将其反汇编.这是因为IL具有某些其他语言无法提供的功能,并且因为有了IL,您可以获得更多的细粒度控制.

我认为这不会给您带来太多好处.我建议您尝试常规的P/Invoke.
祝你好运!
As creating a library from Assembly Language source code and using P/Invoke to link it to C# project is already direct enough ;), I think what you have in mind is something like "inline assembly" available in C++ or Borland Pascal/Delphi code.

In this way, you could actually mix C# and assembly code, but -- no, you can''t, but you should better understand why not and also understand that you really don''t want it.

You can not do such thing, because .NET assembly is not compiled in any CPU''s code. The compilation is done by JIT when the code is already loaded; and this is done gradually, on per-method basis (so, for example, method which never called will not be compiled). This makes things amazingly portable. A good deal of my applications I develop I also run on Linux, even UI applications. The assembly has target CPUs specified in it; and it can be some class of Intel CPU (x86 (32-bit architecture), x86-64 and Itanium (two different 64-bit architectures)), but this is no more than a simple "tag" in the executable image; and the default is "Any CPU" (using auto-detected CPU);

If "inline assembly" existed its code would not get a target to be compiled into.

(Well, I could fantasize remote possibility that such high-level .NET language with "inline assembly" feature can be created, but all it could do is automating process of creating if native assembly in linking is using PInvoke. I don''t think there will be any practical justification for such work.)

Don''t confuse, however, assembly language with IL. Yes, any assembly can be "disassembled" into IL, so IL plays a role of Assembly Language in the word of .NET in that sense it is low-level. IL also target .NET byte code (it actually a text human-readable representation of the byte code resembling tradition Assembly Language), it does not target CPU instruction code.

You could also optimize IL code and compile it back. There are few interesting and useful works where people automatically disassemble an assembly, tweak IL code and assemble it back during build step behind the scene. This is because IL has some features not available in other languages, and because with IL you can gain more fine-grain control.

I don''t think it can give you much. I would advice to try regular P/Invoke.
Good luck!


这篇关于C#P/调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆