简体   繁体   中英

native dll calls a .NET dll

I am trying to figure out how this can work:

  • I have a .NET dll, say NETDll.dll, that I have not source code to modify.
  • I want to use that NETDll.dll in my C++ project, which is going to create a native dll, say NativeDll.dll

So here is what I think where the problem is (I could be wrong tho):

  • Since my NativeDll.dll is native, so it cannot be a CLR/CLI dll. Native dll does not have runtime engine, but CLR does.

How should I do this? Thanks for any suggestions.

您必须在.NET程序集中公开某些内容作为COM。

Your native DLL can be a mixed-mode native-and-.NET assembly, using C++/CLI. Google or search here on "#pragma managed"/"#pragma unmanaged", or look at any of the books on C++/CLI. Basically, assuming you have Visual C++ 2010 or 2012, you can write something like this:

#pragma unmanaged

int main()
{
  CallManagedTrampoline();
}

#pragma managed

void CallManagedTrampoline()
{
  TypeFromDotnetDLL t = new TypeFromDotnetDLL();
  t.CallSomething();
}

In fact, depending on the details of what's in the .NET DLL, you may not even need to put the #pragma managed in front of the CallManagedTrampoline() call--you can sometimes call directly from unmanaged code. C++/CLI is your friend here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM