从 CVI (Win32) 中生成的DLL文件中导出能应用于Borland C++ 或 Visual C++的变量



主要软件: LabWindows/CVI Development Systems>>LabWindows/CVI Full Development System
主要软件版本: 7.0
主要软件修正版本: N/A
次要软件: N/A

问题: 编译器处理从DLL文件导出变量的方式是不同的。如果使用下面的代码在CVI中生成DLL文件,(使用 qualifiers 的方法而不是 #include 文件的方法),怎样在Borland C++ 或 Visual C++的程序中使用这些变量呢?


global.c的内容:

#include <cvirte.h> /* Needed if linking in external compiler; harmless otherwise */
int __declspec(dllexport) x; // export the variable x.

int __stdcall DllMain (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason) {
case DLL_PROCESS_ATTACH:
if (InitCVIRTE (hinstDLL, 0, 0) == 0) /* Needed if linking in external compiler; harmless otherwise */
return 0; /* out of memory */
break;
case DLL_PROCESS_DETACH:
CloseCVIRTE (); /* Needed if linking in external compiler; harmless otherwise */
break;
}

return 1;
}

int __stdcall DllEntryPoint (HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
/* Included for compatibility with Borland */

return DllMain (hinstDLL, fdwReason, lpvReserved);
}


解答: 在Visual C++中,包含CVI所生成的重要库文件,并且在文本文件中键入下列内容:

#include <stdio.h>
extern "C" int __declspec(dllimport) x; // lets Visual C++ know that x will be imported from a DLL.

void main()
{
x = 5;

printf("x is %d\n", x);
}

In Borland C++, include the import library created by CVI and type the following in a text file:

#include <stdio.h>
extern int __declspec(dllimport) x; // lets Borland C++ know that x will be imported from a DLL.

void main()
{
x = 5;

printf("x is %d\n", x);
}


如果在Microsoft Visual C++中,在链接您的程序时仍存在问题,您可以按照下列步骤进行检查: