Posts tagged #pragma comment

How to share data between different instance of same DLL?

3


Usually Dlls are loaded into its corresponding process address space. Indeed the code is shared between all instances but not the global data. So the issue that we might face is – how can we share common data between all dll instances?


You’ve to declare your own shared data segment in your dll. You can use #pragma data_seg() to declare your own data segment. See the sample code snippet below.

#pragma data_seg (".MYSEG")
    int g_nInteger = 0;
    char g_szString[] = "hello world";
#pragma data_seg()

// Tell the linker that the section
// .MYSEG is readable, writable and is shared.
#pragma comment(linker, "/SECTION:.MYSEG,RWS")

Here we are declaring our data segment and are telling that, this segment is RWS – readable, writable and shared. This way we manage to share this data segment between all dll instances.


Targeted Audience – Intermediate.

Automatically link required libraries.

0


In our projects we use number of 3ed party API’s especially from platform SDK and other components and one burden is finding the libraries for those API’s. For Microsoft provided API’s since they have strong documentation, finding the library for particular API is not so tedious. But for other vendors, It may be. We need to dig a lot for finding the lib of that particular API. How can we get rid from this?


You can you #pragma comment( lib, “LibraryName” ) to specify the necessary library file in the header file itself. For e.g. See the following header file.

// EventLog.h
class EventLog
{
    …
};
#ifdef _DEBUG
    // If it’s a debug version…
    #pragma comment( lib, "MyEventLogLibd.lib" )
#else
    // Ooooh!!! Its release version.
    #pragma comment( lib, "MyEventLogLib.lib" )
#endif

If we include the EventLog.h header file, the library required will be automatically included. I dream for the day on which all SDK headers auto includes their libraries… ;)


Target audiance – Intermediate.

Go to Top