delegate int msgbox(IntPtr hwnd, string lptitle, string lptxt, int mb);//理解成函数原型声明吧.
IntPtr funptr = IntPtr.Zero;
private void button1_Click(object sender, EventArgs e)
{
funptr = new IntPtr(0x77d307ea);//这里我硬编码了MessageBoxA的函数地址.
msgbox msg = (msgbox)(Marshal.GetDelegateForFunctionPointer(funptr, typeof(msgbox)));
MessageBox.Show((msg == null).ToString());
msg.DynamicInvoke(IntPtr.Zero, "hell", " ", 4);
}
那拿到delegate后怎么得到指针值呢. So easy.
funptr= Marshal.GetFunctionPointerForDelegate(msg);//msg这个delegate 在上面已经拿到过了.
MessageBox.Show(funptr.ToString("x")); //在这里你会看到这里的值与在上面硬编码的值是一样的.
So All.