George2

Hello everyone,


I am developing a DLL and I implement a class in the DLL. I am wondering in the two situations,

1. DLL is loaded by a single process multiple times

2. DLL is loaded a couple of times by multiple process

How much times will the constructor be invoked I want to learn how objects inside a DLL is shared between different process. Is it possible to make multiple process share one single class (class is defined in DLL) object instance


thanks in advance,
George



Re: Visual C++ Language DLL Load issue

Bruno van Dooren

The constructor will be called once for each process that loads it.

A DLL gets loaded only once per process. If you call LoadLibrary twice, the only thing that will change is the refcount to the DLL.

If that same DLL gets loaded in a second process, then the constructor will execute once again, but in the address space of tha second process.

DLLs do not share global variables by default, so there will be no conflict, as long as your constructor does not use system wide resources like files or mutexes or things like that.

Btw, global constructors are not such a good idea because:

- you cannot handle any errors that might occurr. If an exception is thrown, the application will not load and you will get a useless dialog without much information

- you cannot rely on functionality that resides in another DLL, because DLL loading order is not guaranteed. global constructors can ONLY call functions that reside in Kernel32.dll, because that DLL is the ONLY DLL that is guaranteed to be loaded into the address space before your DLL gets loaded.

- if you attempt something that indirectly causes the load of a DLL through LoadLibrary, you might get a crash or a deadlock. Even if it would work on your system, it might fail on another system. I have seen this happen in the field, and problems like this are very hard to debug.

If you have gobal objects that do anything more than calling kernel32 functions, It is far better to have global pointers, and then initialize them the first time they are used.





Re: Visual C++ Language DLL Load issue

George2

Thanks Bruno,

Bruno van Dooren wrote:

- if you attempt something that indirectly causes the load of a DLL through LoadLibrary, you might get a crash or a deadlock. Even if it would work on your system, it might fail on another system. I have seen this happen in the field, and problems like this are very hard to debug.

I agree with most of your points, but not quite understand the above point. Could you give a sample about your point above please

regards,

George





Re: Visual C++ Language DLL Load issue

UnitUniverse

See 'Remarks' for LoadLibrary and FreeLibrary:

"...

It is not safe to call LoadLibrary(or FreeLibrary) from DllMain. For more information, see the Remarks section in DllMain.

..."

It includes loading DLLs directly or indirectly during DllMain is being called. If any dll is loaded when an API is called

for the first time (once the API's designers want to reduce the memory usage for any uncalled APIs), that canna happen. The point is you must never assume which DLL may/will be loaded when calling any system API, for compatible reasons.




Re: Visual C++ Language DLL Load issue

George2

Thanks UnitUniverse,

UnitUniverse wrote:

See 'Remarks' for LoadLibrary and FreeLibrary:

"...

It is not safe to call LoadLibrary(or FreeLibrary) from DllMain. For more information, see the Remarks section in DllMain.

..."

It includes loading DLLs directly or indirectly during DllMain is being called. If any dll is loaded when an API is called

for the first time (once the API's designers want to reduce the memory usage for any uncalled APIs), that canna happen. The point is you must never assume which DLL may/will be loaded when calling any system API, for compatible reasons.

I am confused why we can not invoke LoadLibrary from DllMain. From the description above, it is unclear to me (the above description only mentioned we can not invoke LoadLibrary, but not mentioned why and any possible impact). Could you show an example please about why we can not call LoadLibrary please

regards,

George





Re: Visual C++ Language DLL Load issue

Simple Samples

Can you please read That is all you need to do; just read. UnitUniverse said "see the Remarks section in DllMain".






Re: Visual C++ Language DLL Load issue

George2

Thanks Simple Samples,

Simple Samples wrote:

Can you please read That is all you need to do; just read. UnitUniverse said "see the Remarks section in DllMain".

The answer is *because this may create dependency loops in the DLL load order*

regards,

George