我是 C/C++ 编程的新手,虽然我查看了 Microsoft 的帮助和其他 StackOverflow 问题,但我还没有找到问题的答案。
我正在尝试从我在 Visual Studio 2008 中制作的 DLL 调用导出的函数。它是从 VBA 宏和在 LabWindows/CVI 中制作的用户界面程序调用的。它适用于 VBA 宏,但是当我尝试加载 LabWindows 中的程序时,它会崩溃。
我尝试了静态和动态调用。这是我所拥有的:
DLL 中的函数以这种方式导出,_stdcall 因此 VBA 可以使用它和 __declspec(dllexport) 来摆脱 .def 文件。
__declspec( dllexport ) double * __stdcall calculos( char * String_inputs);
输入字符串是一个非常长的字符数组。这是一种传递 +60 个字符串的方法(每个字符串用逗号“,”分隔)。然后在代码中,它们使用逗号 (",") 作为参考除以 strtok。这是必要的,因为函数中 VBA 的输入限制为 60。
当我尝试静态调用该函数(将 .lib 文件和 DLL 的标头添加到项目中)时,我收到以下错误。
error: Undefined symbol '_calculos@4' referenced in "c:\PATH\cvibuild.PROJECTNAME\Debug\Main.obj".
静态调用的代码如下:
double * array_out; //Pointer to array of double
array_out = calculos(STRING_INPUT);
我尝试用他的修饰名称调用该函数,但它不起作用。
array_out = _calculos@4(STRING_INPUT);
当我检查 DependencyWalker 函数的名称时,我得到了这个名称:
?calculos@@YGPANPAD@Z
我也尝试在函数调用和定义上使用它,但没有成功。
我做错了什么?
当我尝试动态调用该函数时,程序崩溃了。DLL、头文件和.lib 放在项目文件夹中。
typedef double * (*DLLFUNC)(char*); //DLL function prototype
HINSTANCE hinstLib; //Handle to the DLL
DLLFUNC ProcAddress; //Pointer to the function
hinstLib = LoadLibrary("General.dll");
//The the pointer to the exported function and typecast it so that we can easily call it
//DLLFUNC is typedef'ed above
ProcAddress = (DLLFUNC) GetProcAddress(hinstLib, "?calculos@@YGPANPAD@Z");
//Call the function using the function pointer
double * array_out = ProcAddress(STRING_INPUT); //It crashes here
当我在程序崩溃时调试程序时,我发现我的 char * 中的逗号 (",") 被替换为 '\0',因此只有第一个字符串会到达 DLL。这可能是原因,但我只是不知道为什么逗号(“,”)被替换。
我也试过用装饰的名字来称呼它,但没有成功。
ProcAddress = (DLLFUNC) GetProcAddress(hinstLib, "_calculos@4");//Error -> Dereference to null pointer
ProcAddress = (DLLFUNC) GetProcAddress(hinstLib, "calculos"); //Error -> Dereference to null pointer