none
P/Invoke Interop Problem RRS feed

  • Question

  • Hi All,

    I've been working on a program to control a special type of scientific camera! I'm trying to use the camera's SDK which is written in unmanaged C++, and i'm trying to interoperate with the SDK using c#.

    Everything was going well till i got to work with a function which had some char* arguments, the unmanaged method signature is:

     

     

    int FLIListFirst(int *domain, char *filename, size_t fnlen, char *name, size_t namelen);
    

     

    I first tried to use the following managed signature:

     

     [DllImportAttribute(@strPath, EntryPoint = "FLIListFirst", CallingConvention = CallingConvention.StdCall)]

     public static extern int FLIListFirst(out int domain, out IntPtr filename,  [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U4)] uint fnlen, out IntPtr name,  [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.U4)] uint namelen);

     

    When i Marshalled the returned IntPtrs to String Using Marshal.PtrToStringAuto() the converted string looked strange, like the IntPtr was pointing to a wrong address.

    I then tried to change IntPtrs to StringBuilder, but the result was a StackOverflowException! The current managed signature i'm trying to get worked is:

     

     

     [DllImportAttribute(@strPath, EntryPoint = "FLIListFirst", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall)]

     public static extern int FLIListFirst(out int domain, [MarshalAs(UnmanagedType.LPWStr)] out StringBuilder filename, [System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.U4)] uint fnlen, [MarshalAs(UnmanagedType.LPWStr)] out StringBuilder name, [System.Runtime.InteropServices.MarshalAsAttribute(UnmanagedType.U4)] uint namelen);

    I should mention that i tried to pair StringBuilders whith LPStr too, i also once didn't pair them to any Unmanaged Type at all, but in all cases i got the EXCEPTION!

    Since i'm new to P/Invoke, it seems like i can't figure out what is going wrong!

     

    I appreciate your help, friends!

     





    Friday, May 6, 2011 6:28 AM

Answers

  • Ok, Problem Solved, I Removed the out keyword before StringBuilder definitions, and it's working!

     

    • Marked as answer by Milad_Soc Friday, May 6, 2011 7:11 AM
    Friday, May 6, 2011 7:10 AM