如何在visual-studio和gcc-linux中导出/导入变量?

如何在visual-studio和gcc-linux中导出/导入变量?

问题描述:


我试图在这里实现一个看似简单的事情.但是,我似乎无法正常工作.
我需要的是:从EXE访问变量,该变量位于DLL中.

Hi,
I am trying to implement a seemingly simply thing here. But, I just can''t seem to get it to work.
What I need is: Access a variable from an EXE, where the variable is located in a DLL.

//DLL:
#define __ZG_WIN32__
#ifdef __ZG_WIN32__
#define __ZG_DLL_EXPORT__ __declspec(dllexport)
#endif

#ifdef __ZG_LINUX__ //LINUX
#define __ZG_DLL_EXPORT__ extern
#endif

__ZG_DLL_EXPORT__ int ZGKillerStarter = -1;





//EXE:
#define __ZG_WIN32__
#ifdef __ZG_WIN32__
#define __ZG_DLL_IMPORT__ __declspec(dllimport)
#endif

#ifdef __ZG_LINUX__ //LINUX
#define __ZG_DLL_IMPORT__
#endif

__ZG_DLL_IMPORT__ int ZGKillerStarter;



然后,我将dll作为附加依赖项链接到了编译器中.我什至用Dependency Walker检查了变量的名称.

当我运行该程序时,ZGKillerStarter的值是其初始值-1,即使在DLL端进行更改,它也永远不会更改! ><< br mode ="hold"/>
此外,我什至尝试使用EXPORT/IMPORT函数获取"变量的值,但是每次它都会不断返回初始值.而且,我做了同样的事情,但是使用了一个返回变量地址的函数,该函数的初始值仍然是<&lt ;;;.

Q1:我在这里想念什么?
Q2:在Linux中此代码等效吗?
Q3:我该如何做以上的反向操作! (不是同时,只是从EXE导出变量,然后导入DLL)

注意:我需要遵循某些限制,这些限制禁止我使用函数指针.

参考: http://msdn.microsoft.com/en-us/library/8fskxacy%28v = vs.80%29.aspx [



Then, I linked the dll in the compiler as an additional dependency. I even checked the variable''s name with Dependency Walker.

When I run the program, ZGKillerStarter''s value is its initial value of -1, and never changes even when I change it on the DLL side! ><<br mode="hold" />
Furthermore, I even tried to "Get" the variable''s value using an EXPORT/IMPORT function, but it kept on returning the initial value every time. Moreover, I did the same but using a function that returns the variable''s address, still initial value ><;

Q1: What am I missing here?
Q2: What is the equivalent of this code in Linux?
Q3: How do I do the inverted action of the above!? (NOT at the same time, just exporting variables from EXE, importing to DLL)

NOTE: There are certain restrictions that I need to follow, which prohibits me from using function pointers.

reference: http://msdn.microsoft.com/en-us/library/8fskxacy%28v=vs.80%29.aspx[^]


//EXE side
__ZG_DLL_IMPORT__ int ZGKillerStarter;//first try
__ZG_DLL_IMPORT__ int ZG_getZG( /*void */);//second try
__ZG_DLL_IMPORT__ int *ZG_getZG( /*void */);//third try

int XXXXX = 0; int IIII = 9;

//DLL side
__ZG_DLL_EXPORT__ int ZG_getZG( )//second try
{
	return ZGKillerStarter;
}


__ZG_DLL_EXPORT__ int *ZG_getZG( )//third try
{
	return &ZGKillerStarter;
}
void funcX()
{
if( ZGKillerStarter < 10000 ) ZGKillerStarter += 8;
}


//EXE side
__ZG_DLL_IMPORT__ int ZGKillerStarter;//first try
__ZG_DLL_IMPORT__ int ZG_getZG( /*void */);//second try
__ZG_DLL_IMPORT__ int *ZG_getZG( /*void */);//third try

int XXXXX = 0; int IIII = 9;

//DLL side
__ZG_DLL_EXPORT__ int ZG_getZG( )//second try
{
	return ZGKillerStarter;
}


__ZG_DLL_EXPORT__ int *ZG_getZG( )//third try
{
	return &ZGKillerStarter;
}
void funcX()
{
if( ZGKillerStarter < 10000 ) ZGKillerStarter += 8;
}

您是什么意思在DLL端进行更改"?您执行什么代码来获取变量然后对其进行更改?
How do you mean "change it on the DLL side"? What code do you execute to get the variable and then to change it?


让我们尝试查看事件的顺序:
Let''s try looking at the sequence of events:

  1. 清理两个项目(确保输出目录为空).
  2. 使用上述代码构建DLL.这将产生两个文件name.lib和name.dll(name是您为项目指定的名称).
  3. 在EXE项目的输入链接器属性"的其他依赖项"部分中添加"name.lib"
  4. 将name.lib文件的目录路径添加到EXE项目中常规链接器属性"的其他库目录"部分.
  5. 构建EXE项目.
  6. 将name.dll文件复制到EXE项目的目标文件中,即包含目标.exe文件的目录
  7. 运行程序并检查结果.

  1. Clean both projects (ensure output directories are empty).
  2. Build the DLL using the code as shown above. This should produce two files name.lib and name.dll (name is the name you give the project).
  3. Add "name.lib" to the Additional Dependencies section of the Input Linker Properties in the EXE project.
  4. Add the directory path of the name.lib file to the Additional Library Directories section of the General Linker Properties in the EXE project.
  5. Build the EXE project.
  6. Copy the name.dll file into the target firectory of the EXE project, i.e the directory that contains the target .exe file
  7. Run the program and check results.