QTP调用DLL

2 篇文章 0 订阅
[color=darkred][i][b]Search1:[/b][/i][/color]
关于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


[color=darkred][i][b]Search2:[/b][/i][/color]
第一种是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对象的属性和方法。


[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恢复30天试用破解:
http://www.51testing.com/?uid-306685-action-viewspace-itemid-831140
附件为QTPL, 去后缀 .zip
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值