Valhalla Legends Forums Archive | .NET Platform | [C#] Loading a library and calling a function

AuthorMessageTime
JoeTheOdd
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. :)
February 23, 2007, 8:19 AM
E.T.
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);
}
February 23, 2007, 9:05 AM
warz
Can't create function pointers in C#? Are you sure? I'm willing to bet you can. In C++, it might look similar to...

[code]
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);
[/code]

February 23, 2007, 9:44 AM
JoeTheOdd
[quote author=♥ link=topic=16382.msg165587#msg165587 date=1172223876]
Can't create function pointers in C#? Are you sure? I'm willing to bet you can.
[/quote]

It's more or less that I don't have a clue how. :P
February 23, 2007, 9:48 AM
E.T.
Umm seems you can use PInvoke once you loaded the DLL...
February 23, 2007, 10:31 AM
Myndfyr
Take a look at MBNCSUtil's Late-bound SFmpq API.  It LoadLibrary's, GetProcAddress's, and maps the function pointers to delegates.  GG, stop being a whiney bitch.

[quote author=Joe[x86] 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).
[/quote]
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.
February 24, 2007, 1:45 AM
E.T.
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
February 25, 2007, 9:32 AM
JoeTheOdd
How would I use a char[128]*?
March 8, 2007, 3:40 AM
Myndfyr
It depends.  Is it a char[128]*, char[128], or char*?  There's a big difference.
March 8, 2007, 7:45 AM

Search