• Welcome to Valhalla Legends Archive.
 

[C#] Loading a library and calling a function

Started by Joe[x86], February 23, 2007, 02:19 AM

Previous topic - Next topic

Joe[x86]

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. :)
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

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);
}

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...


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);



Joe[x86]

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
Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

E.T.

#4
Umm seems you can use PInvoke once you loaded the DLL...

MyndFyre

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 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.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

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

Joe[x86]

Quote from: brew on April 25, 2007, 07:33 PM
that made me feel like a total idiot. this entire thing was useless.

MyndFyre

It depends.  Is it a char[128]*, char[128], or char*?  There's a big difference.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.