locked
A call to PInvoke OpenSqlFilestream function has unbalanced the stack RRS feed

  • Question

  • I am using Filestream feature of SQL Server.
    When I try to access the following method :

    [DllImport("sqlncli10.dll", SetLastError = true, CharSet =
    CharSet.Unicode, CallingConvention = CallingConvention.Cdecl)]
            private static extern SafeFileHandle OpenSqlFilestream(string
    path, uint access, uint options, byte[] txnToken, uint txnTokenLength,
    Sql64 allocationSize);

    SafeFileHandle handle = OpenSqlFilestream(filePath, (uint)access, 0, txnToken, (uint)txnToken.Length, new Sql64(0));

    is the calling method.

    A call to PInvoke OpenSqlFilestream function  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.

    What could be the reason?

    I am using Visual Studio 2010

    Monday, September 17, 2012 2:24 PM

Answers

  • Here are som esuggestions

    1) Make sure all variables and points to variables are declared as static variables.  Variables within  a functon are on the execution stack and are not accessable from a dll.  the variables need to be in gloable memory space and declaring the variables outside any function and placing the keyword static will solve this problem

    2) If the function runs and the error occurs when the function exits then the problem is usally the size of the return parameter doesn't match between tthe function and the calling function


    jdweng

    • Proposed as answer by Mike Feng Wednesday, September 19, 2012 10:16 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:40 PM
    Monday, September 17, 2012 5:00 PM
  • Hello VKMT,

    1. Please check whether the OpenSqlFilestream() API is __stdcall or __cdecl.

    2, It might be __stdcall in which case it should be declared "CallingConvention = CallingConvention.StdCall".

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike Feng Wednesday, September 19, 2012 10:16 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:40 PM
    Wednesday, September 19, 2012 5:41 AM

All replies

  • Here are som esuggestions

    1) Make sure all variables and points to variables are declared as static variables.  Variables within  a functon are on the execution stack and are not accessable from a dll.  the variables need to be in gloable memory space and declaring the variables outside any function and placing the keyword static will solve this problem

    2) If the function runs and the error occurs when the function exits then the problem is usally the size of the return parameter doesn't match between tthe function and the calling function


    jdweng

    • Proposed as answer by Mike Feng Wednesday, September 19, 2012 10:16 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:40 PM
    Monday, September 17, 2012 5:00 PM
  • Hello VKMT,

    1. Please check whether the OpenSqlFilestream() API is __stdcall or __cdecl.

    2, It might be __stdcall in which case it should be declared "CallingConvention = CallingConvention.StdCall".

    - Bio.


    Please visit my blog : http://limbioliong.wordpress.com/

    • Proposed as answer by Mike Feng Wednesday, September 19, 2012 10:16 AM
    • Marked as answer by Mike Feng Monday, September 24, 2012 1:40 PM
    Wednesday, September 19, 2012 5:41 AM