Valhalla Legends Forums Archive | C/C++ Programming | Code Explanation -- DLL Explicit Linking

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


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

...

dllhexdump hexdump = (dllhexdump)GetProcAddress(hdll, "hexdump");
[/code]

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.
June 21, 2005, 4:29 AM
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:

[code]
int (*hexdump)(char*, int, char*) = (int (*)(char*, int, char*))GetProcAddress(/* ... */);
[/code]
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.
June 21, 2005, 6:51 AM
Adron
I tend to declare my function pointers like this:

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

...

hexdumpfunction *hexdump = (hexdumpfunction*)GetProcAddress(hdll, "hexdump");
[/code]

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.
June 21, 2005, 4:24 PM
NicoQwertyu
Thanks you two.  :)
June 21, 2005, 5:03 PM

Search