您好,我在使用C#調(diào)用WCHBLEDLL庫,調(diào)用打開設(shè)備接口是得不到響應(yīng),MFC Demo可以成功。在此尋求幫助,謝謝
DLL 調(diào)用:
?????????????///? ????????///?連接狀態(tài)委托 ????????///? ????????///?設(shè)備句柄 ????????///?連接狀態(tài),0-斷開、1-連接 ????????[UnmanagedFunctionPointer(CallingConvention.StdCall)] ????????public?delegate?void?FunDevConnChangeCallBack(IntPtr?hDev,?byte?ConnectStatus); ????????///? ????????///?打開設(shè)備 ????????///? ????????///?設(shè)備路徑,可通過掃描設(shè)備獲取 ????????///?上傳設(shè)備狀態(tài)的回調(diào)函數(shù) ????????///? ????????[DllImport("WCHBLEDLL.dll",?EntryPoint?=?"WCHBLEOpenDevice",?SetLastError?=?true,?CharSet?=?CharSet.Auto,?ExactSpelling?=?false,?CallingConvention?=?CallingConvention.StdCall)] ????????public?static?extern?IntPtr?WCHBLEOpenDevice(string?DevID,?FunDevConnChangeCallBack?pFunDevConnChange);
邏輯代碼:
public?void?BLEConnect(IntPtr?hDev,?byte?connectState) ????????{ ????????????retInptr?=?hDev; ????????????string?hDevStr,?stateStr; ????????????hDevStr?=?Marshal.PtrToStringAnsi(hDev); ????????????stateStr?=?connectState.ToString(); ????????????Console.WriteLine("設(shè)備句柄:"?+?Marshal.PtrToStringAnsi(hDev)); ????????????Console.WriteLine("連接狀態(tài):"?+?connectState); ????????????if?(stateStr?==?"1") ????????????????MessageBox.Show("設(shè)備句柄:"?+?hDevStr?+?",連接狀態(tài):"?+?connectState); ????????????else ????????????????MessageBox.Show("設(shè)備句柄:"?+?hDevStr?+?",斷開狀態(tài):"?+?connectState); ????????????return; ????????} ????????[ThreadStatic] ????????private?static?BLE.FunDevConnChangeCallBack?ConnChangeCallBack; ????????///? ????????///?設(shè)備句柄 ????????///? ????????IntPtr?retInptr; ????????//連接藍(lán)牙 ????????private?void?button3_Click(object?sender,?EventArgs?e) ????????{ ????????????retInptr?=?Marshal.AllocHGlobal(100); ????????????try ????????????{ ????????????????//設(shè)備路徑,可通過掃描設(shè)備獲取 ????????????????string?devInfo?=?listBox1.SelectedItem.ToString().Split(',')[1]; ????????????????ConnChangeCallBack?=?new?BLE.FunDevConnChangeCallBack(BLEConnect); ??????????? ????????????????retInptr?=?BLE.WCHBLEOpenDevice(devInfo,?ConnChangeCallBack); ????????????} ????????????catch?(Exception?ex) ????????????{ ????????????????MessageBox.Show(ex.Message); ????????????} ????????}
不知道是否有C# Demo,或者可以給予幫助,謝謝