该方法可以在dll或者exe中共享变量。注意数据的同步。
// Tell the compiler to put this initialized variable in its own Shared
// section so it is shared by all instances of this application.
#pragma data_seg("Shared")
volatile LONG g_lApplicationInstances = 0;
#pragma data_seg()
当编译器对这个代码进行编译时,它创建一个新节,称为S h a r e d,并将它在编译指示后面
看到的所有已经初始化(i n i t i a l i z e d)的数据变量放入这个新节中。在上面这个例子中,变量放
入S h a r e d节中。该变量后面的#pragma dataseg()一行告诉编译器停止将已经初始化的变量放入
S h a r e d节,并且开始将它们放回到默认数据节中。需要记住的是,编译器只将已经初始化的变
量放入新节中
// Tell the linker to make the Shared section readable, writable, and shared.
#pragma comment(linker, "/Section:Shared,RWS")
虽然可以创建共享节,但是,由于两个原因, M i c r o s o f t并不鼓励你使用共享节。第一,用
这种方法共享内存有可能破坏系统的安全。第二,共享变量意味着一个应用程序中的错误可能
影响另一个应用程序的运行,因为它没有办法防止某个应用程序将数据随机写入一个数据块。