2015-05-26 25 views
0

崩溃C#应用程序我创建了使用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); 

它通常工作,但有时它崩溃。 -I检查事件日志,发现异常代码:0xc0000409 - 它问我,如果我想用VS调试它,然后我有这个堆栈:英语

enter image description here

错误消息是不我会尝试翻译: 非托管异常0x71E2CF1B(clr.dll)。仪表代码堆栈饼干检测到超过堆栈缓冲区

什么会是什么?谢谢 !

至于你问我加了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 StackOverflow但我仍然有同样的错误。我误解了链接中的某些内容吗?

/*********解决************** **************/

我通过创建其它C++ DLL它加载和卸载st10flasher.dll解决我的问题

+0

一切...例如,你不使用'CallingConvention.Cdecl' ......都是你的PInvoke方法'Stdcl'?所有参数和返回值是否正确?如果没有.h文件,我们怎么说? – xanatos

+1

请注意,很少见过'long'用在C库中......在C中,对于Windows,'sizeof(long)== sizeof(int)',所以除非在C中它是一个'long long int' ora'int64_t',那么你应该使用'int' – xanatos

+0

我们需要看到双方。您只向我们展示了C#代码 - 向我们展示了C++声明。 – PaulMcKenzie

回答

0
[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); 

其他所有内容都应该是正确的。

+0

感谢您的答案我会删除它...即使我不使用一些我应该添加所有功能? – PyNico

+0

@PyNico你不用的东西不会崩溃你的程序:-)不,你应该只有你使用的PInvokes – xanatos