使用编组使 C# 应用程序崩溃
Crash C# Application using Marshalling
我创建了一个使用 2 个 C++ Dll 的 C# 应用程序,第一个工作得很好,但我在使用第二个时遇到了一些问题。
我正在刷 CPU :
我的 dll:
[DllImport(@"st10flasher.dll")]
public static extern long SetCom(string PortName, long comspeed);
[DllImport(@"st10flasher.dll")]
public static extern long LoadFile(string FileName, ref long Fsize);
[DllImport(@"st10flasher.dll")]
public static extern long InitMonitor(string device);
[DllImport(@"st10flasher.dll")]
public static extern long ProgramFlash();
[DllImport(@"st10flasher.dll")]
public static extern long EraseFlash(long Block);
[DllImport(@"st10flasher.dll")]
public static extern long CloseCom();
[DllImport(@"st10flasher.dll")]
public static extern long BlockNBToErase(bool EraseBlockError);
它通常可以工作,但有时会崩溃。
-我检查了事件日志,发现异常代码:0xc0000409
- 它问我是否想用 VS 调试它然后我有这个堆栈:
错误消息不是英文的,我会尝试翻译:
非托管异常 0x71E2CF1B (clr.dll)。检测到的检测代码堆栈 cookie 超出堆栈缓冲区
会是什么?谢谢 !
正如您所问,我添加了 VB 声明:
Declare Function SetCom Lib "st10flasher.dll" (ByVal PortName$, ByVal comspeed As Long) As Long
Declare Function LoadFile Lib "st10flasher.dll" (ByVal FileName$, ByRef Fsize As Long) As Long
Declare Function InitMonitor Lib "st10flasher.dll" (ByVal device As Any) As Long
Declare Function ProgramFlash Lib "st10flasher.dll" () As Long
Declare Function GetError Lib "st10flasher.dll" (ByVal BufferForStatus As Any) As Long
Declare Function EraseFlash Lib "st10flasher.dll" (ByVal Block As Long) As Long
Declare Function CloseCom Lib "st10flasher.dll" () As Long
/******************************************** ****** 编辑 ******************************************* *******/
所以我更新了我的函数声明:
[DllImport(@"st10flasher.dll")]
public static extern uint SetCom([MarshalAs(UnmanagedType.LPStr)] string PortName, uint comspeed);
[DllImport(@"st10flasher.dll")]
public static extern uint LoadFile([MarshalAs(UnmanagedType.LPStr)] string FileName, ref uint Fsize);
[DllImport(@"st10flasher.dll")]
public static extern uint InitMonitor([MarshalAs(UnmanagedType.LPStr)] string device);
[DllImport(@"st10flasher.dll")]
public static extern uint ProgramFlash();
[DllImport(@"st10flasher.dll")]
public static extern uint EraseFlash(uint Block);
[DllImport(@"st10flasher.dll")]
public static extern uint CloseCom();
[DllImport(@"st10flasher.dll")]
public static extern uint BlockNBToErase(bool EraseBlockError);
我找到了 dll 的文档:
unsigned int SetCom(char *PortName, unsigned int ComSpeed)
unsigned int CloseCom(void)
unsigned int LoadFile(char *filename)
unsigned int InitMonitor(char *target)
unsigned int EraseFlash(unsigned int BlockMask)
unsigned int ProgramFlash(void)
我尝试使用此 post 更新我的原型:post Whosebug 但我仍然遇到同样的错误。我对 link 有什么误解吗?
/*********************************已解决************ ***************/
我通过创建另一个加载和卸载 st10flasher.dll
的 C++ DLL 解决了我的问题
[DllImport(@"st10flasher.dll")]
public static extern uint LoadFile([MarshalAs(UnmanagedType.LPStr)] string FileName, ref uint Fsize);
似乎有误:
unsigned int LoadFile(char *filename)
删除 ref uint Fsize
您没有为此签名:
[DllImport(@"st10flasher.dll")]
public static extern uint BlockNBToErase(bool EraseBlockError);
其他一切都应该是正确的。
我创建了一个使用 2 个 C++ Dll 的 C# 应用程序,第一个工作得很好,但我在使用第二个时遇到了一些问题。
我正在刷 CPU :
我的 dll:
[DllImport(@"st10flasher.dll")]
public static extern long SetCom(string PortName, long comspeed);
[DllImport(@"st10flasher.dll")]
public static extern long LoadFile(string FileName, ref long Fsize);
[DllImport(@"st10flasher.dll")]
public static extern long InitMonitor(string device);
[DllImport(@"st10flasher.dll")]
public static extern long ProgramFlash();
[DllImport(@"st10flasher.dll")]
public static extern long EraseFlash(long Block);
[DllImport(@"st10flasher.dll")]
public static extern long CloseCom();
[DllImport(@"st10flasher.dll")]
public static extern long BlockNBToErase(bool EraseBlockError);
它通常可以工作,但有时会崩溃。 -我检查了事件日志,发现异常代码:0xc0000409 - 它问我是否想用 VS 调试它然后我有这个堆栈:
错误消息不是英文的,我会尝试翻译: 非托管异常 0x71E2CF1B (clr.dll)。检测到的检测代码堆栈 cookie 超出堆栈缓冲区
会是什么?谢谢 !
正如您所问,我添加了 VB 声明:
Declare Function SetCom Lib "st10flasher.dll" (ByVal PortName$, ByVal comspeed As Long) As Long
Declare Function LoadFile Lib "st10flasher.dll" (ByVal FileName$, ByRef Fsize As Long) As Long
Declare Function InitMonitor Lib "st10flasher.dll" (ByVal device As Any) As Long
Declare Function ProgramFlash Lib "st10flasher.dll" () As Long
Declare Function GetError Lib "st10flasher.dll" (ByVal BufferForStatus As Any) As Long
Declare Function EraseFlash Lib "st10flasher.dll" (ByVal Block As Long) As Long
Declare Function CloseCom Lib "st10flasher.dll" () As Long
/******************************************** ****** 编辑 ******************************************* *******/
所以我更新了我的函数声明:
[DllImport(@"st10flasher.dll")]
public static extern uint SetCom([MarshalAs(UnmanagedType.LPStr)] string PortName, uint comspeed);
[DllImport(@"st10flasher.dll")]
public static extern uint LoadFile([MarshalAs(UnmanagedType.LPStr)] string FileName, ref uint Fsize);
[DllImport(@"st10flasher.dll")]
public static extern uint InitMonitor([MarshalAs(UnmanagedType.LPStr)] string device);
[DllImport(@"st10flasher.dll")]
public static extern uint ProgramFlash();
[DllImport(@"st10flasher.dll")]
public static extern uint EraseFlash(uint Block);
[DllImport(@"st10flasher.dll")]
public static extern uint CloseCom();
[DllImport(@"st10flasher.dll")]
public static extern uint BlockNBToErase(bool EraseBlockError);
我找到了 dll 的文档:
unsigned int SetCom(char *PortName, unsigned int ComSpeed)
unsigned int CloseCom(void)
unsigned int LoadFile(char *filename)
unsigned int InitMonitor(char *target)
unsigned int EraseFlash(unsigned int BlockMask)
unsigned int ProgramFlash(void)
我尝试使用此 post 更新我的原型:post Whosebug 但我仍然遇到同样的错误。我对 link 有什么误解吗?
/*********************************已解决************ ***************/
我通过创建另一个加载和卸载 st10flasher.dll
的 C++ DLL 解决了我的问题[DllImport(@"st10flasher.dll")]
public static extern uint LoadFile([MarshalAs(UnmanagedType.LPStr)] string FileName, ref uint Fsize);
似乎有误:
unsigned int LoadFile(char *filename)
删除 ref uint Fsize
您没有为此签名:
[DllImport(@"st10flasher.dll")]
public static extern uint BlockNBToErase(bool EraseBlockError);
其他一切都应该是正确的。