//.net中声明为结构
[ structlayout( layoutkind.sequential )]
public struct osversioninfo2
{
public int osversioninfosize;
public int majorversion;
public int minorversion;
public int buildnumber;
public int platformid;
[ marshalas( unmanagedtype.byvaltstr, sizeconst=128 )]
public string versionstring;
}
此例中用到mashalas特性,它用于描述字段、方法或参数的封送处理格式。用它作为参数前缀并指定目标需要的数据类型。例如,以下代码将两个参数作为数据类型长指针封送给 windows api 函数的字符串 (lpstr):
[marshalas(unmanagedtype.lpstr)]
string existingfile;
[marshalas(unmanagedtype.lpstr)]
string newfile;
注意结构作为参数时候,一般前面要加上ref修饰符,否则会出现错误:对象的引用没有指定对象的实例。
[ dllimport( "kernel32", entrypoint="getversionex" )]
public static extern bool getversionex2( ref osversioninfo2 osvi );
三、如何保证使用托管对象的平台调用成功?
如果在调用平台 invoke 后的任何位置都未引用托管对象,则垃圾回收器可能将完成该托管对象。这将释放资源并使句柄无效,从而导致平台invoke 调用失败。用 handleref 包装句柄可保证在平台 invoke 调用完成前,不对托管对象进行垃圾回收。
例如下面:
filestream fs = new filestream( "a.txt", filemode.open );
stringbuilder buffer = new stringbuilder( 5 );
int read = 0;
readfile(fs.handle, buffer, 5, out read, 0 ); //调用win api中的readfile函数
由于fs是托管对象,所以有可能在平台调用还未完成时候被垃圾回收站回收。将文件流的句柄用handleref包装后,就能避免被垃圾站回收:
[ dllimport( "kernel32.dll" )]
public static extern bool readfile(
handleref hndref,
stringbuilder buffer,
int numberofbytestoread,
out int numberofbytesread,
ref overlapped flag );
......
......
filestream fs = new filestream( "handleref.txt", filemode.open );
handleref hr = new handleref( fs, fs.handle );
stringbuilder buffer = new stringbuilder( 5 );
int read = 0;
// platform invoke will hold reference to handleref until call ends
readfile( hr, buffer, 5, out read, 0 );