Author Topic: GetProcAddress problem  (Read 11945 times)

MarshMan1101

  • Guest
GetProcAddress problem
« 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?

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: GetProcAddress problem
« Reply #1 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.
Be patient!
This bug will be fixed soon...

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #2 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);
    }
}

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetProcAddress problem
« Reply #3 on: February 19, 2006, 11:17:02 pm »
Hmm... this looks like Joe Normal's standard keylogger template number 34... should certainly work.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #4 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?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetProcAddress problem
« Reply #5 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?
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #6 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).

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: GetProcAddress problem
« Reply #7 on: February 20, 2006, 02:08:46 am »
Do you know for certain "DLL_EXPORT" is defined as "__declspec(dllexport)"?
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #8 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.

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetProcAddress problem
« Reply #9 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).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #10 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

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: GetProcAddress problem
« Reply #11 on: February 20, 2006, 12:02:13 pm »
Can't see anything wrong :(
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline mandrav

  • Project Leader
  • Administrator
  • Lives here!
  • *****
  • Posts: 4315
    • Code::Blocks IDE
Re: GetProcAddress problem
« Reply #12 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?
Be patient!
This bug will be fixed soon...

Offline kkez

  • Almost regular
  • **
  • Posts: 153
    • WinapiZone
Re: GetProcAddress problem
« Reply #13 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.

MarshMan1101

  • Guest
Re: GetProcAddress problem
« Reply #14 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!