locked
A call to PInvoke function unbalanced the stack

    Question

  • Hi,

    I'm coding a program which uses some dll files. When I try to run functions from the dll I get the next exception:

    A call to PInvoke function '...SN_SetSource' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

    I use the next code block to open the dll:

    [System.Runtime.InteropServices.

    DllImport("mid2250")]

    public static extern Int32 SN_SetSource(Int32 Source, Int32 board);

     

    It is important to say that the same code works when I use .Net Framework 3.5.

    I tried to replace the 'Int32' type with 'Int16' type, 'int' type, 'long' type, 'byte' type and more, but none worked!!

    In additon, I also tried to cast the type I send to the SN_SetSource function to any of the above types, i.e:

    mid2250

    .SN_SetSource((Int32)0, (Int32)0);

    mid2250.SN_SetSource((Int16)0, (Int16)0);

    and so on...

     

    What is the problem and how can I fix it?

     

     

    Monday, March 22, 2010 10:10 AM

All replies

  • First find out how the SN_SetSource native signature (from C++ .h file) looks like (and post it here). Verify parameters, calling convention, etc.

    -Karel

    Monday, March 22, 2010 4:30 PM
  • Hi,

    There is no .h file, Instead there is an SDK. In the SDK the SN_SetSource function is declared as follows:

     

    "MID2250_API int SN_SetSource

    (MID2250_SOURCE input, int board);

    Selects between composite and S-video inputs.

    Parameters

    input

        enumerated input MID2250_SOURCE (see mid2250types.h). For model 2250 the allowed

        values are MID2250_SOURCE_COMPOSITE_0 and MID2250_SOURCE_SVIDEO_0.

    board

        board number in the system (use 0 for single board setups).

    Returns

    0 on success, negative value if error (see mid2250types.h for error codes list).  "

     

    MID2250_SOURCE is defined like this:

    public

    enum MID2250_SOURCE : int

    {

    MID2250_SOURCE_COMPOSITE_0 = 0X0,

    MID2250_SOURCE_SVIDEO_0 = 0X1,

    MID2250_SOURCE_NUM = 0X2,

    }

     

    And last thig, the returned value (MID2250_API) is defined as follows:

     

    #ifdef

    ISDLLBUILD

     

    #define MID2250_API extern "C" __declspec(dllexport)

    #else

    #ifdef

    __cplusplus

     

    #define MID2250_API extern "C" __declspec(dllimport)

    #else

     

    #define MID2250_API __declspec(dllimport)

    #endif

     

     

     

    Tuesday, March 23, 2010 6:25 AM
  • Apply all defines, etc. to get a full method declaration. Verify that it is correct - take it out into your own separate DLL, implement it (print all args and return a known value) and then use it the same way from managed code.
    Another thing you can try to create a C++ DLL in the middle and call the function thrugh that. That way you can verify if it is your PInvoke issue, or SN_SetSource issue.

    If there is no .h file, how can you call the function from C++? Maybe the SDK documentation is wrong and the actual signature is different.

    -Karel

    Tuesday, April 06, 2010 6:43 PM