Valhalla Legends Archive

Programming => General Programming => .NET Platform => Topic started by: Joe[x86] on February 23, 2007, 02:19 AM

Title: [C#] Loading a library and calling a function
Post by: Joe[x86] on February 23, 2007, 02:19 AM
You're going to call me a dumbass for this, I just know it, but how the fuck are you supposed to load a library (say, lockdown-IX86-08.dll) at any given time, find a function (say, CheckRevision), and execute that function?

The obvious answer is LoadLibrary and GetProcAddress, but you can't map GetProcAddress back to a callable function in C# (or can you?).

Also, how am I supposed to change a String to a char*? And an int to a LPDWORD? CheckRevision, from the DLL, expects pointers to a char array, as well as a ByReference DWORD for checksum and a ByReference char array for the version check statstring (formerly, exeinfo).

Thanks for any help. :)
Title: Re: [C#] Loading a library and calling a function
Post by: E.T. on February 23, 2007, 03:05 AM
As you say, I don't think you can map an adress to a function in C#... Would a small C++ wrapper dll do? http://www.codeproject.com/csharp/dyninvok.asp

Otherwise, maybe you could hack something with code generation, but I don't know...

Anyhow, here's how to convert str to char* (it must be in a fixed statement to tell GC not to move the string while you're working on it...

I'm not sure for the LPDWORD, but I think this should work too...

int i;
String str;
fixed (char* chars = str)
{
    blah(chars, &i);
}
Title: Re: [C#] Loading a library and calling a function
Post by: warz on February 23, 2007, 03:44 AM
Can't create function pointers in C#? Are you sure? I'm willing to bet you can. In C++, it might look similar to...


typedef int (__stdcall *lpCheckRevisionEx)(LPCSTR, LPCSTR, LPCSTR, LPCSTR, LPDWORD, LPDWORD,
   LPSTR, LPCSTR, HMODULE*);
HMODULE hCheckrevision;

hCheckrevision = LoadLibrary("CheckRevision.dll");
lpCheckRevisionEx CheckRevisionEx = (lpCheckRevisionEx)GetProcAddress(hCheckrevision, "CheckRevisionEx");

if(!CheckRevisionEx(sc, st, bt, vs, &version, &checksum, digest, ld, (HMODULE*)&hFiles))
printf("failed: %X\n", checksum);
else
printf("passed: ver=%X checksum=%X\n", version, checksum);


Title: Re: [C#] Loading a library and calling a function
Post by: Joe[x86] on February 23, 2007, 03:48 AM
Quote from: ♥ on February 23, 2007, 03:44 AM
Can't create function pointers in C#? Are you sure? I'm willing to bet you can.

It's more or less that I don't have a clue how. :P
Title: Re: [C#] Loading a library and calling a function
Post by: E.T. on February 23, 2007, 04:31 AM
Umm seems you can use PInvoke once you loaded the DLL...
Title: Re: [C#] Loading a library and calling a function
Post by: MyndFyre on February 23, 2007, 07:45 PM
Take a look at MBNCSUtil's Late-bound SFmpq API (http://www.jinxbot.net/CodeBrowse/MBNCSUtil_2_0_0_10/Data/LateBoundSfMpqApi.cs.aspx).  It LoadLibrary's, GetProcAddress's, and maps the function pointers to delegates.  GG, stop being a whiney bitch.

Quote from: Joex86] link=topic=16382.msg165581#msg165581 date=1172218788]
Also, how am I supposed to change a String to a char*? And an int to a LPDWORD? CheckRevision, from the DLL, expects pointers to a char array, as well as a ByReference DWORD for checksum and a ByReference char array for the version check statstring (formerly, exeinfo).
P/Invoke automatically maps String to char*.  If you need to pass an integer by reference, you declare it in the parameter list as "ref int."  char* isn't considered by reference, so don't add ref.
Title: Re: [C#] Loading a library and calling a function
Post by: E.T. on February 25, 2007, 03:32 AM
Oh nice, I didn't know about Marshal.GetDelegateForFunctionPointer (in my denfense, it wasn't there in v.1.1  :-\ )

Thanks for the link (I didn't know about beta version of MBNCS either... nice lib BTW, thanks for providing it), the tip and the laugh  ;D
Title: Re: [C#] Loading a library and calling a function
Post by: Joe[x86] on March 07, 2007, 09:40 PM
How would I use a char[128]*?
Title: Re: [C#] Loading a library and calling a function
Post by: MyndFyre on March 08, 2007, 01:45 AM
It depends.  Is it a char[128]*, char[128], or char*?  There's a big difference.