首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 定义dll中的函数解决办法

2012-02-07 
C# 定义dll中的函数在C++中的2个函数为HANDLEOpenFile(char*FileName,int*Num)BOOLPlay(HANDLEHandle,HWND

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 );

[解决办法]
好久不见,(愚翁)

热点排行