日期:2014-05-19  浏览次数:20789 次

C# 定义dll中的函数
在C++中的2个函数为
HANDLE     OpenFile(char   *FileName,   int*   Num)
BOOL     Play(HANDLE   Handle,   HWND   W1,   HWND   W2,   HWND   W3,   HWND   W4   )
如何定义在C#中?

[DllImport( "text.dll ",   EntryPoint= "OpenFile ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
public   static   extern   int   OpenFile(ref   char     FileName,ref   short   iWndNum);


[DllImport(( "text.dll ",   EntryPoint= "Play ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
public   static   extern   bool   Play(Int32   DecodeHandle,System.IntPtr   Wnd1,   System.IntPtr   Wnd2,   System.IntPtr   Wnd3,   System.IntPtr   Wnd4   );
不知道对不对,请高人们指点!
谢谢!!!!!!!

------解决方案--------------------
对的,在C#中,句柄一般都是用IntPtr,用unsigned int也行.建议用IntPtr
------解决方案--------------------
主要看你的iWndNum参数,到底是单个传出参数,还是相应的数组参数,
如果是前者,可以如下:
[DllImport( "text.dll ", EntryPoint= "OpenFile ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
public static extern IntPtr OpenFile( StringBuilder FileName,ref short iWndNum);
calling:
StringBuilder sFileName = new StringBuilder(256);//init buffer
IntPtr hFile = OpenFile( sFileName, ref iWndNum );

否则,如下
[DllImport( "text.dll ", EntryPoint= "OpenFile ",CharSet=CharSet.Auto,CallingConvention=CallingConvention.Winapi)]
public static extern IntPtr OpenFile( StringBuilder FileName, short[] iWndNum);
calling:
StringBuilder sFileName = new StringBuilder(256);//init buffer
short[] iWndNum = new short[...];
IntPtr hFile = OpenFile( sFileName, iWndNum );

------解决方案--------------------
好久不见,(愚翁)