dll中传指针参数出错
在编写dll时,传入值参数没有错误。
而使用传入指针参数,在显式调用返回后出现内存泄漏的错误。
下面是部分源代码,望各位能给予帮助。
scan.h
#ifdef SCAN_EXPORTS
#define SCAN_API __declspec(dllexport)
#else
#define SCAN_API __declspec(dllimport)
#endif
SCAN_API int FunctionDll( char* a )
scan.cpp
SCAN_API int FunctionDll( char* a )
{
return 2;
}
scan.def
LIBRARY Write
EXPORTS
FunctionDll @1
dll调用
HINSTANCE m_lib;
typedef int (CALLBACK *FunctionDll) ( char *p );
FunctionDll aDll;
m_lib = LoadLibrary("Scan.dll");
assert(m_lib);
aDll = ( FunctionDll )GetProcAddress( m_lib, "FunctionDll");
assert(aDll);
char strExample[10];
strcpy(strExample,"Hello");
int ret = aDll( strExample);
ret = ret +1;
在debug下,从dll返回时出错 是memory leak。但如果用lib库方式调用就没有问题。
我不知道问题出在那里。而且如果不传入指针的话,一切都正常。
谢谢高手帮忙!