Question

I have some trouble using a c dll in a c# application. The function which gives me an error is defined in the header file of the dll like this:

int __stdcall DDC_CreateFilePropertyString (DDCFileHandle file, 
                                        const char *property,
                                        const char *value);

I added the following code in my class where I access the dll.

[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi)]
private static extern int DDC_CreateFilePropertyString(IntPtr file,
                         [MarshalAs(UnmanagedType.LPStr)]string property,
                         [MarshalAs(UnmanagedType.LPStr)]string value);

The Type DDCFileHandle is defined in the header file like this:

typedef struct _DDCFile DDCFile;
typedef DDCFile*    DDCFileHandle;

There is are no additional information about the _DDCFile struct in the header file (i don't have any other files from the library im using).

Before I'm calling the function DDC_CreateFilePropertyString() I call the following function to create a file and get the file handle.

[DllImport("nilibddc.dll", CallingConvention = CallingConvention.Cdecl, CharSet=CharSet.Ansi]
private static extern int DDC_CreateFile(char[] filePath,
                          char[] fileType,
                          char[] name,
                          char[] description,
                          char[] title,
                          char[] author,
                          ref IntPtr file);

The definition in the header file looks like this.

int __stdcall DDC_CreateFile (const char *filePath,
                          const char *fileType,
                          const char *name,
                          const char *description,
                          const char *title,
                          const char *author,
                          DDCFileHandle *file);

Now always when i call the function DDC_CreateFilePropertyString it returns me an error telling me that i have some wrong parameters passed. What am i doing wrong? The library I'm using is the TDMS C API from National instruments.

Thanks for your help.

Was it helpful?

Solution

Your p/invokes are a little off. You need to use CallingConvention.Stdcall, which is the default. And for the const char* parameters you should simply declare them to be string at the C# end.

The correct C# p/invoke for DDC_CreateFile is:

[DllImport("nilibddc.dll", CharSet=CharSet.Ansi]
private static extern int DDC_CreateFile(
    string filePath,
    string fileType,
    string name,
    string description,
    string title,
    string author,
    ref IntPtr file
);

And for DDC_CreateFilePropertyString you need this:

[DllImport("nilibddc.dll", CharSet=CharSet.Ansi)]
private static extern int DDC_CreateFilePropertyString(
    IntPtr file,
    string property,
    string value
);

If, after fixing your code, you still receive errors when calling these functions, then you are clearly using the library incorrectly. And that's beyond the scope of this question. Consult the documentation, and/or seek support from the library vendor.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top