Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 在Perl中使用Win32::API导入从dll函数调用返回字符串时遇到困难_Windows_Perl_Winapi - Fatal编程技术网

Windows 在Perl中使用Win32::API导入从dll函数调用返回字符串时遇到困难

Windows 在Perl中使用Win32::API导入从dll函数调用返回字符串时遇到困难,windows,perl,winapi,Windows,Perl,Winapi,函数如下所示,string myFunction(long,long*) 因此,我尝试了很多方法,但未能返回字符串。 请帮帮我 Win32::API->Import('My.dll','DWORD MyFunction(long a,long* b)')or die $^E; my $var = MyFunction(1,0); printf "%d : '%s'\n", length($var),$var; DWORD只是一个“long”类型,并且Win32::API不会对

函数如下所示,
string myFunction(long,long*)

因此,我尝试了很多方法,但未能返回字符串。
请帮帮我

Win32::API->Import('My.dll','DWORD MyFunction(long a,long* b)')or die $^E;  
my $var = MyFunction(1,0);  
printf "%d : '%s'\n", length($var),$var;  

DWORD
只是一个“
long
”类型,并且
Win32::API
不会对这样的返回值进行任何转换。如果函数返回一个
char*
,只需将其原型声明为
char*MyFunction(…)

或者使用中已定义的指向字符的指针的众多别名之一


Edit:它实际上就像设置一个返回
char*
的原型一样简单。复杂的部分是创建一个DLL,用于导出您(以及
Win32::API
)希望它导出的函数。例如,此代码创建自己的DLL,然后通过Win32::API导入并调用其函数,可在我的系统上运行(草莓Perl 5.12.0):

$STRAWBERRY=“C:/strawberry512”#YMMV
取消链接“my_func.dll”;
打开DLL_SRC,'>','my_func.c';
打印DLL_SRC q!
#包括
#包括
#包括
char*WINAPI MyFunc(int a,int b)
{
char*s=(char*)malloc(32);
如果(a==0&&b==0){
strcpy(s,“JAPH”);
}否则{
s[0]=32+(a%64);
s[1]=32+(b%64);
s[2]='\0';
}
申报表;
}
!;
关闭DLL_SRC;
打开DLL_DEF,“>”,“my_func.DEF”;
打印DLL_DEF“导出\nMyFunc\@8\n”;
关闭DLL_DEF;
系统($草莓/c/bin/gcc.exe)、“-c”、“my_func.c”)||
系统($草莓/c/bin/gcc.exe),
“-mdll”,
“-o”、“junk.tmp”,
“-Wl,--base file,my_func.tmp”,“my_func.o”)||
系统($草莓/c/bin/dlltool),
“--dllname”、“my_func.dll”,
“--基本文件”“my_func.tmp”,
“--output exp”,“my_func.exp”,
“--def”、“my_func.def”、“-k”)||
系统($草莓/c/bin/gcc),
“-mdll”,
“-o”、“my_func.dll”,
“我的职能”,
“-Wl,my_func.exp”)||
打印“my_func.dll似乎已成功创建。\n\n”;
使用Win32::API;
Win32::API->Import('my_func',
“char*MyFunc(int a,int b)”或die$$^E
$val=MyFunc(0,0);
打印$val;
打印MyFunc(1,65);
取消链接“my_func.dll”、“libmy_func.a”、“my_func.def”,
“my_func.o”、“my_func.exp”、“my_func.tmp”、“my_func.c”;

如果复制此示例时遇到问题,请从简单的函数开始,例如,一个不带参数并返回整数的普通函数,然后首先使其工作。检查
$
$^E
在每一步

字符串
不是C类型。MyFunction实际返回什么?
$STRAWBERRY = "C:/strawberry512";  # YMMV

unlink "my_func.dll";

open DLL_SRC, '>', 'my_func.c';
print DLL_SRC q!
#include <stdlib.h>
#include <stdio.h>
#include <windows.h>

char* WINAPI MyFunc(int a, int b)
{
    char *s = (char *) malloc(32);
    if (a==0 && b==0) {
        strcpy(s, "JAPH");
    } else {
        s[0] = 32 + (a % 64);
        s[1] = 32 + (b % 64);
        s[2] = '\0';
    }
    return(s);
}
!;
close DLL_SRC;

open DLL_DEF, '>', 'my_func.def';
print DLL_DEF "EXPORTS\nMyFunc\@8\n";
close DLL_DEF;

system("$STRAWBERRY/c/bin/gcc.exe", "-c", "my_func.c") ||
system("$STRAWBERRY/c/bin/gcc.exe", 
       "-mdll",
       "-o", "junk.tmp",
       "-Wl,--base-file,my_func.tmp", "my_func.o") ||
system("$STRAWBERRY/c/bin/dlltool",
       "--dllname", "my_func.dll",
       "--base-file", "my_func.tmp",
       "--output-exp", "my_func.exp",
       "--def", "my_func.def", "-k") ||
system("$STRAWBERRY/c/bin/gcc",
       "-mdll",
       "-o", "my_func.dll",
       "my_func.o",
       "-Wl,my_func.exp") ||
print "my_func.dll seems to have created successfully.\n\n";

use Win32::API;
Win32::API->Import('my_func', 
                   'char* MyFunc(int a, int b)') or die $!,$^E;
$val = MyFunc(0,0);
print $val;
print MyFunc(1,65);
unlink "my_func.dll", "libmy_func.a", "my_func.def",
   "my_func.o", "my_func.exp", "my_func.tmp", "my_func.c";