我在使用c#应用程序中的c dll时遇到了一些麻烦。导致错误的函数在dll的头文件中被定义如下:
int __stdcall DDC_CreateFilePropertyString (DDCFileHandle file,
const char *property,
const char *value);
我在访问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);
Type DDCFileHandle在头文件中定义如下:
typedef struct _DDCFile DDCFile;
typedef DDCFile* DDCFileHandle;
头文件中没有有关_DDCFile结构的其他信息(我没有使用库的其他文件)。
在调用函数DDC_CreateFilePropertyString()之前,我调用以下函数创建文件并获取文件句柄。
[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);
头文件中的定义如下所示。
int __stdcall DDC_CreateFile (const char *filePath,
const char *fileType,
const char *name,
const char *description,
const char *title,
const char *author,
DDCFileHandle *file);
现在每次我调用函数DDC_CreateFilePropertyString时,它都会返回一个错误,告诉我传递了一些错误的参数。我做错了什么?我使用的库是National Instruments的TDMS C API。
谢谢您的帮助。
DDC_CreateFile
是否成功?你怎么知道?它的const char *
参数不应该像DDC_CreateFilePropertyHandle
一样作为string
而不是char[]
传递吗?由于您的DLL函数也声明为stdcall
,因此您将不得不在pinvoke声明中更改cdecl
调用约定。 - anton.burger