QTP调用DLL

Search1:
关于DLL文件:
DLL严格意义上说 dll 是微软的 私有格式, 不是 C/C++标准中的, 也无法跨平台的。

其中作用为实现可重复性代码的集合和exe没有本质区别,很难被反编译,因此,即使有了dll文件,仍然不能看到里面具体写了什么,当然现在有很多工具,能逐步识别dll文件的反编译汇编语言

dll工程里面有个dllmain文件,相当于exe文件,但是dll文件不能单独运行,此main文件里面有入口参数,主要作用是机器判断是线呈还是进程,就是一个空壳,和程序员无关,switch ul reason for attach这个值。

test.h文件里主要写具体此函数是做什么的。原理上可以是任何语言

qtp或者lr调用都可

  QTP拥有自己的.NET Factory接口,以调用.NET生成的DLL,也可以使用Extern.Declare来进行外部的DLL的访问。

  语法:

  Extern.Declare(RetType, MethodName, LibName, Alias [, ArgType(s)])

  参数说明:

  RetType: 方法返回值的类型。

  MethodName:调用DLL文件中的某个方法的方法名。

  LibName: DLL文件名。

  Alias: 别名,当别名为空时,方法名和别名一样(此参数通常为空)。

  ArgType(s): 传入的参数。

当然,要放在测试脚本路径下,文件,Action中使用如上语法就可以调用了

LR也一样LR_load_dll(testdll.dll)就可以调用了

当然还需要配置dat文件/dat directory之下将最后一行改为dll名称
将winnt_dll属性改为testdll.dll



Search2:
第一种是ActiveX对象生成的Dll
  在这里的外部dll非本机生成,则在qtp访问前必须在本机器注册,方法为:regsvr32 d:\dll文件路径;取消注册为:
  regsvr32  /u D:\dll文件;
  当然在本机器上生成的dll则不需要注册;
  注册完成后,就可以在qtp中利用createobject方法调用注册的dll文件了;
  set res=CreateObject("文件名.类名")
  res.方法
  这样就可以用res调用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文件中的函数,Add为sum所命的别名;
  第三种方法是利用DotNetFactory对象
  在QTP中为访问.net对象,专门提供了DotNetFactory对象。通过DotNetFactory可以访问.NET对象的属性和方法。



Search3:
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恢复30天试用破解:
http://www.51testing.com/?uid-306685-action-viewspace-itemid-831140
附件为QTPL, 去后缀 .zip

猜你喜欢

转载自ktc7000.iteye.com/blog/1753123
QTP
今日推荐