Hello,
I have a .NET application that calls functions in a native DLL. In
order to ease deployment, and for simplicity's sake, it occurred to me
that the best way to distribute this DLL would be as a resource.
So I've added the DLL to the project as an embedded resource, and I've
confirmed that I can, at runtime load that dll into a buffer, a la:
byte[] buf = null;
using(Stream bar =
Assembly.GetCallingAssembly().GetManifestResourceStream(theName))
{
buf = new byte[bar.Length];
bar.Read(buf, 0, (int)bar.Length);
}
And then I tried loading that dll using both
AppDomain.CurrentDomain.Load(buf);
and
Module baz = Assembly.GetCallingAssembly().LoadModule(theName, buf);
Both of these throw an exception. AppDomain.Load throws
BadImageFormatException (which seems understandable since this DLL
doesn't have a CLR header and isnt a .NET assembly) and
Assembly.LoadModule complains about the filename. Also, I'll admit
that I dont really understand what Assembly.LoadModule is supposed to
do, but it also seems, from MSDN, that this too is supposed to act on
CLR modules.
I've found, on google, references to people who acheived this goal by
storing the DLL as a resource and writing it out to a local file at run
time and then accessing it normally. This seems inelegant to me. Is
this really the only solution?
Thanks
Ian
Willy Denoyette [MVP] - 19 Nov 2005 20:42 GMT
No, you can't do this. Unmanaged libraries (DLL's), have to be loaded and
mapped in the process VAS by the OS loader, which also perform the necessary
relocation. Even if it was possible to load/map the exact image (which you
don't, you are loading a file), it wouldn't be impossible to call into it
because the caller has no way to know where the code would reside.
Willy.
> Hello,
>
[quoted text clipped - 36 lines]
> Thanks
> Ian
Patrick - 20 Nov 2005 17:30 GMT
Hi Ian,
I was thinking the same thing about my project. I'm doing some video stuff
with a native dll and was thinking about embeding that dll in my class
library that uses it.
My first and last thought was to "writing it out to a local file" when
needed because I did not want to keep up with two seperate dll's. Anyway, I
think this is the only solution. Unless [DllImport] can read from a resource
stream, which I seriously doubt it.
Patrick
> Hello,
>
[quoted text clipped - 36 lines]
> Thanks
> Ian