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. :)
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);
}
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);
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
Umm seems you can use PInvoke once you loaded the DLL...
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.
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
How would I use a char[128]*?
It depends. Is it a char[128]*, char[128], or char*? There's a big difference.