Chinaunix首页 | 论坛 | 博客
  • 博客访问: 573651
  • 博文数量: 493
  • 博客积分: 2891
  • 博客等级: 少校
  • 技术积分: 4960
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 17:11
文章分类

全部博文(493)

文章存档

2010年(493)

分类:

2010-05-12 19:42:35

7 试题
 某WIN32程序中采用了动态引用DLL中函数 (函数名foo),但是系统经常提示非法操作内存错误,请问下面哪种方法是正确的不会出错,不必考虑内存泄漏    (正确答案D)
(A)
 动态库链接静态C运行库 代码为
void foo(string & strOut)
{
 strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
EXE链接动态C运行库  代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo"); 
string strContent;
(*theFunc) (strContent);
cout << strContent << endl;

(B)   
动态库链接静态C运行库 代码为
void foo(string & str)
{
 str = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
 
EXE链接动态C运行库  代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo"); 
string  * pStrContent = new string;
(*theFunc) (* pStrContent);
  cout << * pStrContent <      delete (pStrContent); 
(C)
动态库链接静态C运行库 代码为
void foo(string & strOut)
{
 strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
并且DLL提供释放内存函数
void deleteFoo(string *pStrContent)
{
   if(pStrContent)
{
      delete pStrContent;
    }
}
EXE链接动态C运行库  代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo"); 
DEFFUNC delFunc = GetProcAddress(h," deleteFoo");
string  * pStrContent = new string;
(*theFunc)(* pStrContent);
 cout << * pStrContent <(*delFunc)( pStrContent);
 
(D) 动态库链接动态C运行库
 void foo(string & strOut)
{
    strOut = "xxxxxxxxxxxxxxxxxxxxxxxx";
}
EXE链接动态C运行库  代码为
HMODULE h = LoadLibrary("xxx.dll");
LIBFUNC theFunc = GetProcAddress(h," foo"); 
string strContent;
(*theFunc) (strContent);
cout << strContent << endl;
阅读(380) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~