公共语言运行库(CLR)开发系列课程___学习笔记(1)

很久没学习.NET了, 工作中用得越来越少.已经快被LAMP架构所代替,之前用海量分词研究版时写个P/I方式调用其C++接口,过段时间(两三个月有一次)报内存出错,现在决定利用空余时间学习一下CLR.

1)非托管函数原型

BOOL WINAPI Beep(

__in DWORD dwFreq,

__in DWORD dwDuration

)

2)托管函数原型

[DlllmportAttribute("kernerl32.dll")]

public static extern bool Beep(

[In] uint dwFreq,

[In] uint dwDuration

);

static 如果不是static,就是成员函数,相对应,会有一个隐式指针,非托管只有两个参数,没有这个指针。

extern 表示此函数不是在CLR中,而是在别的中。

DWORD 是32位无符号整数,对应.NET中的uint

 

[DllImport("msvcrt.dll", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.Cdecl)]
public static extern int printf(String format, int i, double d);

CharSet C#中string使用Unicode,而C++则用的是ANSI(char,char*),w_char*一般是unicode?

CallingConvention 调用接口约定(如进栈顺序,谁负责清理堆栈等);支持:WinAPI,Cdecl,StdCall,ThisCall;

SetLastError 上次错误值,保存上次错误值,使用Marshal.GetLastWin32Error获得。

ThrowOnUnmappableChar 无法映射时抛出异常。

 

http://msdn.microsoft.com/zh-cn/library/aa686045.aspx

posted @ 2010-04-11 23:50  青歌绿酒醉眼猫  Views(235)  Comments(0)    收藏  举报