Author Topic: Problems with building dynamic link libraries, please help.  (Read 5656 times)

Offline Haziq

  • Single posting newcomer
  • *
  • Posts: 5
Problems with building dynamic link libraries, please help.
« on: October 13, 2009, 08:34:16 pm »
I wonder if anyone has noticed this problem with the dynamic link libraries that are built using gcc compiler.
Dll's are built, but the problem is that when the Dll is loaded in memory, the function DllMain never gets called. This is the main entry point which is called by the operating system, where we receive an instance handle, so it is absolutely important that this function be called.
What actually happens is that the functions defined in the dll are directly called.
I am sure this problem might have been noticed before, please help me if anybody has an answer.

zabzonk

  • Guest
Re: Problems with building dynamic link libraries, please help.
« Reply #1 on: October 13, 2009, 09:27:22 pm »
How are you determining if DLLMain is being called or not? You are very limitted in what you are allowed to do in DLLMain, so setting a break point or displaying a message box may well not work, but that doesn't mean the function is not being called.

However, this is really a GCC question, rather than a CB one - you may be better off asking this on a site such as Stack Overflow.


Offline Haziq

  • Single posting newcomer
  • *
  • Posts: 5
Re: Problems with building dynamic link libraries, please help.
« Reply #2 on: October 14, 2009, 06:06:05 am »
Well, I determined it by the use of a message box. And using a VC++ compiler, the message box gets displayed.

I actually encountered this problem while writing a mouse hook routine. You need to pass an instance handle to this routine which we receive in DllMain. After I consistently failed, I then noticed that the handle received in DllMain points to nothing i.e., a null pointer.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7592
    • My Best Post
Re: Problems with building dynamic link libraries, please help.
« Reply #3 on: October 14, 2009, 06:21:38 am »
Sounds like an testing/code problem, compiler/linker, or an operating system problem.
( Completely unrelated to Code::Blocks IDE.)

I suggest going to another site to further your research into the cause.

MinGW GCC site that supports your OS would be best.

Tim S.

« Last Edit: October 14, 2009, 06:23:14 am by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

thehtoad

  • Guest
Re: Problems with building dynamic link libraries, please help.
« Reply #4 on: November 04, 2009, 12:17:48 am »
stahta01 is right it seems to be a problem with MinGW.

I found the answer here:

Quote
If you are writing code in C++ you should note that DllMain must not be "name-mangled" like normal C++ functions are (name mangling is a process that makes it possible to have two functions with the same name in C++ as long as they have different arguments). You can prevent the mangling of the DllMain function by adding

extern "C"

Before the name DllMain in the code above. You could also put DllMain in a separate .c source code file (but then you would have to worry about any calls made from inside DllMain to C++ code).

If you do not force DllMain to be a C function it will not be found by the call in the Mingw32 start up code, and the default DllMain in libmingw32.a will get called instead. If your initialization code doesn't seem to be called, and you are programming in C++, check that your DllMain function is either in a separate C source file or is forced to be a C function with extern "C".

I added extern "C" before my DllMain function and it now gets called.
Maybe someone more experienced could tell us if there are any repercussions to doing this.