pythnet 非託管程式碼呼叫細節處理
背景:有一DLL,C#的例程裡沒有用引用和using,而是直接用
[DllImport("FY4400.dll")]
public static extern IntPtr FY4400_OpenDevice(Int32 Devnum);
[DllImport("FY4400.DLL")]
public static extern Int32 FY4400_CloseDevice(IntPtr hDevice);
[DllImport("FY4400.DLL")]
public static extern Int32 FY4400_DI(IntPtr hDevice);
做法是:
1.在類下面,我們重新做個DLL,把這些宣告的函式,重新做成方法給python呼叫:
public int openCard()
{
hDevice= FY4400_OpenDevice(0);
if (hDevice== (IntPtr)(-1))
{ a = 5; }
//構造視窗的時候呼叫此函式
return a;
}
public Int32 closeCard()
{
//構造視窗的時候呼叫此函式
return FY4400_CloseDevice(hDevice);
}
public Int32 DI()
{
//構造視窗的時候呼叫此函式
return FY4400_DI(hDevice);
}
2.生成DLL
3.重點是不要複製原生DLL,FY4400.dll到python程式資料夾.,讓程式到系統裡自動查詢,否則操作不了硬體