How to Call LoadLibrary/GetProcAddress?

S

Siegfried Heintze

I have some C functions I need to expose as XML web services. My original
plan was to deply an XML Web service in C# and use P/Invoke to call my C
functions. This is not working because the web service cannot find the DLL
in the same directory.

This is probably because the DLL needs to be in a different directory,
perhaps the same directory as ASP.NET or \WinNT\System32. Unfortunately,
placing my DLL in these directories is not an option because I'm using a
hosting service.

Since I'm not getting any response from my queries in the WebServices forum
I guess I need to take another approach.

I know how to explicitly specify the full path of a DLL when calling
LoadLibrary/GetProcAddress when using C++. Is it possible to call my C
functions in their DLL using C# with LoadLibrary/GetProcAddress? What does
GetProcAddress return, a delegate? How does C# deal with function pointers?

Siegfried
 
S

Shakir Hussain

Yes, its possible to call those functions from c#

you have to use DllImport for that.

[DllImport("kernel32")]
public extern static int LoadLibrary(string librayName);

[DllImport("kernel32", CharSet=CharSet.Ansi)]
public extern static int GetProcAddress(int hwnd, string procedureName);

The usage example is

//load the library user32
int hwnd=LoadLibrary("User32");

//get the proc address of messageboxW
int addr=GetProcAddress(hwnd, "MessageBoxW");


Shak.
 
W

Willy Denoyette [MVP]

If you don't mind hardcode paths, just specify the full path of your DLL in
the DllImport, eg.
DllImport["d:\\somedir\\some.dll"]
Another option is to include the dll's path in the PATH environment.

Willy.
 

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