QTP调用DLL
[color=darkred][i][b]Search1:[/b][/i][/color]
關(guān)于DLL文件:
DLL嚴格意義上說 dll 是微軟的 私有格式, 不是 C/C++標準中的, 也無法跨平臺的。
其中作用為實現(xiàn)可重復(fù)性代碼的集合和exe沒有本質(zhì)區(qū)別,很難被反編譯,因此,即使有了dll文件,仍然不能看到里面具體寫了什么,當然現(xiàn)在有很多工具,能逐步識別dll文件的反編譯匯編語言
dll工程里面有個dllmain文件,相當于exe文件,但是dll文件不能單獨運行,此main文件里面有入口參數(shù),主要作用是機器判斷是線呈還是進程,就是一個空殼,和程序員無關(guān),switch ul reason for attach這個值。
test.h文件里主要寫具體此函數(shù)是做什么的。原理上可以是任何語言
qtp或者lr調(diào)用都可
QTP擁有自己的.NET Factory接口,以調(diào)用.NET生成的DLL,也可以使用Extern.Declare來進行外部的DLL的訪問。
語法:
Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)])
參數(shù)說明:
RetType: 方法返回值的類型。
MethodName:調(diào)用DLL文件中的某個方法的方法名。
LibName: DLL文件名。
Alias: 別名,當別名為空時,方法名和別名一樣(此參數(shù)通常為空)。
ArgType(s): 傳入的參數(shù)。
當然,要放在測試腳本路徑下,文件,Action中使用如上語法就可以調(diào)用了
LR也一樣LR_load_dll(testdll.dll)就可以調(diào)用了
當然還需要配置dat文件/dat directory之下將最后一行改為dll名稱
將winnt_dll屬性改為testdll.dll
[color=darkred][i][b]Search2:[/b][/i][/color]
第一種是ActiveX對象生成的Dll
在這里的外部dll非本機生成,則在qtp訪問前必須在本機器注冊,方法為:regsvr32 d:\dll文件路徑;取消注冊為:
regsvr32 /u D:\dll文件;
當然在本機器上生成的dll則不需要注冊;
注冊完成后,就可以在qtp中利用createobject方法調(diào)用注冊的dll文件了;
set res=CreateObject("文件名.類名")
res.方法
這樣就可以用res調(diào)用dll文件中的各種方法了。
第二種方法是利用Extern object
可以利用Extern.Declare 聲明,如下面所示:
Extern.Declare micInteger , "Add", "E:\QTP\DLL\LRDllTest.dll", "Sum", micInteger, micInteger
res = Extern.Add(1,1)
Msgbox res
sum為dll文件中的函數(shù),Add為sum所命的別名;
第三種方法是利用DotNetFactory對象
在QTP中為訪問.net對象,專門提供了DotNetFactory對象。通過DotNetFactory可以訪問.NET對象的屬性和方法。
[color=darkred][i][b]Search3:[/b][/i][/color]
What is a DLL?
Dynamic Linked Library is MS implementation of shared library concept in Windows. To understand this term more clearly, DLL can be broken down into Dynamic Link(ed) + Library
Dynamic Link means that the subroutines of a library are loaded into an application program at runtime, rather than being linked in at compile time, and remain as separate files on disk.
Library is a collection of subroutines
How to know about the functions in a DLL?
It is assumed that if you intend to call a DLL, you should know the function to be called from inside and what that function does. If you are clueless about how to get the function names you can get download Microsoft Dependency Walker or a 3rd party utility called PE Explorer which can help you to find the functions.
How can the functions inside DLL be called from QTP?
This part is actually simple and a two step process…
Declare the method using Extern.Declare
Example
Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
where:
micHwnd -the data type of value returned by method
FindWindow -the user supplied procedure name. You can set it to anything as long as it’s a valid syntax.
user32.dll -the DLL from where you wish to call the method
FindWindowA -The actual method name inside the DLL
Last two are the data types of the arguments that will be passed to the procedure
Call the method
Example:
Extern.FindWindow(“Notepad”, vbNullString)
To show the above process in action, here is an example to change the title of the Notepad window by calling the user32.dll
1: 'Declare FindWindow method
2: Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
3:’Declare SetWindowText method
5: Extern.Declare micLong, “SetWindowText”, “user32.dll”, “SetWindowTextA”, micHwnd, micString
7: ‘Get HWND of the Notepad window
8: hwnd = Extern.FindWindow(“Notepad”, vbNullString)
10: if hwnd = 0 then
12: MsgBox “Notepad window not found”
14: end if
16: ‘Change the title of the notepad window
17: res = Extern.SetWindowText(hwnd, “LearnQTP.com”)
Simple copy-paste the code above in your QTP ‘Expert View’. Open a blank notepad window. Run this code. You will now see that the name has changed from Untitled-Notepad to LearnQTP.com
Windows7+QTP/UFT11.5恢復(fù)30天試用破解:
http://www.51testing.com/?uid-306685-action-viewspace-itemid-831140
附件為QTPL, 去后綴 .zip
關(guān)于DLL文件:
DLL嚴格意義上說 dll 是微軟的 私有格式, 不是 C/C++標準中的, 也無法跨平臺的。
其中作用為實現(xiàn)可重復(fù)性代碼的集合和exe沒有本質(zhì)區(qū)別,很難被反編譯,因此,即使有了dll文件,仍然不能看到里面具體寫了什么,當然現(xiàn)在有很多工具,能逐步識別dll文件的反編譯匯編語言
dll工程里面有個dllmain文件,相當于exe文件,但是dll文件不能單獨運行,此main文件里面有入口參數(shù),主要作用是機器判斷是線呈還是進程,就是一個空殼,和程序員無關(guān),switch ul reason for attach這個值。
test.h文件里主要寫具體此函數(shù)是做什么的。原理上可以是任何語言
qtp或者lr調(diào)用都可
QTP擁有自己的.NET Factory接口,以調(diào)用.NET生成的DLL,也可以使用Extern.Declare來進行外部的DLL的訪問。
語法:
Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)])
參數(shù)說明:
RetType: 方法返回值的類型。
MethodName:調(diào)用DLL文件中的某個方法的方法名。
LibName: DLL文件名。
Alias: 別名,當別名為空時,方法名和別名一樣(此參數(shù)通常為空)。
ArgType(s): 傳入的參數(shù)。
當然,要放在測試腳本路徑下,文件,Action中使用如上語法就可以調(diào)用了
LR也一樣LR_load_dll(testdll.dll)就可以調(diào)用了
當然還需要配置dat文件/dat directory之下將最后一行改為dll名稱
將winnt_dll屬性改為testdll.dll
[color=darkred][i][b]Search2:[/b][/i][/color]
第一種是ActiveX對象生成的Dll
在這里的外部dll非本機生成,則在qtp訪問前必須在本機器注冊,方法為:regsvr32 d:\dll文件路徑;取消注冊為:
regsvr32 /u D:\dll文件;
當然在本機器上生成的dll則不需要注冊;
注冊完成后,就可以在qtp中利用createobject方法調(diào)用注冊的dll文件了;
set res=CreateObject("文件名.類名")
res.方法
這樣就可以用res調(diào)用dll文件中的各種方法了。
第二種方法是利用Extern object
可以利用Extern.Declare 聲明,如下面所示:
Extern.Declare micInteger , "Add", "E:\QTP\DLL\LRDllTest.dll", "Sum", micInteger, micInteger
res = Extern.Add(1,1)
Msgbox res
sum為dll文件中的函數(shù),Add為sum所命的別名;
第三種方法是利用DotNetFactory對象
在QTP中為訪問.net對象,專門提供了DotNetFactory對象。通過DotNetFactory可以訪問.NET對象的屬性和方法。
[color=darkred][i][b]Search3:[/b][/i][/color]
What is a DLL?
Dynamic Linked Library is MS implementation of shared library concept in Windows. To understand this term more clearly, DLL can be broken down into Dynamic Link(ed) + Library
Dynamic Link means that the subroutines of a library are loaded into an application program at runtime, rather than being linked in at compile time, and remain as separate files on disk.
Library is a collection of subroutines
How to know about the functions in a DLL?
It is assumed that if you intend to call a DLL, you should know the function to be called from inside and what that function does. If you are clueless about how to get the function names you can get download Microsoft Dependency Walker or a 3rd party utility called PE Explorer which can help you to find the functions.
How can the functions inside DLL be called from QTP?
This part is actually simple and a two step process…
Declare the method using Extern.Declare
Example
Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
where:
micHwnd -the data type of value returned by method
FindWindow -the user supplied procedure name. You can set it to anything as long as it’s a valid syntax.
user32.dll -the DLL from where you wish to call the method
FindWindowA -The actual method name inside the DLL
Last two are the data types of the arguments that will be passed to the procedure
Call the method
Example:
Extern.FindWindow(“Notepad”, vbNullString)
To show the above process in action, here is an example to change the title of the Notepad window by calling the user32.dll
1: 'Declare FindWindow method
2: Extern.Declare micHwnd, “FindWindow”, “user32.dll”, “FindWindowA”, micString, micString
3:’Declare SetWindowText method
5: Extern.Declare micLong, “SetWindowText”, “user32.dll”, “SetWindowTextA”, micHwnd, micString
7: ‘Get HWND of the Notepad window
8: hwnd = Extern.FindWindow(“Notepad”, vbNullString)
10: if hwnd = 0 then
12: MsgBox “Notepad window not found”
14: end if
16: ‘Change the title of the notepad window
17: res = Extern.SetWindowText(hwnd, “LearnQTP.com”)
Simple copy-paste the code above in your QTP ‘Expert View’. Open a blank notepad window. Run this code. You will now see that the name has changed from Untitled-Notepad to LearnQTP.com
Windows7+QTP/UFT11.5恢復(fù)30天試用破解:
http://www.51testing.com/?uid-306685-action-viewspace-itemid-831140
附件為QTPL, 去后綴 .zip
總結(jié)
- 上一篇: QTP中对用户自定义环境变量的XML操作
- 下一篇: QTP中字符串替换函数