Search code examples
c#c++dllmarshallingvhosts

Crash C# Application using Marshalling


I created a C# application which use 2 C++ Dll, the first one work very well, but i have some trouble with the second.

I'm flashing a CPU :

My 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);

It usually work but sometime it crash with. -I checked the event log and found the exception code : 0xc0000409 -It ask me if i want to debug it with VS then i have this stack :

enter image description here

The Error message isn't in english i'll try to translate : unmanaged exception 0x71E2CF1B (clr.dll). the instrumentation code stack cookie detected exceeding the stack buffer

What could it be ? thanks !

As you asked i added the VB declaration :

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

/************************************************** EDIT **************************************************/

So i updated my functions declaration like that :

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

and i found the dll's documentation :

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)

i tried to update my prototype using this post : post StackOverflow But i still have the same error. Did i misunderstood something from the link ?

/******************************SOLVED****************************/

I solved my problem by creating an other C++ DLL which load and unload the st10flasher.dll


Solution

  • [DllImport(@"st10flasher.dll")]
    public static extern uint LoadFile([MarshalAs(UnmanagedType.LPStr)] string FileName, ref uint Fsize);
    

    seems wrong:

    unsigned int LoadFile(char *filename)
    

    remove the ref uint Fsize

    You didn't have a signature for this:

    [DllImport(@"st10flasher.dll")]
    public static extern uint BlockNBToErase(bool EraseBlockError);
    

    Everything else should be correct.