4、 定义函数类型
在调用DLL的函数之前,我们要在TC中使用DefineProc方法为它定义函数类型,告诉TC函数名称、参数和结果类型。例如下面脚本定义了DrawTextExA函数类型:
// Registers the function type in TestComplete
Def_DLL.DefineProc('DrawTextExA', // function name
vt_i4, // device context
vt_lpstr, // pointer to the string to be drawn
vt_i4, // the string length
Def_Rect, // pointer to the RECT structure
vt_i4, // additional drawing settings
Def_DrawTextParams, // pointer to DRAWTEXTPARAMS
vt_i4); // result type
5、 为结构填充数据
在使用前面创建的RECT类型变量之前,应该为其填充数据:
r := DLL.New('RECT');
r.left := 50;
r.top := 50;
r.right := 350;
r.bottom := 100;
6、 指定string参数的值
DrawTextEx方法的第二个参数是指定在屏幕上画出来的某个字符串,应该按下面方法准备这段字符串:
LpStr:=DLL.New(‘LPSTR’,256)
LpStr.Text:=’My text’;
7、 把DLL加载到内存
使用DLL.Load方法加载指定的DLL,例如:
Lib:=DLL.Load(‘C:\Windows\System32\USER32.DLL’,’USER32’);
对于系统DLL,例如上面的USER32.DLL,我们可以忽略第一个参数,直接使用:
Lib:=DLL.Load(‘USER32’);
8、 调用函数
现在,我们可以调用DLL的函数了,例如,以下脚本调用DrawTextExA方法:
i := Lib.DrawTextEx(dc, LpStr, Length(LpStr.Text), r,
DT_PATH_ELLIPSIS, dtp);
完整的调用USER32.DLL的DrawTextEx函数的脚本如下:
procedure CallRoutineFromDLL;
const
DT_PATH_ELLIPSIS = $4000;
var
p, w, s : OleVariant;
Def_DrawTextParams, Def_Rect, Def_DLL, Lib : OleVariant;
dtp, LpStr, r, dc, i : OleVariant;
begin
// Text to draw
s := 'My string: C:\Program Files\Files.txt';
// Defines the dll type
Def_DLL := DLL.DefineDLL('USER32');
// Registers the DRAWTEXTPARAMS type in TestComplete
Def_DrawTextParams := DLL.DefineType('DRAWTEXTPARAMS',
vt_i4, 'cbSize',
vt_i4, 'iTabLength',
vt_i4, 'iLeftMargin',
vt_i4, 'iRightMargin',
vt_i4, 'uiLengthDrawn');
// Registers the RECT type in TestComplete
Def_Rect := DLL.DefineType('RECT',
vt_i4, 'left',
vt_i4, 'top',
vt_i4, 'right',
vt_i4, 'bottom');
// Registers the function type in TestComplete
Def_DLL.DefineProc('DrawTextExA', // function name
vt_i4, // device context
vt_lpstr, // pointer to the string to be drawn
vt_i4, // the string length
Def_Rect, // pointer to the RECT structure
vt_i4, // additional drawing settings
Def_DrawTextParams, // pointer to DRAWTEXTPARAMS
vt_i4); // result type
// Creates an alias for DrawTextExA
Def_DLL.DefineAlias('DrawTextEx', 'DrawTextExA');
// Registers the types of the GetDC
// and ReleaseDC routines. They will
// be used to work with the device context
Def_DLL.DefineProc('GetDC', vt_i4, vt_i4);
Def_DLL.DefineProc('ReleaseDC', vt_i4, vt_i4, vt_i4);
// Creates the RECT structure and fills it with data
r := DLL.New('RECT');
r.left := 50;
r.top := 50;
r.right := 350;
r.bottom := 100;
// Creates the DRAWTEXTPARAMS structure and fills it with data
dtp := DLL.New('DRAWTEXTPARAMS');
dtp.cbSize := 20; // Structure size
dtp.iTabLength := 2;
dtp.iLeftMargin := 2;
dtp.iRightMargin := 2;
dtp.uiLengthDrawn := 0;
// Creates the string parameter
LpStr := DLL.New('LPSTR', 256);
LpStr.Text := s;
// Loads the dll
// We use such a brief notation,
// because USER32.DLL is a system library and
// the dll type name (USER32) coincides with
// the dll file name
Lib := DLL.Load('USER32');
// Gets the device context for drawing
dc := Lib.GetDC(0);
// Calls the function from the DLL
i := Lib.DrawTextEx(dc, LpStr, Length(LpStr.Text), r,
DT_PATH_ELLIPSIS, dtp);
// Releases the device context
Lib.ReleaseDC(0, dc);
end;