我有Delphi編寫的非託管的DLL文件,其中包含如下定義的函數:編組PDouble(C# - >德爾福)
function F(tgran: integer; inputs: PDouble; goutp, outputs, toutp: PDouble): integer; stdcall; external 'mydll.dll';
我已經寫在C#中的適配器應該幫助我消耗它。
[UnmanagedFunctionPointer(APIAdapter.Convention)]
public delegate int FDelegate(int tgran, IntPtr inputs, IntPtr goutp, IntPtr outputs, IntPtr toutp);
public class APIAdapter : IDisposable
{
public const string DllName = "mydll.dll";
public const CallingConvention Convention = CallingConvention.StdCall;
public FDelegate F;
[DllImport("kernel32")]
private static extern IntPtr LoadLibrary(string lpLibFileName);
[DllImport("kernel32")]
private static extern bool FreeLibrary(IntPtr hModule);
[DllImport("kernel32.dll")]
private static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
private IntPtr _dllHandle;
public APIAdapter()
{
_dllHandle = LoadLibrary(DllName);
F = (FDelegate)GetFunction<CalcCavSpDelegate>("F");
}
private Delegate GetFunction<T>(string procName)
{
IntPtr procAddress = GetProcAddress(_dllHandle, procName);
return Marshal.GetDelegateForFunctionPointer(procAddress, typeof(T));
}
~APIAdapter()
{
Dispose(false);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
private void Dispose(bool disposing)
{
if (disposing)
{
}
while (FreeLibrary(_dllHandle))
{
}
}
}
的使用是非常簡單的:
using(var api = new APIAdapter())
{
// Call API functions
}
的問題是DLL文件中會發生,一個AccessViolationException。
我試過把012變量作爲double[], double* (unsafe code), IntPtr
。不管我選擇什麼方法,都是一樣的故事。我試圖大幅增加傳入的數組的大小,以排除數組索引中的錯誤 - AccessViolation異常。
將一個PDouble傳遞給一個非託管的Delphi DLL文件的正確方法是什麼?
由於我們不知道數組有多大以及誰負責分配它們,所以不可能爲此提供代碼。另外,我敦促你用'DllImport'重寫代碼,以便問題可以集中在真正的問題上。 – 2012-07-12 12:19:36
同樣令人驚訝的是,您包含了所有樣板鏈接代碼,但省略了實際調用本地DLL的代碼! – 2012-07-12 12:33:57
我將很快更新上面的帖子(tm) – Maciek 2012-07-12 15:03:30