如何在 BCB 的 DLL 中创建共享内存段?
原文来自: http://bdn.borland.com/article/
How to create a shared segment in a dll. - by Borland Developer Support Staff
Abstract:Gives an example of creating a shared segment in Builder4 using compiler switches and DEF file.
Question:
How do I build a dll that contains a shared data segment? Books I've been reading mention the #pragma data_seg() directive, but this doesn't seem to be supported by Builder.
Answer:
Builder 4 does not support the #pragma data_seg() directive, but there are compiler switches that will, along with the proper .def file, allow you to do this.
You will have to put the data you want shared in it's own source file and use the -zR and -zT compiler switches. These switches change the data segment name and class name for whatever source file they are used in. You will have to initialize the data you want to share, as uninitialized data is automatically moved to the default data segment. You must also create a .def file for your dll that tells the linker to give that segment the shared property.
Example:
There are four files in this example:
dll.h // foreward declarations for the dll
dllmain.cpp // the CPP file that contains the dll entrypoint
dllshared.cpp // contains the shared data
dlldef.def // module definition file for the dll
#pragma hdrstop
#include
#define __dll__
#include "dll.h"
#pragma argsused
int WINAPI DllEntryPoint(HINSTANCE hinst, unsigned long reason, void*)
\{
return 1;
\}
extern int SharedInteger;
int GetInteger(void)
\{
return SharedInteger;
\}
void SetInteger(int arg)
\{
SharedInteger = arg;
\}
//---- end file dllmain.cpp ----//
//---- begin file dllshared.cpp ----//
#pragma option -zRSHARESEG // sets segment name for this source file to SHARESEG
#pragma option -zTSHARECLS // sets class name for segment to SHARECLS
int SharedInteger = 0;
//---- end file dllshared.cpp ----//
//---- begin file dlldef.def ----//
LIBRARY DLLMAIN
SEGMENTS
SHARESEG CLASS "SHARECLS" SHARED
//---- end file dlldef.def ----//
--------------------------------------------------------------------------------
Add or View comments on this article
Products:
Borland C++ Builder 4.0
Platforms:
Windows NT 4.0
Article ID: 20132 Publish Date: November 24, 1999 Last Modified: May 10, 2000
也可去看 cker 翻译的: http://www.csdn.net/Develop/read_article.asp?id=7300
这个例子我已经测试通过, 但我的程序却总是报错
下面是我的部分代码:
//---- begin file dlldef.def ----//
LIBRARY DLLMAIN
SEGMENTS
SHARESEG CLASS "SHARECLS" SHARED
//---- end file dlldef.def ----//
//---- begin file dllshared.cpp ----//
#pragma option -zRSHARESEG // sets segment name for this source file to SHARESEG
#pragma option -zTSHARECLS // sets class name for segment to SHARECLS
DWORD g_dwShare = 0;
错误提示: [C++ Error] dllshare.cpp(5): E2141 Declaration syntax error
如果在第一行加入
#include <windows.h>
错误提示:
[C++ Error] dllshare.cpp(3): E2075 Incorrect pragma directive option: -zRSHARESEG
如果在第三行加入
#include <windows.h>
错误提示:
[Linker Error] Section SHARESEG defined in .def file is empty