一、创建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文件内容为:
|
(4)在Resouce files 中创建一个C++源文件MyDll.cpp,文件内容为:
#include <stdio.h> int Min(int a, int b) { } |
(6)在工程文件(MyDll文件夹)中的Debug文件中将MyDll.dll文件拷贝至silktest工程文件下(如:在工程文件目录下建一个scripts文件夹)
二、Silktest 中调用dll 文件。
(1)在silktest工程中创建一script文件(如:MyDll.t)
(2)文件内容如下:
testcase MyDll() |
结果返回 500
看看是不是很简单,呵呵!
这里需要说明几点,要特别注意:
- 由 于是使用C++语言,C++支持函数重载。因此导出函数的函数明会因为重载有所变化。所以需要加上 extern "C"强制使用C语言进行导出。当然这样一来,导出函数也就不能出现重载版本了。
- 另外,由于很多DLL的使用者是使用.lib文件+.h文 件的方法(静态方法)访问DLL的,而非LoadLibrary +GetProcAddress的方法(动态方法)。因此我们书写.h文件的时候做一个宏判断如果是在DLL编译中,就定义EXTERN为__declspec(dllexport)。如果是用户使用就定义EXTERN为extern__declspec(dllimport)。
- 对 于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.