//在一个.h文件,结构体定义:(dll和调用工程各有一个.h文件)
typedef struct DAtA_Info
{
char buff[3];
CString CarNo;
}Data_Info;
extern Data_Info p;//dll中的输出函数,返回值是结构体指针
Data_Info* _stdcall SG_CToS(LPBYTE pdata,int a)
{
Data_Info* p;
p->CarNo="13948515595";
strcpy(p->buff,"123");
return p;
}//调用dll得过程:
Data_Info* p1=new Data_Info;
HINSTANCE hinst;
hinst=LoadLibrary("Protocol.dll");
typedef Data_Info* ( _stdcall *firstlast)(LPBYTE lpdata,int a);
firstlast mess=(firstlast)GetProcAddress(hinst,"SG_CToS");
if(!mess)
{
AfxMessageBox("addresserror",MB_OK,0);
return;
}
p1=mess(lpData,2);
strData=p1->buff;以上代码有内存错误!救命啊!
我听说在调用dll时要给结构体动态分配内存,是这样吗?怎么分配,请大家帮忙!
typedef struct DAtA_Info
{
char buff[3];
CString CarNo;
}Data_Info;
extern Data_Info p;//dll中的输出函数,返回值是结构体指针
Data_Info* _stdcall SG_CToS(LPBYTE pdata,int a)
{
Data_Info* p;
p->CarNo="13948515595";
strcpy(p->buff,"123");
return p;
}//调用dll得过程:
Data_Info* p1=new Data_Info;
HINSTANCE hinst;
hinst=LoadLibrary("Protocol.dll");
typedef Data_Info* ( _stdcall *firstlast)(LPBYTE lpdata,int a);
firstlast mess=(firstlast)GetProcAddress(hinst,"SG_CToS");
if(!mess)
{
AfxMessageBox("addresserror",MB_OK,0);
return;
}
p1=mess(lpData,2);
strData=p1->buff;以上代码有内存错误!救命啊!
我听说在调用dll时要给结构体动态分配内存,是这样吗?怎么分配,请大家帮忙!
{
Data_Info* p;
p->CarNo="13948515595";
strcpy(p->buff,"123");
return p;
}
返回局部变量的地址,不行~
{
p->CarNo="13948515595";
strcpy(p->buff,"123");
}然后调用的时候Data_Info pppp;…………mess(lpData,2,&pppp);这样不行???
{
p.CarNo="13948515595";
strcpy(p.buff,"123");
}