调用从C#code德尔福DLL的方法 [英] Calling a delphi DLL method from C# Code
问题描述
我试图调用从C#ASP.NET code德尔福的功能。该函数的声明如下:
I am trying to call a Delphi function from C# ASP.NET code. The function's declaration looks like this:
function SomeFunction(const someString, SomeOtherString: string): OleVariant;
这是我的C#code我有这个code:
From my C# code I have this code:
[DLLImport(MyDLL.dll", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Ansi)]
public static extern object SomeFunction(string someString, string SomeOtherString);
我每次调用这个方法,并将其保存为一个对象,我得到的P / Invoke错误。我从来没有所谓的非托管code,从我的C#之前,让我有点不知所措。
Every time I call this Method and store it as an object, I get a P/Invoke error. I've never called unmanaged code from my C# before, so I'm kind of at a loss.
推荐答案
您不能调用的是的DLL函数,因为它采用了德尔福的具体字符串
数据类型,它在非英巴卡迪诺产品不等价的。 (即使你的C#code能比赛的结构的德尔福字符串类型,您还需要分配使用DLL的内存管理器,它几乎可以肯定不会导出的内存。)
You cannot call that DLL function because it uses the Delphi-specific string
data type, which has no equivalent in non-Embarcadero products. (Even if your C# code can match the structure of Delphi's string type, you would also need to allocate the memory using the DLL's memory manager, which it almost certainly doesn't export.)
如果您需要更改DLL,然后进行参数的类型PAnsiChar或PWideChar的能力。 (从C#的声明,它看起来像你想PAnsiChar。)这就是该DLL应该使用所有沿。
If you have the ability to change the DLL, then make the parameters have type PAnsiChar or PWideChar. (From your C# declaration, it looks like you want PAnsiChar.) That's what the DLL should have used all along.
如果你不能改变的DLL,然后写一个使用PAnsiChar或PWideChar,然后转发这些参数原来的DLL德尔福在Delphi或C ++ Builder的包装DLL。或者大声抱怨到DLL的供应商,并要求使用是更友好的其他语言类型的新版本。
If you can't change the DLL, then write a wrapper DLL in Delphi or C++ Builder that uses PAnsiChar or PWideChar and then forwards those parameters to the original Delphi DLL. Or complain loudly to the DLL vendor and request a new version that uses types that are more friendly to other languages.
这篇关于调用从C#code德尔福DLL的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!