tags:

views:

120

answers:

1

I am making a wrapper to read TDM and TDMS files but i have a problem

        [DllImport(lib, CharSet = CharSet.Auto)]
    static extern int DDC_OpenFileEx(
        [MarshalAs(UnmanagedType.LPStr)]
                        string filePath,
        [MarshalAs(UnmanagedType.LPStr)]
  string fileType,
                        int read_only,
                        ref long file);

works fine but

        [DllImport(lib, CharSet = CharSet.Auto, SetLastError = true)]
    static extern int DDC_GetNumChannelGroups(long file,
                         [MarshalAs(UnmanagedType.U4)]
                                ref int numChannelGroups); 


            int numGru = 0;

            errCode = ReadTDM.DDC_GetNumChannelGroups(file,ref numGru);
            System.Console.WriteLine("Error Code {0}  GetNumChannelGroups", errCode);

gives an error -6202, // An invalid argument was passed to the library. i have tried ref uint, uint * (unsafe), UIntPtr. The def from .h file

int __stdcall DDC_GetNumChannelGroups (DDCFileHandle file,unsigned int *numChannelGroups);

the second parametr is the problem. it seems that unsigned int* != uint.

Does anyone have an idea how to call this function form the dll?

http://forums.ni.com/ni/board/message?board.id=60&thread.id=11821

+1  A: 

It is the 1st argument that's declared wrong. That throws off the stack frame and prevents the unmanaged code from properly reading the pointer for the 2nd argument. "long" is 64-bits, DDCFileHandle is almost certainly a pointer, 32-bits on a 32-bit operating system.

Change the argument declaration to IntPtr. You'll also need to change the declaration of the function that returns that handle.

Hans Passant
THX a lot.I had a wrong .h file. this one is for matlabtypedef long DDCFileHandle;typedef long DDCChannelGroupHandle;typedef long DDCChannelHandle;i changed handles to int and it works
lukas