我想问个问题,我想通过Win32::API来调用我的dll中的函数。如果参数是integer类型,能够正常调用,返回正确的计算结果。但是,如果参数是double类型,就会报错!
"Perl Command Line Interpreter has encountered a problem and needs to close. We are sorry for the inconvenience"
是不是Win32::API不支持double类型的参数?
#这个能正确调用
my $function=Win32::API->new('TestDLL', 'int foo1(int P, int T)');
my $return=$function->Call(100, 200);
#这个调用会出错
my $function2=Win32::API->new('TestDLL', 'double foo2(double P, double T)');
my $return2=$function2->Call(10.0, 20.0);
The third parameter, the input parameter list, specifies how many arguments the function wants, and their types. It can be passed as a single string, in which each character represents one parameter, or as a list reference. The following forms are valid:
"abcd"
[a, b, c, d]
\@LIST
But those are not:
(a, b, c, d)
@LIST
The number of characters, or elements in the list, specifies the number of parameters, and each character or element specifies the type of an argument; allowed types are:
I: value is an integer (int)
N: value is a number (long)
F: value is a floating point number (float)
D: value is a double precision number (double)
C: value is a char (char)
P: value is a pointer (to a string, structure, etc...)
S: value is a Win32::API::Struct object (see below)
K: value is a Win32::API::Callback object (see Win32::API::Callback)
Our function needs two parameters: a number (DWORD) and a pointer to a string (LPSTR):
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', ...
Then
The fourth and final parameter is the type of the value returned by the function. It can be one of the types seen above, plus another type named V (for void), used for functions that do not return a value. In our example the value returned by GetTempPath() is a DWORD, so our return type will be N:
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
Now the line is complete, and the GetTempPath() API is ready to be used in Perl. Before calling it, you should test that $GetTempPath is defined, otherwise either the function or the library could not be loaded; in this case, $! will be set to the error message reported by Windows. Our definition, with error checking added, should then look like this:
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
if(not defined $GetTempPath) {
die "Can't import API GetTempPath: $!\n";
}
use Win32::API;
# import the cos function
$cos = new Win32::API('msvcrt', 'cos', 'D', 'D');
# $cos is an object, use the Call() method on it
$result = $cos->Call(2.0);
print "cos(2.0) = $result\n";
您好!
很高兴能再perl大会上听到您的演讲,很遗憾由于有事没能和您当面交流。大会上您说的可以安排一个面试的机会,不知道现在和您联系可不可以。
我的MSN是lifuxue223521@hotmail.com
QQ是 236130132
期待您的消息,谢谢!
您好,可以的。
您感兴趣的话,可以先把简历发到我的邮箱 junliang.li#alibaba-inc.com。
我想问个问题,我想通过Win32::API来调用我的dll中的函数。如果参数是integer类型,能够正常调用,返回正确的计算结果。但是,如果参数是double类型,就会报错!
"Perl Command Line Interpreter has encountered a problem and needs to close. We are sorry for the inconvenience"
是不是Win32::API不支持double类型的参数?
#这个能正确调用
my $function=Win32::API->new('TestDLL', 'int foo1(int P, int T)');
my $return=$function->Call(100, 200);
#这个调用会出错
my $function2=Win32::API->new('TestDLL', 'double foo2(double P, double T)');
my $return2=$function2->Call(10.0, 20.0);
The third parameter, the input parameter list, specifies how many arguments the function wants, and their types. It can be passed as a single string, in which each character represents one parameter, or as a list reference. The following forms are valid:
"abcd"
[a, b, c, d]
\@LIST
But those are not:
(a, b, c, d)
@LIST
The number of characters, or elements in the list, specifies the number of parameters, and each character or element specifies the type of an argument; allowed types are:
I: value is an integer (int)
N: value is a number (long)
F: value is a floating point number (float)
D: value is a double precision number (double)
C: value is a char (char)
P: value is a pointer (to a string, structure, etc...)
S: value is a Win32::API::Struct object (see below)
K: value is a Win32::API::Callback object (see Win32::API::Callback)
Our function needs two parameters: a number (DWORD) and a pointer to a string (LPSTR):
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', ...
Then
The fourth and final parameter is the type of the value returned by the function. It can be one of the types seen above, plus another type named V (for void), used for functions that do not return a value. In our example the value returned by GetTempPath() is a DWORD, so our return type will be N:
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
Now the line is complete, and the GetTempPath() API is ready to be used in Perl. Before calling it, you should test that $GetTempPath is defined, otherwise either the function or the library could not be loaded; in this case, $! will be set to the error message reported by Windows. Our definition, with error checking added, should then look like this:
$GetTempPath = new Win32::API('kernel32', 'GetTempPath', 'NP', 'N');
if(not defined $GetTempPath) {
die "Can't import API GetTempPath: $!\n";
}
I don't know if you can understood .
这个我看过,但是没能成功!我觉得Win32::API可能不支持double类型的参数和返回值。
你能不能试验确认一下?
刚刚测试《Perl高级编程(Profssional Perl Programming)》中的例子
use Win32::API;
# import the cos function
$cos = new Win32::API('msvcrt', 'cos', 'D', 'D');
# $cos is an object, use the Call() method on it
$result = $cos->Call(2.0);
print "cos(2.0) = $result\n";
仍然没有成功!你试一试,能不能行?
你那个测试提示什么错误?
我一般在linux下,现在没有windows平台。