Simon
Simon

Reputation: 25993

Can I embed a win32 DLL in a .NET assembly, and make calls into it using P/Invoke?

I'm writing a C# wrapper for a third-party native library, which we have as a DLL. I would like to be able to distribute a single DLL for the new assembly. Is it possible for me to embed the win32 DLL in my .NET DLL, and still make calls into it using P/Invoke? If so, how?

Upvotes: 6

Views: 1484

Answers (3)

Kev
Kev

Reputation: 119826

I've never done it but I know of an opensource project that does this. They embed the native SQLite3 code into the managed SQLite assembly using their own tool called mergebin.

Go and take a look at the SQLite project for .NET by PHX and grab the source and you can see how it's done.

Upvotes: 2

Oliver Friedrich
Oliver Friedrich

Reputation: 9250

Should work, if the native dll does not have any dependencies.

You can compile the dll in as embedded resource, than access the stream from inside your code, serialize it to the temporary folder and use it from there.

Too much to post example code here, but the way is not to complicated.

Upvotes: 2

Mehrdad Afshari
Mehrdad Afshari

Reputation: 422112

I don't think you can do it directly, but it's possible to extract it at runtime to some temporary location and make call to that copy.

Upvotes: 1

Related Questions