main函数里的一个全局变量 能不能被他调用的dll文件使用?该如何处理
main函数里的一个全局变量 能不能被他调用的dll文件使用?
类似于这个问题
http://www.cplusplus.com/forum/general/105135/
我在测试文件里有一个变量testvariable然后赋值,然后我希望dll能够访问到这个值。
现在又个公共的头文件 定义了
extern int testvariable;
这个头文件被库和我的exe文件都包含了。main函数里给testvariable赋值了。但是dll里面testvariable却没有被赋值。有什么办法能做的这个吗?貌似用什么导出符号表,把testvariable导入到符号表,然后dll就可以正确访问testvariable了。但是我不知道怎么做。
求解,谢谢~
------解决方案--------------------
Using Shared Memory in a Dynamic-Link Library
This section shows how the DLL entry-point function can use a file-mapping object to set up memory that can be shared by processes that load the DLL. The shared DLL memory persists only as long as the DLL is loaded.
The example uses file mapping to map a block of named shared memory into the virtual address space of each process that loads the DLL. To do this, the entry-point function must:
Call the CreateFileMapping function to get a handle to a file-mapping object. The first process that loads the DLL creates the file-mapping object. Subsequent processes open a handle to the existing object. For more information, see Creating a File-Mapping Object.
Call the MapViewOfFile function to map a view into the virtual address space. This enables the process to access the shared memory. For more information, see Creating a File View.
// File: DLLSHMEM.C.
// The DLL entry-point function sets up shared memory using
// a named file-mapping object.
#include <windows.h>
#include <memory.h>
#define SHMEMSIZE 4096
static LPVOID lpvMem = NULL; // pointer to shared memory
BOOL DllMain(HINSTANCE hinstDLL, // DLL module handle
DWORD fdwReason, // reason called
LPVOID lpvReserved) // reserved
{
HANDLE hMapObject = NULL; // handle to file mapping
BOOL fInit, fIgnore;
switch (fdwReason)
{
// The DLL is loading due to process
// initialization or a call to LoadLibrary.
case DLL_PROCESS_ATTACH:
// Create a named file mapping object.
hMapObject = CreateFileMapping(
(HANDLE) 0xFFFFFFFF, // use paging file
NULL, // no security attributes
PAGE_READWRITE, // read/write access
0, // size: high 32-bits
SHMEMSIZE, // size: low 32-bits
"dllmemfilemap"); // name of map object
if (hMapObject == NULL)
return FALSE;
// The first process to attach initializes memory.
fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
// Get a pointer to the file-mapped shared memory.
lpvMem = MapViewOfFile(
类似于这个问题
http://www.cplusplus.com/forum/general/105135/
我在测试文件里有一个变量testvariable然后赋值,然后我希望dll能够访问到这个值。
现在又个公共的头文件 定义了
extern int testvariable;
这个头文件被库和我的exe文件都包含了。main函数里给testvariable赋值了。但是dll里面testvariable却没有被赋值。有什么办法能做的这个吗?貌似用什么导出符号表,把testvariable导入到符号表,然后dll就可以正确访问testvariable了。但是我不知道怎么做。
求解,谢谢~
------解决方案--------------------
Using Shared Memory in a Dynamic-Link Library
This section shows how the DLL entry-point function can use a file-mapping object to set up memory that can be shared by processes that load the DLL. The shared DLL memory persists only as long as the DLL is loaded.
The example uses file mapping to map a block of named shared memory into the virtual address space of each process that loads the DLL. To do this, the entry-point function must:
Call the CreateFileMapping function to get a handle to a file-mapping object. The first process that loads the DLL creates the file-mapping object. Subsequent processes open a handle to the existing object. For more information, see Creating a File-Mapping Object.
Call the MapViewOfFile function to map a view into the virtual address space. This enables the process to access the shared memory. For more information, see Creating a File View.
// File: DLLSHMEM.C.
// The DLL entry-point function sets up shared memory using
// a named file-mapping object.
#include <windows.h>
#include <memory.h>
#define SHMEMSIZE 4096
static LPVOID lpvMem = NULL; // pointer to shared memory
BOOL DllMain(HINSTANCE hinstDLL, // DLL module handle
DWORD fdwReason, // reason called
LPVOID lpvReserved) // reserved
{
HANDLE hMapObject = NULL; // handle to file mapping
BOOL fInit, fIgnore;
switch (fdwReason)
{
// The DLL is loading due to process
// initialization or a call to LoadLibrary.
case DLL_PROCESS_ATTACH:
// Create a named file mapping object.
hMapObject = CreateFileMapping(
(HANDLE) 0xFFFFFFFF, // use paging file
NULL, // no security attributes
PAGE_READWRITE, // read/write access
0, // size: high 32-bits
SHMEMSIZE, // size: low 32-bits
"dllmemfilemap"); // name of map object
if (hMapObject == NULL)
return FALSE;
// The first process to attach initializes memory.
fInit = (GetLastError() != ERROR_ALREADY_EXISTS);
// Get a pointer to the file-mapped shared memory.
lpvMem = MapViewOfFile(