I have an application that requires a DLL and an executable that uses the
DLL, both of which were implemented in Visual C++ using unmanged code. Both
the executable and the DLL are linked with functions that are stored in a
static library. During initialization of the executable, classes and static
globals within functions linked from the static library appear to be
initialized twice, once when the executable initializes the run-time code,
and once when the DLL initializes the run-time code. This dual
initialization is not always benign and I am at a loss of how to stop it.
Presumably, there must be some method of including functions from a static
library once, and once only, either in the DLL or in the referencing
executable.
Perhaps this situation has been encountered before. If so, any suggestions
would be appreciated.
--
=============== =============== =============== =============== ==========
=============== =============== =============== =============== ==========
==
== Bob Riedel
== Beckman Coulter, Incorporated
== PO Box 8000 W-529
== 200 S Kraemer Blvd
== Brea CA 92822-8000
==
== Email 1: ra******@beckma n.com
== Email 2: ra******@mindsp ring.com
==
==
== The opinions expressed are my own, and do not necessarily represent
== those of Beckman Coulter, Inc.
==
=============== =============== =============== =============== ==========
=============== =============== =============== =============== ==========
==
== "Effective education is the key to successful democracy."
==
== "Criticizin g the actions of others offers so little risk, and
== requires so little effort that it is, without exception, the tool
== of the lazy and of the foolish -- who have neither the intelligence
== to discover, nor the discipline to pursue, a realistic
== alternative."
==
=============== =============== =============== =============== ==========
=============== =============== =============== =============== ========== 8 3021
Robert A Riedel wrote: I have an application that requires a DLL and an executable that uses the DLL, both of which were implemented in Visual C++ using unmanged code. Both the executable and the DLL are linked with functions that are stored in a static library. During initialization of the executable, classes and static globals within functions linked from the static library appear to be initialized twice, once when the executable initializes the run-time code, and once when the DLL initializes the run-time code. This dual initializati on is not always benign and I am at a loss of how to stop it.
Presumably, there must be some method of including functions from a static library once, and once only, either in the DLL or in the referencing executable.
Perhaps this situation has been encountered before. If so, any suggestions would be appreciated.
I can think of two solutions. I'll start with the more general one.
As your EXE and DLL are linking statically to the library, they each get
their own, private copies of the library's code and data. For them to share
this library's code and data, you will have to turn it into a DLL, which
they both link to. A similar situation exists with the CRT. Modules which
link statically to it have independent CRT state, including heap, file
descriptors, errno, and so on, which creates module boundary problems in
which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't
be passed between modules, and so on. Linking everyone to the same CRT DLL
avoids these problems and makes your program act much more like a statically
linked program.
Alternatively, you could have your DLL link to the static library and define
a forwarding interface for use by the EXE, which would no longer link to the
static library. Of course, this could be very inconvenient.
--
Doug Harrison
Microsoft MVP - Visual C++
This is a helpful response. It is the solution that I expected, even though
I had hoped to avoid it. Thank you for the reply.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:gv******** *************** *********@4ax.c om... Robert A Riedel wrote:
I have an application that requires a DLL and an executable that uses the DLL, both of which were implemented in Visual C++ using unmanged code.
Boththe executable and the DLL are linked with functions that are stored in a static library. During initialization of the executable, classes and
staticglobals within functions linked from the static library appear to be initialized twice, once when the executable initializes the run-time
code,and once when the DLL initializes the run-time code. This dual initializati on is not always benign and I am at a loss of how to stop it.
Presumably, there must be some method of including functions from a
staticlibrary once, and once only, either in the DLL or in the referencing executable.
Perhaps this situation has been encountered before. If so, any
suggestionswould be appreciated. I can think of two solutions. I'll start with the more general one.
As your EXE and DLL are linking statically to the library, they each get their own, private copies of the library's code and data. For them to
share this library's code and data, you will have to turn it into a DLL, which they both link to. A similar situation exists with the CRT. Modules which link statically to it have independent CRT state, including heap, file descriptors, errno, and so on, which creates module boundary problems in which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't be passed between modules, and so on. Linking everyone to the same CRT DLL avoids these problems and makes your program act much more like a
statically linked program.
Alternatively, you could have your DLL link to the static library and
define a forwarding interface for use by the EXE, which would no longer link to
the static library. Of course, this could be very inconvenient.
-- Doug Harrison Microsoft MVP - Visual C++
While attempting to implement this suggestion, I am having difficulty
interpreting a semanic issue with respect to the __declespec(
dllexport/dllimport ) keyword. Suppose for example a class is declared in a
header file:
//Header file 'a.h' :
#ifndef API
#define API __declspec( dllimport )
#endif
class
API
a
{
void do_some_stuff( void ) const ;
} ;
// Implementation file:
#define API __declspec( dllexport )
#include "a.h"
namespace {
struct B { int n ; } ;
B _b ;
} // END:: anonymous namespace
a::do_some_stuf f( void ) const
{
_b.n++ ;
}
// END implementation file.
Will the static global _b declared in the anonymous namespace be properly
initialized by the CRT when the DLL startup code runs without specifying any
specific attributes?
If I understand the way a DLL should work, I would claim that static global
_b in the anonymous namespace would be instantiated in the DLL's private
segment when a process attaches.
Is this correct?
A related issue concerns the instantiation of template classes. If I
understand the way that would work, a template class cannot be given the
dllexport attribute, however, one could explicitly instantiate the class and
specify the attribute.
For example:
template <class T>
class A
{
} ;
template class __declspec( dllexport ) A<char> ; // Legal, right ???
Although I am puzzled about how exactly the import would be declared in a
header file. Perhaps:
typedef A<char> mytype ;
template class __declspec( dllimport ) A<char> ;
Or is there some other way?
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:gv******** *************** *********@4ax.c om... Robert A Riedel wrote:
I have an application that requires a DLL and an executable that uses the DLL, both of which were implemented in Visual C++ using unmanged code.
Boththe executable and the DLL are linked with functions that are stored in a static library. During initialization of the executable, classes and
staticglobals within functions linked from the static library appear to be initialized twice, once when the executable initializes the run-time
code,and once when the DLL initializes the run-time code. This dual initializati on is not always benign and I am at a loss of how to stop it.
Presumably, there must be some method of including functions from a
staticlibrary once, and once only, either in the DLL or in the referencing executable.
Perhaps this situation has been encountered before. If so, any
suggestionswould be appreciated. I can think of two solutions. I'll start with the more general one.
As your EXE and DLL are linking statically to the library, they each get their own, private copies of the library's code and data. For them to
share this library's code and data, you will have to turn it into a DLL, which they both link to. A similar situation exists with the CRT. Modules which link statically to it have independent CRT state, including heap, file descriptors, errno, and so on, which creates module boundary problems in which memory allocated by the DLL can't be freed in the EXE, FILE*'s can't be passed between modules, and so on. Linking everyone to the same CRT DLL avoids these problems and makes your program act much more like a
statically linked program.
Alternatively, you could have your DLL link to the static library and
define a forwarding interface for use by the EXE, which would no longer link to
the static library. Of course, this could be very inconvenient.
-- Doug Harrison Microsoft MVP - Visual C++
Robert A Riedel wrote: While attempting to implement this suggestion, I am having difficulty interpreting a semanic issue with respect to the __declespec( dllexport/dllimport ) keyword. Suppose for example a class is declared in a header file:
//Header file 'a.h' :
#ifndef API #define API __declspec( dllimport ) #endif
class API a { void do_some_stuff( void ) const ; } ;
// Implementation file:
#define API __declspec( dllexport )
That's not the best way to use those __declspecs; see this message for a
better way: http://groups.google.com/groups?selm...ocbo%404ax.com
#include "a.h"
namespace {
struct B { int n ; } ;
B _b ;
} // END:: anonymous namespace
a::do_some_stu ff( void ) const { _b.n++ ; }
// END implementation file.
Will the static global _b declared in the anonymous namespace be properly initialized by the CRT when the DLL startup code runs without specifying any specific attributes?
Yes.
If I understand the way a DLL should work, I would claim that static global _b in the anonymous namespace would be instantiated in the DLL's private segment when a process attaches.
Is this correct?
Yes. It will be in the DLL's data segment, and it will be initialized and
destroyed when the DLL receives the DLL_PROCESS_DET ACH and
DLL_PROCESS_DET ACH notifications. But see "DllMain" in MSDN for some
important restrictions which apply to DLL global data initialization, which
is performed from DllMain context. See this message for a problem which can
affect local static data in DLLs: http://groups.google.com/groups?selm...bcf3%404ax.com
A related issue concerns the instantiation of template classes. If I understand the way that would work, a template class cannot be given the dllexport attribute, however, one could explicitly instantiate the class and specify the attribute.
For example:
template <class T> class A { } ;
template class __declspec( dllexport ) A<char> ; // Legal, right ???
Although I am puzzled about how exactly the import would be declared in a header file. Perhaps:
typedef A<char> mytype ;
template class __declspec( dllimport ) A<char> ;
Or is there some other way?
That syntax is almost right. See these articles for more:
HOWTO: Exporting STL Components Inside & Outside of a Class http://support.microsoft.com/?kbid=168958
Explicit Instantiation http://msdn.microsoft.com/library/de...tantiation.asp
--
Doug Harrison
Microsoft MVP - Visual C++
While porting the library to a DLL, there are numerous occassions where a
C4251 compiler warning is encountered on certain class private data. Since
clients of these classes have no reason to access the private data section,
are these warnings material and do they need to be remediated?
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:9b******** *************** *********@4ax.c om... Robert A Riedel wrote:
While attempting to implement this suggestion, I am having difficulty interpreting a semanic issue with respect to the __declespec( dllexport/dllimport ) keyword. Suppose for example a class is declared in
aheader file:
..
..
.. -- Doug Harrison Microsoft MVP - Visual C++
Robert A Riedel wrote: While porting the library to a DLL, there are numerous occassions where a C4251 compiler warning is encountered on certain class private data. Since clients of these classes have no reason to access the private data section, are these warnings material and do they need to be remediated?
You should make sure that no inline function accesses the data member. This
includes default ctor, copy ctor, dtor, and assignment operator the compiler
generates for you. You should define all these members out of line. Then if
the compiler still complains about C4251, you should be able to ignore it.
See these messages for more on C4251: http://groups.google.com/groups?selm...kf11%404ax.com http://groups.google.com/groups?selm...mbdd%404ax.com http://groups.google.com/groups?selm...1ob0%404ax.com
--
Doug Harrison
Microsoft MVP - Visual C++
Thanks for the info.
The other thing that it seems possible to do is to only export the public
functions and data, instead of the entire class.
"Doug Harrison [MVP]" <ds*@mvps.org > wrote in message
news:de******** *************** *********@4ax.c om... Robert A Riedel wrote:
While porting the library to a DLL, there are numerous occassions where a C4251 compiler warning is encountered on certain class private data.
Sinceclients of these classes have no reason to access the private data
section,are these warnings material and do they need to be remediated? You should make sure that no inline function accesses the data member.
This includes default ctor, copy ctor, dtor, and assignment operator the
compiler generates for you. You should define all these members out of line. Then
if the compiler still complains about C4251, you should be able to ignore it.
See these messages for more on C4251:
http://groups.google.com/groups?selm...kf11%404ax.com http://groups.google.com/groups?selm...mbdd%404ax.com http://groups.google.com/groups?selm...1ob0%404ax.com -- Doug Harrison Microsoft MVP - Visual C++
Robert A Riedel wrote: Thanks for the info.
The other thing that it seems possible to do is to only export the public functions and data, instead of the entire class.
That can often work, but exporting/importing the whole class does something
special. See this message for more on that: http://groups.google.com/groups?selm...9d%40philiplu2
--
Doug Harrison
Microsoft MVP - Visual C++ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: mccoyn |
last post by:
I'm porting an old project to use .NET. When I try to link in a static library (.lib) that has a single managed class in it. I get the following errors:
LINK : error LNK2020: unresolved token (0600000E) PatInfo::.ctor
LINK : error LNK2020: unresolved token (0600000F) PatInfo::Finalize
LINK : fatal error LNK1120: 2 unresolved externals
I've striped everything out of this library except for the single class which has an empty constructor...
|
by: free2cric |
last post by:
Hi,
I have two c++ static libraries say A.lib and B.lib.
I am creating third static library C
I create project in microsoft visual studio as win32 static library.
I go to project-> settings -> link , i dont find object/library module
where I
can type in the names of A.lib and b.lib.. this option is available if
i create dynamic library.
Thanks
cric
|
by: mpatnam |
last post by:
I have an executable which links to a static library (.a).
I want to provide a hook by overriding a function part of this static
library.
Eg: I have a function "int blkstart(int i)" in this static library.
I want to override this by having another function which is exactly
similar in signature including name.
When somebody call this function, control should automatically come to
my overriddent function. Inside this function, based on...
|
by: newest newbie |
last post by:
I'm new to using c#, but what I'm trying to do is create a library control
containing a form which in turn contains a Windows application that I have
defined in another library.
I'm able to do it if my application which I put in the form is a c++ dll
like so:
private extern static void RunMyApp(IntPtr handle);
|
by: Manny Silva |
last post by:
Hi,
I would like to create a static library that uses and in effect adds to
another static library. I could simply add functionality to the existing
library, but functionally it doesn't really belong there... that is, the one
library is very inspecific. The Library I need to create will be project
specific, and I prefer not to have to mix the two... in any case, I tried to
create a library that referenced the first library. I added...
| |
by: Notre Poubelle |
last post by:
Hello,
I have a large legacy MFC application. As is typical, there is an
executable along with several MFC DLLs. One of these DLLs is created by
staticly linking in many libraries resulting in one very large DLL that has
the bulk of the code.
I've downloaded the MFCWinFormsSample.EXE and have noticed that the main app
could stay as a native executable (i.e. no CLR support) whereas the various
..DLLs can be marked as having CLR...
|
by: mnowosad |
last post by:
As far I know, static variables are tied to AppDomain scopes. So, every time
an executing code within an AppDomain references a class for the the first
time since the AppDomain was created/loaded, the .NET executes the
assignments done in the class static variables declarations and runs the
static constructor of that class.
So, I expected that, as in ASP.NET web site, for a given Web Service site,
the AppDomain would be initialized upon...
|
by: sealo |
last post by:
Hello,
I have a test in VS2005 that static library A.lib use the dynamic
library C.dll.
Then a application App use the A.lib.
App-->A.lib-->C.dll
It works. But if I remove the C.dll, the app tell me can not find the
C.dll.
I think because A.lib is a static library, so it should contain the
|
by: Even |
last post by:
Hi all,
As far as I know, relative address will be assigned by program at link
time.Right? Ok, here we go.
So if we use a static library, it will give the program an relative
address(i mean the function address) at link time.
Also the code of the library will load into the program. Go on.
Now if we use a dynamic library, what it will give the program?
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |