Silktest调用VC++编写的DLL文件

Silktest 调用 VC++ 编写的DLL文件
一、创建dll文件
(1)在Visual studio 2008上,点击New新建一个工程(选择 Win32project,并输入工程名字MyDll),点击OK,点击Next---》 Next。
(2)在Application Settings 页面中Application Type选择 dll;在AdditionalOptions 选择Empty project。点击“Finish”按钮。
(3)在header file中 创建一个头文件MyDll.h文件内容为:
 extern "C" _declspec(dllexport) int Min(int a, int b);

(4)在Resouce files 中创建一个C++源文件MyDll.cpp,文件内容为:
 #include "MyDll.h"
#include <stdio.h>
int Min(int a, int b)
{
 printf("Min is called");

 if (a >= b) return b;

 else return a;
}
(5)编译文件,Visual studio会提示 “Executable For DebugSession”对话框,关闭这个对话框,此时文件已经编译好了,dll文件已经生成。
(6)在工程文件(MyDll文件夹)中的Debug文件中将MyDll.dll文件拷贝至silktest工程文件下(如:在工程文件目录下建一个scripts文件夹)
二、Silktest 中调用dll 文件。
(1)在silktest工程中创建一script文件(如:MyDll.t)
(2)文件内容如下:
 dll "MyDll.dll"
   
testcase MyDll()
  Print(Min(500,600))
运行:
结果返回 500
看看是不是很简单,呵呵!


这里需要说明几点,要特别注意:

  1. 由 于是使用C++语言,C++支持函数重载。因此导出函数的函数明会因为重载有所变化。所以需要加上 extern "C"强制使用C语言进行导出。当然这样一来,导出函数也就不能出现重载版本了。
  2. 另外,由于很多DLL的使用者是使用.lib文件+.h文 件的方法(静态方法)访问DLL的,而非LoadLibrary +GetProcAddress的方法(动态方法)。因此我们书写.h文件的时候做一个宏判断如果是在DLL编译中,就定义EXTERN为__declspec(dllexport)。如果是用户使用就定义EXTERN为extern__declspec(dllimport)。
  3. 对 于C++导出给其他语言(Delphi, C++ Builder)的DLL.要根据其他语言函数的调用方式(CallingConvention)加入适当的修饰符。比如给delphi要用pascal修饰,给WindowsAPI回调要使用stdcall等等。

Common DLL problems

Here are some issues that could come up ifyou are calling DLL functions in a script.

Error in results file: dll notfound

This usually means that your path does not include the directorycontaining the DLL. If you are running remotely, make sure that thepath on the machine running the Agent includes the DLLdirectory.

Error after compile: dll not found

In the DLL declaration, use the fully qualified path of the DLL,not just the file name.

Error in results file: function<name> not found in dll

Probably the DLL is a C++ library and the function name has beenmangled. To use functions in a C++ library, you need to wrap thefunctions with the C wrapper and recompile. Then SilkTest canaccess the function in the library.

If this is not the problem, there might be a typo in thefunction name in the DLL.

Error in results file: dll could not beloaded

Make sure the directory containing the DLL is on the path.

Warning in results file: String buffer size was increased from x to 256 characters.

If the user calls a DLL function with an output string bufferthat is less then the minimum size of 256 characters, the originalstring buffer is resized to 256 characters and a warning isprinted. This warning, "String buffer size was increased fromx to 256 characters" (where x is the length of the given string plus one) alertsthe user to a potential problem where the buffer used might beshorter than necessary.

Difficulty creating DLLs to use withSilkTest

Only specific data types are compatible with 4Test. These datatypes are listed in C data types for DLLfunctions.

If your DLL calls have data types not supported by 4Test, thenthe functions must be wrapped such that only compatible data typesare used for the return type and arguments of the function. Anydata types can be used inside the DLLfunction.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值