我有这个类用c ++访问DLL我觉得我没有访问权限。只是一个简单的文档:
class SB6
{
[UnmanagedFunctionPointer(CallingConvention.ThisCall)]
public delegate void USBMESSAGEFUNCTION(byte Cmd, IntPtr pData, int DataLen);
public static USBMESSAGEFUNCTION funct;
[DllImport("USBENGINE.DLL")]
extern public static bool InitialEngine();
[DllImport("USBENGINE.DLL")]
extern public static eUSB_Open StartUsbEngine();
[DllImport("USBENGINE.DLL", CallingConvention = CallingConvention.Cdecl)]
unsafe extern public static void SetMessageCallbackFunction(USBMESSAGEFUNCTION pFunc);
[DllImport("USBENGINE.DLL")]
extern public static void StopUsbEngine();
[DllImport("USBENGINE.DLL")]
extern public static void UnInitialEngine();
}
public enum eUSB_Open
{
eOpen_Success = 0x00,//Successfully opened
eOpen_Opened,//Already in open state
eOpen_No_Device,//No USB device
eOpen_Open_Failed,//Open failed
eOpen_No_Initial,//Dynamic library not initialized
};
public enum eUsbMsg
{
USB_MSG_CONNECT = 0x00,//USBEngien connected to currency-counting machine
USB_MSG_DISCONNECT,//USBEngine disconnected from currency-counting machine
USB_MSG_START,//Currency counting started (motor started)
USB_MSG_DATA,//Currency-counting machine uploading currency information data
USB_MSG_END,//Currency counting completed (motor stopped)
};
public struct SN_Data_Struct
{
public UInt16 Date;
public UInt16 Time;
public UInt16 tfFlag;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public UInt16[] ErrorCode;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public UInt16[] MoneyFlag;
public UInt16 Ver;
public UInt16 Value;
public UInt16 CharNum;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)]
public UInt16[] SNo;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 24)]
public UInt16[] MachineSNo;
public TImageSNo ImageSNo;
};
public struct TImageSNo
{
public UInt16 col;
public UInt16 row;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2176+18+256)]
public UInt16[] data;
};
我在主表单上有这个代码:
public Form1()
{
InitializeComponent();
bool status = SB6.InitialEngine();
}
static void onMessage(byte Cmd, IntPtr pData, int DataLen)
{
String cmd = Cmd.ToString();
String serial = "";
String response = "";
String currency = "";
byte[] res = new byte[DataLen];
if (cmd == "0")
response = eUsbMsg.USB_MSG_CONNECT.ToString();
else if (cmd == "1")
response = eUsbMsg.USB_MSG_DISCONNECT.ToString();
else if (cmd == "2")
response = eUsbMsg.USB_MSG_START.ToString();
else if (cmd == "3")
{
response = eUsbMsg.USB_MSG_DATA.ToString();
SN_Data_Struct output = (SN_Data_Struct)Marshal.PtrToStructure(pData, typeof(SN_Data_Struct));
for (int x = 0; x < output.MoneyFlag.Length - 1; x++)
{
currency += (char)output.MoneyFlag[x];
}
for (int x = 0; x < output.SNo.Length; x++)
{
serial += (char)output.SNo[x];
}
}
else if (cmd == "4")
response = eUsbMsg.USB_MSG_END.ToString();
}
private void btnConnect_Click(object sender, EventArgs e)
{
eUSB_Open result = SB6.StartUsbEngine();
if (result.ToString() == "eOpen_Success")
{
SB6.funct = new SB6.USBMESSAGEFUNCTION(onMessage);
GC.KeepAlive(SB6.funct);
SB6.SetMessageCallbackFunction(SB6.funct);
MessageBox.Show("Successfully opened");
}
else
MessageBox.Show("Failed to open");
}
}
我的问题是,当SetMessageCallbackFunction
被触发时,我的主表单中的onMessage
函数会执行。但执行onMessage
后,应用程序自行退出。我没有像application.exit
这样的代码来退出应用程序或其他任何内容。
程序退出的原因可能是什么?请帮帮我。