(LoadRunner)包装一个DLL的经验总结

LoadRunner里可以直接调用VC++的DLL,关于DLL基本的制作及调用请见:
http://www.cnblogs.com/coderzh/archive/2008/04/02/1135118.html

今天包装一个由开发写好的加密解模块,中间遇到不少问题,记录一下:

问题1: debug编的过,但运行时出现系统无法执行指定程序。。。release版本编不过(fatal error LNK1257: code generation failed)

解决办法:安装VS2005的SP1补丁(狂晕~~)

问题2:#pragma comment(lib, “zlibmdd.lib”) 编译后出现

解决办法:

    1.  将xxx.lib拷贝到工程当前目录

    2. “Project” - “Propertites” - “Linker” - “Input” - “Additional Dependencies”,把xxx.lib加进去

问题3:把开发的代码直接添加进工程后,编译,出现unexpected end of file while looking for precompiled hearder:

解决办法:找到出错的文件,右键“Property” - “C/C++” - “Precompiled Headers” - “Create/Use Precompild Header”,设置 “Not Using Precompiled Headers”

问题4:工程有的时候编译没问题,关闭后再打开时,编译出现_DllMain@12 already defined in MSVCRTD.lib(dllmain.obj):

解决办法:没有什么很好的解决办法,具体的原因不知。暂时使用下面的方法解决:

    “Project” - “Propertites” - “Linker” -“Command Line” ,加入:”/FORCE:MULTIPLE”     (注意:需要引号)

问题5:char* 与 wchar_t* 的转换

解决办法:使用 mbstowcs(wchar_t转char) 和 wcstombs(char* 转 wchar_t*)

#include   <stdlib.h>  
#include   
<stdio.h>  
   
void   main(   void   )  
{  
          
int   i;  
          
char         pmbnull     =   NULL;  
          
char         
pmbhello   =   (char   )malloc(   MB_CUR_MAX   );  
          wchar_t   
pwchello   =   LHi;  
          wchar_t   
pwc             =   (wchar_t   )malloc(   sizeof(   wchar_t   ));  
   
          printf(   
Convert   to   multibyte   string:\n   );  
          i   
=   wcstombs(   pmbhello,   pwchello,   MB_CUR_MAX   );  
          printf(   
\tCharacters   converted:   %u\n,   i   );  
          printf(   
\tHex   value   of   first   );  
          printf(   
   multibyte   character:   %#.4x\n\n,   pmbhello   );  
   
          printf(   
Convert   back   to   wide-character   string:\n   );  
          i   
=   mbstowcs(   pwc,   pmbhello,   MB_CUR_MAX   );  
          printf(   
\tCharacters   converted:   %u\n,   i   );  
          printf(   
\tHex   value   of   first   );  
          printf(   
   wide   character:   %#.4x\n\n,   pwc   );  
}
  

问题6:C++如何调用DLL

解决办法:详见http://blog.csdn.net/huahua70528158/archive/2007/11/08/1872827.aspx

    1. 隐式链接

#pragma comment(lib,"MyDll.lib")
extern "C"_declspec(dllimport) int Max(int a,int b);
#include
#include
"Dlltest.h"
void main()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        
int c;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        c
=Max(1,2)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)}
    2. 显式链接
void main(void)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        typedef 
int(*pMax)(int a,int b);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        typedef 
int(*pMin)(int a,int b);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        HINSTANCE hDLL;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        PMax Max
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        HDLL
=LoadLibrary("MyDll.dll");//加载动态链接库MyDll.dll文件;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
        Max=(pMax)GetProcAddress(hDLL,"Max");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        A
=Max(5,8);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        Printf(
"比较的结果为%d\n",a);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        FreeLibrary(hDLL);
//卸载MyDll.dll文件;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
}

问题7:C语言没有Dictionary,因此要返回Name,Value这样的键值对列表,采用返回两个数组的方式。需要使用到指针数组。

解决办法:指针数组使用示例

#include <stdio.h>


int main()
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)    
char *name[6= {"Rainer","Richie","Crystal","Stef","Sammy"};
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    
//定义一个指针数组,并将其初始化。这时候name[0]将存储字符串"Rainer"的
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    
//第一个字符'R'的地址,依此类推。
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
    char *p;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    
//定义一个指向字符变量的指针变量
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
    int i;
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    name[
5= "Jacky";
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    
//将数组第六个元素赋值为"Jacky"
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
    for(i = 0;i < 6;++i)
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)![](http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)    
{//输出数组内容
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
        p = *(name + i);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        
//name + i即为name[i],*name[i]即为数组name的第i个元素的值,
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)        
//即为第i个元素中字符串的首字符的地址
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
        printf("%s ",p);
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)       
//注意格式字符串中的s
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
    }

![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    printf(
"\n");
![](http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)    
return 0;
![](http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)}

[温馨提示]:该文章由原博客园导入而来,如排版效果不佳,请移步:http://www.cnblogs.com/coderzh/archive/2008/07/15/1243659.html

微信扫一扫交流

作者:CoderZh
微信关注:hacker-thinking (一个程序员的思考)
本文出处:https://blog.coderzh.com/2008/07/15/1243659/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。