Load unmanaged DLL in runtime

R

Ralfeus

Hi, all.
I need to use a function from unmanaged DLL. This DLL will be at the
same place as executable. But the problem in PDA that specifying just
file name without path means the file in the root but not in the
directory of executed file. So how can I specify the full path to DLL?
Of course I don't know this path at design time :)

Thanks
Mykhaylo
 
R

Ralfeus

Well, now the library was loaded just using file name. It was at
executable's directory. Miracle but it's fact :)
Ok, but the question remains. How can I specify the path to DLL at
runtime?
 
R

Ralfeus

I tried such thing:
static string dllPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
+ "\\LiteZipPPC.dll";

[DllImport(dllPath)]
private extern static int ZipCreateFileW(ref IntPtr hz, string
zipFile, string password);

and got a compiler error:
"An attribute argument must be a constant expression, typeof
expression or array creation expression"

Did you mean this?
 
G

Guest

Ah, so you want to dynamically set up the P/Invoke. Unfortunately you can't
and there's no provision to allow it (you can't P/Invoke LoadLibrary and
GetProcAddress as the IntPtr you get back cannot be converted to a Function
Ptr in the CF (I've already brought this to the attention of the CF team,
but hopefully another request will help spur them on).

The DLL needs to be in the app path or the system path (\Windows or other if
defined). You could "work around" this by adding their target location to
the system path, though I'm not sure if that requires a restart - never
tried it (just thought of it now in fact).


--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded World
www.OpenNETCF.com


Ralfeus said:
I tried such thing:
static string dllPath =
Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
+ "\\LiteZipPPC.dll";

[DllImport(dllPath)]
private extern static int ZipCreateFileW(ref IntPtr hz, string
zipFile, string password);

and got a compiler error:
"An attribute argument must be a constant expression, typeof
expression or array creation expression"

Did you mean this?

Use reflection to get the ExecutingAssembly's path.

--

Chris Tacke, Embedded MVP
OpenNETCF Consulting
Managed Code in an Embedded Worldwww.OpenNETCF.com
 
E

eduardo.lallana

Well, now the library was loaded just using file name. It was at
executable's directory. Miracle but it's fact :)
Ok, but the question remains. How can I specify the path to DLL at
runtime?

Maybe this can be of some help:

/// <summary>
/// Current directory
/// </summary>
/// <returns>String with the current directory ("\Directory\")</
returns>
private string CurrentDirectory()
{
string dir2;
System.Reflection.Assembly a =
System.Reflection.Assembly.GetExecutingAssembly();
string dir = a.GetName().CodeBase;
dir2 = System.IO.Path.GetFileName(dir);
dir = dir.Substring(1, (dir.Length - 1) - dir2.Length);
return dir;
}
 
R

Ralfeus

That's a same situation. You can't use anything except constants in
DllImport...
I have found a way to to this using LoadLibrary() and GetProcAddress()
API functions. But then this way requires to create a proxy assembly
using C++. However it's impossible to create .net cf assemblies using C
++
 

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