google搜索 51Testing站内搜索                    软件测试门户 | 软件测试培 训 | 文章资料精选 | 软件测试论坛 | 软件测试博客 | 测试招聘求职 
打印

在QTP中引用C#编写的dll的问题

在QTP中引用C#编写的dll的问题


In QTP script, i want to use external dll which is written by C# language.
I produced a very simple dll, pls see the following code

namespace ADDFun
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class ADDFun
{
public int AddFunction()
{
return 1;
}
}
}

Then in the QTP, i used it as the following way
Extern.Declare micInteger,"AddFunction", "C:\ADDFun.dll", "AddFunction"

The function has no parameter, and it will return a number.

Then i ran this script, a error occured, it said that "Invalid procedure call or argument" .

Can anyone help me?
Thanks

TOP

Probably it doesn't work that simply as you thought.sdlkfj2

To call an external function through Extern.Declare within a QTP test script is applicable for those functions like Windows API.
But it won't work for a C# dll like you developed because no function is exported directly from that dll.

Actually, the function AddFunction is exported through a public method in the class ADDFun from the dll.
Suppose I want to call the method AddFunction in another C# application, first I would import ADDFun.dll into that application, And then in the application, I would implement several statements like below:
//Declare an object instance with ADDFun class
ADDFun objADDFun;
//Call the function from the object instance
objADDFun.AddFunction();
It's impossible to do the same in a QTP script.

Maybe you can find a way to directly export a simple function instead of a class in a C# dll. Otherwise, you won't be able to call the function.

TOP

靠,用英语来贼啊
看了半天,看到最后一句
Otherwise, you won't be able to call the function.
sdlkfj4
我现在用VB6.0也不行啊

TOP

在那个用C#开发的dll里面导出的不是一个简单的Function,而是一个Class,本来想导出的Function是那个Class的一个Public方法。
在其他应用程序里面,只能先声明这个类的对象实体,然后才能调用那个方法。显然QTP是不支持的,QTP只能引用包含了类似WinAPI的外部dll。

TOP

Please see other people feedback:

It should be signed DLL and registered in GlobalCashe

[for Extern.Declare calling]

Or:

written as COM [for calling via Object Creation]

If you can code on C# - there is new possibility in QTP 9.1 - DotNetFactory object. It's amazing and powerful.
You can write DLL as well as use direct C# coding in TestCase with instantiating C# public classes.
Play with that.

Janna

TOP

SQAForum里面有好几个主题贴都深入讨论了QTP如何调用Dll。
不同的Dll输出方式是有区别的,QTP引用dll可以分成两部分来看,Dll输出什么,QTP如何调用。我觉得最初那段C#代码输出的不是一般意义的函数,而是一个类,该类包含了一个方法是希望被QTP调用的。
QTP9.1里面的DotNetFactory方法,可以闯将已经注册的类型。但是前提是用C#开发出来的类型要注册到系统里面。

TOP

呵呵,小阎为什么发帖用英文?
C#没用过帮不了你sdlkfj7
引用:
原帖由 Alexander 于 2007-4-26 16:29 发表
In QTP script, i want to use external dll which is written by C# language.
I produced a very simple dll, pls see the following code

namespace ADDFun
{
///
/// Summary description for Cla ...

TOP

最终解决方案


其实摸索这个问题好像是很难,但是其实很简单,大家来看看我的代码
using System;
using System.Collections.Generic;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
        public int Test()
        {
            return 10;
        }
    }
}

下面就是关键了,
1.必须用VS2005 的命令行 键人: sn -k "XXXX\*.snk" 其中X,*是路径和文件名
2.打开AssemblyInfo.cs文件, 键入
  [assembly: AssemblyKeyFile("D:\\Projects\\MyTest\\Dll\\ClassLibrary1\\ClassLibrary1\\bin\\Debug\\mykey.snk")]
3.Build
4.把这个dll文件靠本到目标机子中的 system\assembly文件夹中
5.打开QTP 9.2(好像9.0版本及以上都可以)
  键入 :
   Set myclass=DotNetFactory.CreateInstance("ClassLibrary1.Class1","ClassLibrary1")
  c1=myclass.Test()
  MsgBox c1
  显示是10

结论: 大家看是不是很简单,主要是强名称的问题,对于非强名称的dll,现在还不知道有什么办法调用。

TOP

呵呵 强人啊
顶一下
马上下来试试

当测试已成为一种习惯...

TOP

厉害

TOP

学习了,顶
从今天起, 做一个勤快的人
发帖, 回帖,灌水, 样样都不少

TOP

你们试过能行???

TOP

昨我试过不行!

TOP

1.必须用VS2005 的命令行 键人: sn -k "XXXX\*.snk" 其中X,*是路径和文件名
我做.它说不支持此命令
Set myclass=DotNetFactory.CreateInstance("ClassLibrary1.Class1","ClassLibrary1")
它说cannot load assembly qtpinit
具体此DLL要放在哪里.. 可以在代码那里指定它放的位置不?

TOP

问题解决了.

TOP

请问 QTP 里面的 对象模型 在 C++ 里面怎么引用啊?

谢谢了哈!!

TOP

Cannot load Assembly ClassLibrary1
我这里这样提示~啥情况?>
我用QTP9.0

[ 本帖最后由 milan330330 于 2007-10-16 15:45 编辑 ]

TOP

VC中,这种类的调用,好象是实现不了,不知道大家有什么好的办法.看这个贴子.
http://bbs.51testing.com/thread-95390-1-1.html

TOP

回复 8# 的帖子


我使用强名称,用sn -v XX.dll 察看,已经成功,为什么在qtp调用的时候提示:文件格式不正确?希望你能予以解答

TOP

同志们谁能帮助解决一下

TOP

 
当前时区 GMT+8, 现在时间是 2008-10-13 06:52Copyright(C)上海博为峰软件技术有限公司 2001-2007 电话:021-64471599-8017
当您在访问网站、论坛及博客过程中遇到问题时可发送email:webmaster@51testing.com或发送论坛短信至管理员风在吹