本文介绍了无法将全局变量从dll导入应用程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个DLL A.dll 这是啊 #include < windows.h > #include < stdio.h > #include < stdlib.h > #include < string.h > #ifdef的__cplusplus #define DLL_EXPORT externC__ declspec(dllexport) #else #define DLL_EXPORT __declspec(dllexport) #endif DLL_EXPORT void function(); DLL_EXPORT char ** ReturnArr; 这是Ac void function() { char * str = 你好; char * str1 = 你好吗? ?; ReturnArr =( char **)malloc( sizeof ( char *)* 2 ); for (; j< 2; j ++) { ReturnArr [j] =( char *)malloc( 256 ); if (NULL == ReturnArr [j]) break ; } strcpy(ReturnArr [ 0 ], STR); strcpy(ReturnArr [ 1 ], STR1\" ); } 现在我的Application.c将使用dll #include < windows.h > #include < stdio.h > typedef int ( __ cdecl * MYPROC)(LPWSTR); _declspec( dllimport ) char ** ReturnArr; int main( void ) { HINSTANCE hinstLib; MYPROC ProcAdd; int a = 0 ; BOOL fFreeResult,fRunTimeLinkSuccess = FALSE; // 获取DLL模块的句柄。 hinstLib = LoadLibrary(TEXT( A.dll)); // 如果句柄有效,请尝试获取函数地址。 if (hinstLib!= NULL) { ProcAdd =(MYPROC)GetProcAddress(hinstLib, function); // 如果函数地址有效,请调用函数。 if (NULL!= ProcAdd) { fRunTimeLinkSuccess = TRUE; (ProcAdd)(L 发送到DLL函数的消息\ n); printf( %s,Returnarr [ 0 ]); } fFreeResult = FreeLibrary(hinstLib); } // 如果无法调用DLL函数,请使用替代方法。 if (!fRunTimeLinkSuccess) printf( 从executable\\\打印的消息); return 0 ; } ##在Visual Studio中CommonProperties->引用:我添加了A.dll它显示我编译器错误 错误1错误LNK2001:未解析的外部符号__declspec(dllimport)char ** ReturnArr(__ imp_?ReturnArr @@ 3PAPADA)和错误2致命错误LNK1120:1未解决## externals 告诉我一个方法我怎么能在我的应用程序中将ReturnArr作为全局变量打印 谢谢解决方案 您还没有给出定义您的A.cpp文件中的ReturnArr,但只是声明它。添加如下行: char * * ReturnArr = 0 ; 到您的A.cpp文件。 您已使用 externC属性在DLL中声明了 ReturnArr ,但在您的应用程序中,您刚刚声明了它作为外部rnal,所以编译器生成了一个C ++样式的装饰名称。您应该为DLL创建一个正确的头文件,详见 http://msdn.microsoft .com / zh-cn / library / ms235636(VS.90).aspx [ ^ ],然后你可以在你的应用程序中包含它,并确保在整个过程中使用正确的声明。 您需要注意声明或定义您的变量: 1. 声明可以在任何你喜欢的地方重复。它所做的只是使变量可以从不同的地方(文件/ DLL)访问。声明就像电话簿中的条目:您可以在任何地方复制条目 - 但是,条目实际上并不创建电话连接! 2 。定义可能只存在一次!这意味着它应该在* .c或* .cpp文件中,而不是在标题内(可能包含在多个文件中)。定义类似于实际的电话连接:它实际安装在一个地方。 在你的情况下,这意味着你需要定义 ReturnArr 在Ac(或同一DLL内的另一个.c文件)中,然后在Ah内声明(你已经这样做了)使其可访问。由于A.h被其他文件包含在内,因此无需在其他地方复制声明。 (但你可以) I have a DLLA.dllThis is A.h#include <windows.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#ifdef __cplusplus#define DLL_EXPORT extern "C" __declspec(dllexport)#else#define DLL_EXPORT __declspec(dllexport)#endifDLL_EXPORT void function();DLL_EXPORT char ** ReturnArr;This is A.cvoid function(){char *str = "hello";char *str1 = "how are you?";ReturnArr = (char **)malloc(sizeof(char*) * 2);for(;j<2;j++){ReturnArr[j] = (char *) malloc(256);if(NULL == ReturnArr[j])break;}strcpy(ReturnArr[0],"str");strcpy(ReturnArr[1],"str1");}Now i have Application.c that would use dll#include <windows.h>#include <stdio.h>typedef int (__cdecl *MYPROC)(LPWSTR);_declspec(dllimport) char ** ReturnArr;int main( void ){HINSTANCE hinstLib;MYPROC ProcAdd;int a = 0;BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;// Get a handle to the DLL module.hinstLib = LoadLibrary(TEXT("A.dll"));// If the handle is valid, try to get the function address.if (hinstLib != NULL){ProcAdd = (MYPROC) GetProcAddress(hinstLib, "function");// If the function address is valid, call the function.if (NULL != ProcAdd){fRunTimeLinkSuccess = TRUE;(ProcAdd) (L"Message sent to the DLL function\n");printf("%s",Returnarr[0]);}fFreeResult = FreeLibrary(hinstLib);}// If unable to call the DLL function, use an alternative.if (! fRunTimeLinkSuccess)printf("Message printed from executable\n");return 0;}##In Visual studio CommonProperties->references:i added A.dll its showing me compiler errorError1 error LNK2001: unresolved external symbol "__declspec(dllimport) char ** ReturnArr" (__imp_?ReturnArr@@3PAPADA)" and Error 2 fatal error LNK1120: 1 unresolved ##externalstell me a way how can i actually print ReturnArr as a global variable in my applicationthanks 解决方案 You have not given a definition for ReturnArr in your A.cpp file, but just declared it. Add a line like:char** ReturnArr = 0;to your A.cpp file.You have declared ReturnArr in your DLL with extern "C" attributes, but in your application you have just declared it as external, so the compiler has generated a C++ style decorated name. You should create a proper header file for the DLL as detailedin http://msdn.microsoft.com/en-us/library/ms235636(VS.90).aspx[^], and then you can include that in your application, and ensure correct declarations are used throughout.You need to be careful where to declare or define your variable:1. The declaration can be repeated everywhere you like. All it does is make the variable accessible from different places (files/DLLs). A declaration is like an entry in a telephone register: you can copy the entry wherever you like - however, an entry doesn't actually create a telephone connection!2. The definition may only exist once! This means it should be inside a *.c or *.cpp file, not inside a header (which may be included by multiple files). The definition is like the actual telephone connection: it is physically installed in one place.In your case it means you need to define ReturnArr inside A.c (or another .c file inside the same DLL), and then declare it inside A.h (you already did that) to make it accessible. Since A.h gets included by the other files, there is no need to copy the declaration elsewhere. (but you could) 这篇关于无法将全局变量从dll导入应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-11 02:54