Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / November 2005

Tip: Looking for answers? Try searching our database.

Native DLL as a resource? Possible?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian - 20 Nov 2005 15:11 GMT
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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.