DLL Programming Hints

This contains hints to developing DLL's. I'll continue to add them as time goes on. If you know something that may help people developing DLL's, please email me.

Last updated July 27, 1998

Shared Memory

Thanks to Jim Lyssy for his additions and improvements.

Quite often, the same DLL is loaded in different process spaces. This means special code needs to be written to share data by all instances of the DLL.

When sharing data in a DLL, it is important to know if the data is initialized or uninitialized. Uninitialized data is placed into a bss_seg. By keeping the data initialized, the data_seg pragma may then be used. To simply initialize the data, it can be placed in a structure, then initialize it by a {0}. (see example)

Create an initialized data structure in a DLL.

#pragma data_seg(".SECNAME")
/* Data declarations go here*/
static struct
    int A;
    int B;
}MySharedData = {0};
#pragma data_seg()

After this, the linker needs to be advised that this section is a shared section. This can be done a couple ways. One way is to place this option right after the #pragma data_seg() call.

//option passed to the linker to share the segment
#pragma comment(linker,"section:/.SECNAME:rws")

Another method is to define the section as sharable in the DEF file. Following is a snippet of a DEF file for a library named LibName with a shared section named .SECNAME.


If you want to have the data unitialized, use bss_seg(). This isn't often used, because the DLL tends to need a starting point for the data.

Create an uninitialized shared data structure in a DLL.

#pragma bss_seg("Share")
static struct
    int A;
    int B;
} MySharedData;
#pragma bss_seg()

//options passed to linker to share the segment
#pragma comment(linker, "section:/Share:rws");

Go to driver annotation page