Code::Blocks Forums

User forums => Help => Topic started by: MarshMan1101 on February 19, 2006, 10:29:43 pm

Title: GetProcAddress problem
Post by: MarshMan1101 on February 19, 2006, 10:29:43 pm
My program needs to install a windows hook (SetWindowsHookEx), so I created a DLL with the necessary code and everything compiled fine. In my main program, I used LoadLibrary to load the DLL and GetProcAddress to retrieve the function's address. LoadLibrary works fine, but GetProcAddress always fails. GetLastError returns ERROR_PROC_NOT_FOUND.

I did this type of thing many times before using Dev-C++ and never had problems. What on earth am I doing wrong?
Title: Re: GetProcAddress problem
Post by: mandrav on February 19, 2006, 10:33:34 pm
Have you exported your function?
For GCC you can export all symbols automatically by adding --export-all-symbols (duh) to the linker options.
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 19, 2006, 11:01:11 pm
I wen't to the Project's build options, clicked on the Linker tab, and added --export-all-symbols, and it still didn't work. I've also made sure I haven't misspelled the function name for GetProcAddress, so that can't be it.

The function looks like this:

DLL_EXPORT LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam) {
        // my code here.
}

and here is the code that trys to load it:

dllInst = LoadLibrary("loader.dll");
if (dllInst != NULL) {
    cbtProc = (HOOKPROC) GetProcAddress(dllInst, "CBTProc");
    if (cbtProc != NULL) {
        cbtHook = SetWindowsHookEx(WH_CBT, cbtProc, dllInst, tId);
    }
}
Title: Re: GetProcAddress problem
Post by: thomas on February 19, 2006, 11:17:02 pm
Hmm... this looks like Joe Normal's standard keylogger template number 34... should certainly work.
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 20, 2006, 12:37:36 am
Ok, so I'm not the best programmer in the world. I mainly do it for fun. Does anyone have any ideas on what's going on?
Title: Re: GetProcAddress problem
Post by: thomas on February 20, 2006, 12:55:18 am
No, no... I did not mean it that way. I meant to say that this is the "school book way" or "cook book way" of installing a hook, just like you will probably find it on MSDN -- so it should certainly work.

You don't happen to strip the library?
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 20, 2006, 01:25:15 am
It's not striped. I wen't to the build options and the only thing I checked was Optimize generated code (for size).
Title: Re: GetProcAddress problem
Post by: TDragon on February 20, 2006, 02:08:46 am
Do you know for certain "DLL_EXPORT" is defined as "__declspec(dllexport)"?
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 20, 2006, 02:18:17 am
#ifdef BUILD_DLL
    #define DLL_EXPORT __declspec(dllexport)
#else
    #define DLL_EXPORT
#endif

and yes, BUILD_DLL is defined.

I did this many times with Dev-C++ before without any issues, so I figure its some option in Code::Blocks that I'm missing, except I can't figure out what it is.
Title: Re: GetProcAddress problem
Post by: thomas on February 20, 2006, 02:38:11 am
You should post the complete build log for the library, so we can see what options are passed to the compiler/linker (set logging to "full commandline" in compiler options if you haven't done so anyway).
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 20, 2006, 02:54:33 am
Project   : DLL Sample
Compiler  : GNU GCC Compiler (called directly)
Directory : C:\Program Files\CodeBlocks\Projects\sss\dll\
--------------------------------------------------------------------------------
Switching to target: default
mingw32-gcc.exe   -Os -DBUILD_DLL -Os -DBUILD_DLL     -I"C:\Program Files\CodeBlocks\include" -c main.c -o .objs\main.o
mingw32-g++.exe -shared   -Wl,--dll    -L"C:\Program Files\CodeBlocks\lib" .objs\main.o   -o loader.dll   --export-all-symbols   
Process terminated with status 0 (0 minutes, 0 seconds)
0 errors, 0 warnings
Title: Re: GetProcAddress problem
Post by: thomas on February 20, 2006, 12:02:13 pm
Can't see anything wrong :(
Title: Re: GetProcAddress problem
Post by: mandrav on February 20, 2006, 12:08:09 pm
Quote
dllInst = LoadLibrary("loader.dll");

Maybe it's picking up a loader.dll from somewhere else? Can you try giving there the full path to your loader.dll?
Title: Re: GetProcAddress problem
Post by: kkez on February 20, 2006, 12:11:28 pm
Do you compile the file as a C file? If not, are you using "extern "c"" to avoid the c++ name mangling?
Code
extern "C"
{

DLL_EXPORT LRESULT CALLBACK CBTProc(int code, WPARAM wParam, LPARAM lParam)
{
        // my code here.
}

}

If this still does not resolve your problem, go look inside the .def file what is the real name for the exported function. In my experience, CALLBACK functions are always exported with "@16 " at the end, for example:
CBTProc exported as CBTProc@16
and so on.
Title: Re: GetProcAddress problem
Post by: MarshMan1101 on February 20, 2006, 03:34:10 pm
THANK YOU KKEZ!!!

Actually, according to the .def file, it is CBTProc@12. So I called GetProcAddress(dllInst, "CBTProc@12") and everything went perfectly! Thanks again!