【发布时间】:2015-01-14 10:32:23
【问题描述】:
我必须将我的项目链接到将用于另一个应用程序的 dll。我的项目必须从 dll 中读取结构,更改其变量的一些值并将结构返回给 dll。
这是我的 .ccp
#include "stdafx.h"
#include <iostream>
#include "dbase.h"
#pragma comment(lib, "DBase.lib")
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
int a;
cout << DBASE.IHMI[1] << "\n";
//DBASE.IHMI[1] = 22;
cin >> a;
return 0;
}
这是我的 .h:
#ifndef DBASE_H
#define DBASE_H
typedef signed char L1;
typedef short int I2;
typedef int I4;
typedef float R4;
#pragma pack(1)
typedef struct _DBASESTRUT {.......} DBASESTRUT;
#pragma pack()
#ifdef __cplusplus
extern "C"{
#endif
__declspec(dllimport) extern DBASESTRUT DBASE;
#ifdef __cplusplus
}
#endif
#endif
我已将 DBase.lib 添加到配置属性 |链接器 |输入 |附加依赖项 和 dll 目录到配置属性 | VC++ 目录 |图书馆目录
我的问题是我用另一个应用程序修改了 IHMI[1] 的值,然后当我使用这个程序读取它时,我读取了未初始化的值 (0)。
有什么建议吗? dll 和程序是否正确?
注意:dll 与项目位于不同的文件夹中。其他文件(.ccp、.h 和 .lib)位于项目文件夹中的同一文件夹中。
注意 2:我正在使用 MVS2013 - C++ Win32 控制台应用程序
非常感谢!
【问题讨论】:
-
这两个应用程序将分别从 DLL 中拥有自己的、独立的数据区域。您需要明确将数据标记为共享。
-
一切正常,但你不明白 Dll 是如何工作的。在不同进程中运行的两个 Dll 实例之间没有任何关系。您需要整理文件中的数据,或使用进程间通信。
-
Codeproject 有一个数据共享的例子,如果有帮助的话。但是,这种方法有很多陷阱,因此首选某种形式的 IPC。
-
@Roger Rowland,你是完全正确的,看起来我正在使用 c++ 程序修改一个结构,并使用应用程序在同一个 dll 中修改另一个结构。谢谢