Flexible DllImport

F

FrzzMan

Hello, is there anyway to import unmanaged functions in a DLL file that
locate somewhere out of the current path?

Like this, when the app start, it'll locate the dll file, once it found
the file, it'll import the file's functions.

But the problem is, I have the location of the file store in a string
variable, I can't find a way to use that string variable with DllImport?
 
G

Guest

You can use Reflection.Emit namespace functions to create the code at runtime, which specifies the DllImport attribute
taken from that string variable... Its just my idea, I don't know how complex it is..

HT
Sunil
 
R

Ryan LaNeve

FrzzMan said:
Hello, is there anyway to import unmanaged functions in a DLL file that
locate somewhere out of the current path?

Like this, when the app start, it'll locate the dll file, once it found
the file, it'll import the file's functions.

But the problem is, I have the location of the file store in a string
variable, I can't find a way to use that string variable with DllImport?

You can use a combination of "LoadLibrary" or "LoadLibraryEx" with
"FreeLibrary" as an option. Once the DLL has been loaded with either of the
"Load?" functions, you can call its methods without specifying the full path
to the file in the DllImport. Another option may be to use
"SetDllDirectory", though I haven't actually tested that one. The first I
mentioned definitely works.

Good luck,
Ryan LaNeve
 
R

Ryan LaNeve

Ryan LaNeve said:
You can use a combination of "LoadLibrary" or "LoadLibraryEx" with
"FreeLibrary" as an option. Once the DLL has been loaded with either of the
"Load?" functions, you can call its methods without specifying the full path
to the file in the DllImport. Another option may be to use
"SetDllDirectory", though I haven't actually tested that one. The first I
mentioned definitely works.

Good luck,
Ryan LaNeve
I forgot to include this link in case you need it:
http://msdn.microsoft.com/library/d...lproc/base/dynamic_link_library_functions.asp
(watch for line-wrap)

Ryan LaNeve
 
F

FrzzMan

Currently I don't know how to use Emit namespace, but I'm reading
documentation about it @ MSDN. Hope your idea will do the trick :)

Thank you.

But, let me explain my scenario, hope this will help you understand
better the problem and help me, of course :D

My app is plugins-based, when user want to use unavailable features, it
will request the server and the server will send the plugins to it.
After that it will save the DLL file somewhere (so that user can keep
all the plugins in the safe place).

The app will search for plugins when it need to use it, once found it'll
load the DLL and call the function, unfortunately most of them are
unmanaged, but fortunately the plugins are all have the same functions
list, so it won't be a problem of calling unavailable function.

The problem is, the plugins file are saved to unpredictable place, user
will specify it. So the DllImport seem to be useless :(

TIA, if you have better approach, please tell me, too :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top