• Welcome to Valhalla Legends Archive.
 

Code Explanation -- DLL Explicit Linking

Started by NicoQwertyu, June 20, 2005, 11:29 PM

Previous topic - Next topic

NicoQwertyu

I know HOW to call my DLL functions explicitly, but I don't understand WHY I have to do it the way I do.  If anyone can explain these lines for me, I'd appreciate it.



typedef int (*dllhexdump)(char *s_out, int out_size, char *s_in);

...

dllhexdump hexdump = (dllhexdump)GetProcAddress(hdll, "hexdump");


Basicly the typedef is what's throwing me off so much.  I understand that it's just something I have to do, but I don't understand exactly what it is I'm doing.

K

look at the return type to GetProcAddress -- FARPROC.  I'm not sure, but I believe a FARPROC is typedef'ed as a function that takes no arguments and returns an int. (or maybe has no return value).

What you have done is created a typedef for a function: a function that returns an int and takes the three arguments listed above.

When you call GetProcAddress, it returns the address in memory of whatever procedure you looked up, but it returns a FARPROC.  The compiler has no built in conversion from a function pointer taking no arguments and returning an int to a function returning an int and taking three arguments, so you have to explicitly cast it to the type it actually is.

The typedef is really for clarity. You could rewrite it as:


int (*hexdump)(char*, int, char*) = (int (*)(char*, int, char*))GetProcAddress(/* ... */);

which creates a variable of type int(*)(char*, int, char*) named hexdump, just like your code.

except it's a lot prettier with the typedef.

Adron

I tend to declare my function pointers like this:


typedef int hexdumpfunction(char *s_out, int out_size, char *s_in);

...

hexdumpfunction *hexdump = (hexdumpfunction*)GetProcAddress(hdll, "hexdump");


That way I avoid the (*variablename) syntax that feels a bit weird. It's clearer then that I declare what a hexdumpfunction looks like, and then create a pointer to a hexdumpfunction.