开发者

How to execute unmanaged assembly in AppDomain?

开发者 https://www.devze.com 2023-01-10 00:51 出处:网络
I got the following error when executing an unmanaged assembly: Could not load file or assembly \'file://开发者_开发知识库/C:\\Program

I got the following error when executing an unmanaged assembly:

Could not load file or assembly 'file://开发者_开发知识库/C:\Program Files\Maxima-5.21.1\lib\maxima\5.21.1\binary-gcl\maxima.exe' or one of its dependencies. The module was expected to contain an assembly manifest.

What should I do? Is it impossible to execute an unmanaged assembly using AppDomain?


  1. AppDomains are pure managed construct. Any unmanaged code running in the process is unaffected by the AppDomain boundaries and has full access to all process memory, data and code.

  2. Unmanaged assemblies are not executed the same way managed assemblies are. The process of loading the assembly, and finding and executing the entry point for the unmanaged assembly is different than the one for managed assemblies. Hence the particular failure you get.

  3. If you want to execute functions exported by an unmanaged dll, you should use P/Invoke, which will ensure that the assembly is loaded using the right mechanism and the proper entry point is invoked.

  4. You can't run code from an executable in the same process, as in your scenario above; you can only start a new process.


You're correct, an un-managed DLL cannot be loaded into an AppDomain. You need to use P/Invoke to call methods in the DLL.


executing an unmanaged assembly:

First - this does not exist. An "Assembly" (which has the physical form aof a .DLL file) is ALWAYS managed. An unmanaged DLL is simply not an assembly PER DEFINITION. DLL's were around WAY before .NET and the term Assembly is SPECIFIC for a managed .NET DLL in this context.

Is it impossible to execute an unmanaged assembly using AppDomain?

Exactly. Which part of the vast documentation indicated otherwise?

You can use:

  • P/Invoke to make calls out into a DLL. This sometimes is REALLY hard as the .NET managed API is not so elegant for certain unmanaged structure constructs.
  • Use C++/CLI to create an assembly that references the DLL. Managed C++ is great for bridging the gap.
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号