Delphi根据手册运行:
function SET_XML(var path: PAnsiChar): LongInt;
function GET_CALCULATION_FAN_ALONE(var fanDescription: PAnsiChar): LongInt;
根据手册在VB中使用:
Public Declare Function SET_XML_PATH Lib "fan.dll" (ByRef path As String) As Long
Public Declare Function GET_CALCULATION_FAN_ALONE Lib "fan.dll" (ByRef path As String) As Long
Sub Main()
Dim a As Long, b As Long, Str_Result As String, Str_Input As String
Str_Input = "C:\Users\Sebastiaan\Documents\Visual Studio 2010\Projects\Lucam selectie\Lucam selectie\bin"
a = SET_XML_PATH(Str_Input)
Str_Result = "65464;;;1,2;;23;424,8;0,3766;;"
b = GET_CALCULATION_FAN_ALONE(Str_Result)
End Sub
我确实用C#
重写了它const string _dllLocation = "EbmPapstFan.dll";
[DllImport(_dllLocation)]
public static extern long SET_XML_PATH(ref String path);
[DllImport(_dllLocation)]
public static extern long GET_CALCULATION_FAN_ALONE(ref String fanDescription);
public Main()
{
String path = @"C:\Users\Sebastiaan\Documents\Visual Studio 2010\Projects\Lucam selectie\Lucam selectie\bin";
long a = SET_XML_PATH(ref path);
String fanDescription = "65464;;;1,2;;23;424,8;0,3766;;";
long c = GET_CALCULATION_FAN_ALONE(ref fanDescription);
}
运行de application时,我得到一个AccessViolationException
尝试读取或写入受保护的内存。这通常表明其他内存已损坏。
当设置一个断点并逐步调试代码时,没有什么东西可以正常运行。有时我在调试模式下出错(在线“长c = GET_CALU ......”)
我做错了什么?
答案 0 :(得分:3)
我不熟悉从C#调用Delphi,但是从快速搜索看来,调用约定存在一些问题(如何从堆栈中推送和弹出值)。请参阅以下主题:
Calling a Delphi DLL from C# produces unexpected results
Calling a delphi DLL method from C# Code
由于“fastcall”调用约定,听起来可能无法直接调用它。您可以尝试更改调用约定,这值得快速测试。见本页:
http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.callingconvention.aspx
如果这不起作用,我可能只会编写一个调用Delphi DLL的C包装DLL,并验证它是否有效(确保它不仅仅是DLL中的错误)。如果是,那么你可以通过在你的包装DLL函数上使用DllImport从C#程序调用C DLL。
希望有所帮助,
约翰