Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.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
返回字符串的C#PInvoke Delphi dll仅在IIS中失败_C#_Delphi_Iis_Pinvoke - Fatal编程技术网

返回字符串的C#PInvoke Delphi dll仅在IIS中失败

返回字符串的C#PInvoke Delphi dll仅在IIS中失败,c#,delphi,iis,pinvoke,C#,Delphi,Iis,Pinvoke,我有一个Delphi 7 dll,可导出以下函数: function StringTest(var StringOut : pchar) : boolean; stdcall; begin GetMem(StringOut, 100); StrPCopy(StringOut, 'Test output string.'); result := true; end; 此函数在C#中导入,如下所示: [DllImport(@"C:\\Test\\DelphiTest.dl

我有一个Delphi 7 dll,可导出以下函数:

function StringTest(var StringOut : pchar) : boolean; stdcall;

begin
    GetMem(StringOut, 100);
    StrPCopy(StringOut, 'Test output string.');
    result := true;
end;
此函数在C#中导入,如下所示:

[DllImport(@"C:\\Test\\DelphiTest.dll")]
public static extern bool StringTest(out string stringOut);

当我从WPF应用程序调用导入时,它工作正常,并且我在out参数中看到返回的测试字符串。当我在卡西尼号的一个网站上尝试同样的事情时,效果也很好。然而,当我从IIS7中托管的站点运行该方法时,它失败了。如果我注释掉GetMem和StrPCopy行,则函数在IIS中返回“true”。在IIS托管的站点中,如何从Delphi将一些字符串数据返回到C#中?

这不是“普通”dll函数返回字符串的方式。代码中不清楚谁应该释放字符串。也许这就是.Net并不总是喜欢它的原因。调用者应该分配足够的内存来放入结果字符串

function StringTest(const StringOut : pchar; MaxLen: Integer) : Boolean; stdcall;
begin
    StrPLCopy(StringOut, 'Test output string.', MaxLen);
    result := true;
end;

[DllImport(@"C:\\Test\\DelphiTest.dll", CharSet = CharSet.Ansi)]
public static extern bool StringTest(ref string stringOut, int maxLen);

这不是“普通”dll函数返回字符串的方式。代码中不清楚谁应该释放字符串。也许这就是.Net并不总是喜欢它的原因。调用者应该分配足够的内存来放入结果字符串

function StringTest(const StringOut : pchar; MaxLen: Integer) : Boolean; stdcall;
begin
    StrPLCopy(StringOut, 'Test output string.', MaxLen);
    result := true;
end;

[DllImport(@"C:\\Test\\DelphiTest.dll", CharSet = CharSet.Ansi)]
public static extern bool StringTest(ref string stringOut, int maxLen);

您确实应该更具体地了解错误的性质。简单地说“它失败了”并不能告诉任何人任何事情。你真的应该更具体地说明错误的性质。简单地说“它失败了”并不能告诉任何人任何事情。