我在c#应用程序中使用c dll时遇到了一些麻烦。这给了我一个错误的功能是在DLL中像这样的头文件中定义:从C传递字符串到c 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);
的类型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 *'参数是否也不作为'string'(与'DDC_CreateFilePropertyHandle'相同)而不是'char []'传递?你的DLL函数也被声明为'stdcall',所以你必须改变你的pinvoke声明中的'cdecl'调用约定。 – shambulator
是的,它确实成功。返回码是好的。如果我只是调用'DDC_CreateFile'函数,它会正确地在文件系统中创建文件。我将'char'改为'string','cdec1'改为'stdcall',但我仍然得到错误。 – Devkev