using system;
using system.runtime.interopservices;
namespace fastcsharp
{
class class1
{
[stathread]
unsafe static void main(string[] args)
{
// cominvoke();
// helloworld();
// structinvoke();
// classinvode();
// pointerinvoke();
unsafemethod();
}
// 调用一个com组件
// tlbimp工具
public static void cominvoke()
{
//string filename = "c://windows//clock.avi";
string filename = @"c:/documents and settings/administrator/桌面/webapplication1/心中心密咒/dgmz.wma";
quartztypelib.filgraphmanager graphmanager = new quartztypelib.filgraphmanagerclass();
quartztypelib.imediacontrol mc = (quartztypelib.imediacontrol)graphmanager;
mc.renderfile(filename);
mc.run();
console.writeline("hello world");
console.readline();
}
public static void helloworld()
{
win32invoke.messagebox(0,"hello world","platform invoke sample",0);
}
// 传递结构变量
public static void structinvoke()
{
point p = new point();
p.x = 1;
p.y = 1;
rect r = new rect();
r.left = 0;
r.bottom = 2;
r.right = 2;
r.top = 0;
bool b = win32invoke.ptinrect(ref r,p);
system.console.writeline("point[1,1] is int rect[0,0,2,2] ?" +b );
}
// 传递一个对象
public static void classinvode()
{
mysystemtime t = new mysystemtime();
win32invoke.getsystemtime(t);
console.writeline("system time is "
+ t.wyear + "/"
+ t.wmonth + "/"
+ t.wday + " "
+ t.whour + ":"
+ t.wminute + ":"
+ t.wsecond
);
}
// 传送一个c#函数指针给dll函数
public static void pointerinvoke()
{
callback mycallback = new callback(class1.report);
win32invoke.enumwindows(mycallback,0);
}
public static bool report(int hwnd, int lparam)
{
console.writeline("window handle is" + hwnd);
return true;
}
// 指针函数
unsafe static void squareparam(int *p)
{
//unsafe mthod:takes pointer to int
*p *= *p;
}
// unsafe method : user address-of operator (&)
unsafe public static void unsafemethod()
{
int i = 5;
squareparam(&i);
console.writeline("5 * 5 == " + i );
p pt = new p();
pt.x = 5;
pt.y = 6;
//fixed 关键字是告诉c#资源回收器接下来不要对某过变量的地址进行运算,不要对此变量的地址有任何改动
//pin pt in place
fixed(int* pointer =&pt.x)
{
squareparam(pointer);
}
//pt now nupinned
console.writeline("pt.x == {0} pt.y == {1}",pt.x,pt.y);
}
}
public class win32invoke
{
[dllimport("user32.dll",charset = charset.auto)]
public static extern int messagebox(int hwnd,string text,string caption,uint type);
[dllimport("user32.dll")]
public static extern bool ptinrect(ref rect r, point p);
[dllimport("kernel32.dll")]
public static extern void getsystemtime(mysystemtime st);
[dllimport("user32.dll")]
public static extern int enumwindows(callback x, int y);
}
public struct point
{
public int x;
public int y;
}
public class p
{
public int x;
public int y;
}
[structlayout(layoutkind.explicit)]
public struct rect
{
[fieldoffset(0)] public int left;
[fieldoffset(4)] public int top;
[fieldoffset(8)] public int right;
[fieldoffset(12)] public int bottom;
}
[structlayout(layoutkind.sequential)]
public class mysystemtime
{
public ushort wyear;
public ushort wmonth;
public ushort wdayofweek;
public ushort wday;
public ushort whour;
public ushort wminute;
public ushort wsecond;
public ushort wmilliseconds;
}
// delegate
public delegate bool callback(int hwnd, int lparam);
}
新闻热点
疑难解答