none
P/Invoke call makes StackUnbalanced error - what did i wrong? RRS feed

  • Question

  • Hey everyone

    I'm doing some open-sorce program for my college degree. I would like to connect Econometric program Gretl with Excel. But I have a problem with P/Invoke calls. Let me just give a snap shot of a code...

    Libgretl.dll

     

    PRN *gretl_print_new (PrnType ptype, int *err)
    {
     if (ptype == GRETL_PRINT_FILE) {
    	fprintf(stderr, "gretl_print_new: needs a filename\n");
    	return NULL;
     }
    
    #if PRN_DEBUG
     fprintf(stderr, "gretl_print_new() called, type = %d\n", ptype);
    #endif
    
     return real_gretl_print_new(ptype, NULL, NULL, NULL, err);
    }
    
    struct PRN_ {
     FILE *fp; /* file to which to print, or NULL */
     FILE *fpaux; /* auxiliary file (output redirected) */
     char *buf; /* buffer to which to print, or NULL */
     size_t bufsize; /* allocated size of buffer */
     size_t blen; /* string length of buffer */
     int savepos; /* saved position in stream or buffer */
     PrnFormat format; /* plain, TeX, RTF */
     int fixed; /* = 1 for fixed-size buffer */
     char delim; /* CSV field delimiter */
     char *fname; /* file name, or NULL */
    };
    

     

    ----------------------------------------------------
    And I am doing in C# project
    ----------------------------------------------------


     

    string gretlPath = "C:\\Program Files\\gretl\\libgretl.dll";
    
    [DllImport(gretlPath, CharSet = CharSet.Unicode)]
    public static extern IntPtr gretl_print_new(PrnType ptype, ref int err);
    
    
    
    //and then call in main method
    
    IntPtr p = gretl.gretl_print_new(gretl.PrnType.GRETL_PRINT_STDOUT, ref i);
     prn = (gretl.PRN)Marshal.PtrToStructure
    (p, typeof(gretl.PRN));
    

     

    Any help would be very appriciated.

    Have a nice day!

     




    • Edited by Saymon Rupar Wednesday, July 20, 2011 9:26 PM code design
    Wednesday, July 20, 2011 9:24 PM

Answers

All replies

  •  

    Have a look at this tool for Mashall issues. It should give you some ideas in this situation.

    http://blogs.msdn.com/b/clrteam/archive/2009/08/26/il-stub-diagnostic-tool.aspx

     

     


    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    • Marked as answer by Saymon Rupar Sunday, July 24, 2011 10:20 AM
    Thursday, July 21, 2011 11:22 AM
  • O, this looks like a great tool. I'll give it a try.

    Thank you very much.
    Thursday, July 21, 2011 1:11 PM
  • Also have a look at this MDA. It explains the reasons:

    http://msdn.microsoft.com/en-us/library/0htdy0k3.aspx

     

    The managed signature of the platform invoke call might not match the unmanaged signature of the method being called. This mismatch can be caused by the managed signature not declaring the correct number of parameters or not specifying the appropriate size for the parameters. The MDA can also activate because the calling convention, possibly specified by the DllImportAttribute attribute, does not match the unmanaged calling convention.


    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, July 21, 2011 2:40 PM
  • I have found out that, my problem was because I made wrong struct in C#. File in C is not the same as File in C#. The problem still persist but I know where to look.

    Thank you.
    Saturday, July 23, 2011 8:26 PM
  • Ok I have found out what my problem was. In DllImport I have to include CallingConvertion.Cdecl. Now it works perfectly.

    Thank you Riquel I'm

    Monday, July 25, 2011 7:18 AM
  • Hi Saymon,

    Welcome to discuss CLR dev issues in CLR forum. It is my pleasure.

     


    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Wednesday, July 27, 2011 2:43 AM