Reputation: 4151
No matter how weird it sounds, I need it O_o Well, the another solution, if it possible to load native DLL as byte array into RAM, but call function from there without DllImport >_<
Kinda this way:
byte[] dll_data = File.RealAllBytes("native.dll"); //this is just example, in real architecture bytes comes from another source and should never be stored on HDD
//uhh... ohh... do something to call exported function named "WeirdNativeFunction"
Upvotes: 3
Views: 216
Reputation: 2967
Someone ported MemoryModule to c#: https://github.com/dretax/DynamicDllLoader
Here is an example from the project how to use it:
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
delegate int AddDelegate(int a, int b);
static void RunAdd(byte[] dllBytes)
{
DLLFromMemory dll = new DLLFromMemory(dllBytes);
AddDelegate addFunc = dll.GetDelegateFromFuncName<AddDelegate>("Add");
Console.WriteLine("Calling add(1, 2): " + addFunc(1, 2) + "\n");
dll.Close();
}
but the most recent fork including TLS-support seems to be: https://github.com/schellingb/DLLFromMemory-net
Upvotes: -1
Reputation: 564771
You'd need to call the appropriate methods to load the native DLL into the calling process. The MemoryModule project on GitHub provides a (native) API for handling this, which you could use from a C++/CLI project.
Once you had the native.dll
loaded into the process, you could use P/Invoke to call GetProcAddress to get a handle to the "WeirdNativeFunction"
, and Marshal.GetDelegateForFunctionPointer
to convert it to a managed delegate, which you could then call.
Upvotes: 2