[转]Marshal.GetDelegateForFunctionPointer
2008-09-09 15:30
337 查看
原来是为了在游戏外挂中发送键盘鼠标消息,自己写个sendmessage或者是postmessage又比较麻烦。于是google了一下,发现现在很多脚本工具都有这个功能,其中按键精灵的一个叫361度的插件已经有这个的实现,还验证过了。为什么不拿来己用呢?
首先分析一下按键精灵插件的接口,发现:
public static void BGKM_Mouse(IntPtr hWnd, int code, int x, int y)
{
BGKM_ExecuteCommand(15, BuildParameters(hWnd, code, x, y), null);
}
public static WinApi.POINT BGKM_ScrToCli(IntPtr hWnd, int x, int y)
{
StringBuilder retVal = new StringBuilder();
BGKM_ExecuteCommand(16, BuildParameters(hWnd, x, y), retVal);
string[] tmp = retVal.ToString().Split('|');
return new WinApi.POINT(int.Parse(tmp[0]), int.Parse(tmp[1]));
}
}
}
好了,方便哇?这样一来,我们可以在.net上面实现动态加载和卸载Win32 dll. 具体思路就是:(还是代码来得方便)
public delegate int MsgBox(int hwnd,string msg,string cpp,int ok);
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
private static Delegate GetAddress(int dllModule, string functionname, Type t)
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
private void button1_Click(object sender, EventArgs e)
{
int huser32 = 0;
huser32 = LoadLibrary("user32.dll");
MsgBox mymsg = (MsgBox)GetAddress(huser32, "MessageBoxA", typeof(MsgBox));
mymsg(this.Handle.ToInt32(), txtmsg.Text, txttitle.Text , 64);
FreeLibrary(huser32);
}
首先分析一下按键精灵插件的接口,发现:
public static void BGKM_Mouse(IntPtr hWnd, int code, int x, int y)
{
BGKM_ExecuteCommand(15, BuildParameters(hWnd, code, x, y), null);
}
public static WinApi.POINT BGKM_ScrToCli(IntPtr hWnd, int x, int y)
{
StringBuilder retVal = new StringBuilder();
BGKM_ExecuteCommand(16, BuildParameters(hWnd, x, y), retVal);
string[] tmp = retVal.ToString().Split('|');
return new WinApi.POINT(int.Parse(tmp[0]), int.Parse(tmp[1]));
}
}
}
好了,方便哇?这样一来,我们可以在.net上面实现动态加载和卸载Win32 dll. 具体思路就是:(还是代码来得方便)
public delegate int MsgBox(int hwnd,string msg,string cpp,int ok);
[DllImport("Kernel32")]
public static extern int GetProcAddress(int handle, String funcname);
[DllImport("Kernel32")]
public static extern int LoadLibrary(String funcname);
[DllImport("Kernel32")]
public static extern int FreeLibrary(int handle);
private static Delegate GetAddress(int dllModule, string functionname, Type t)
{
int addr = GetProcAddress(dllModule, functionname);
if (addr == 0)
return null;
else
return Marshal.GetDelegateForFunctionPointer(new IntPtr(addr), t);
}
private void button1_Click(object sender, EventArgs e)
{
int huser32 = 0;
huser32 = LoadLibrary("user32.dll");
MsgBox mymsg = (MsgBox)GetAddress(huser32, "MessageBoxA", typeof(MsgBox));
mymsg(this.Handle.ToInt32(), txtmsg.Text, txttitle.Text , 64);
FreeLibrary(huser32);
}
相关文章推荐
- Marshal.GetDelegateForFunctionPointer
- Marshal.GetDelegateForFunctionPointer 方法和Marshal.GetFunctionPointerForDelegate 方法
- 巧用Marshal.GetDelegateForFunctionPointer--C#如何调用按键精灵插件dll
- .NET 2.0 中 GetDelegateForFunctionPointer 函数实现原理浅析 [草稿]
- WSAIoctl函数SIO_GET_EXTENSION_FUNCTION_POINTER参数的意义
- GetBuffer' : 2 overloads have no legal conversion for 'this' pointer
- JNI: Passing multiple parameters in the function signature for GetMethodID
- C++: member function pointer for SomeClass
- WSAIoctl 的SIO_GET_EXTENSION_FUNCTION_POINTER用法
- Question 7: Which of the following correctly identify benefits of the getline() member function for cin over the extraction oper
- c# - FunctionPointer/IntPtr and delegate conver...
- [转] 'function' : number overloads have no legal conversions for 'this' pointer
- C++'s Function pointer and C#'s delegate
- (原創) Function Pointer、Delegate和Function Object (C/C++) (template) (.NET) (C#)
- (原創) Function Pointer、Delegate和Function Object (C/C++) (template) (.NET) (C#)
- 由于错误传入Marshal.GetTypedObjectForIUnknown参数导致的应用程序crash
- Get ready for develop media player function in vs2010
- The method getTextContent() is undefined for the type Node方法报错
- C Pointer-to-Function 与 C++ Pointer-to-MemberFunction 的区别
- Mysql问题:can't get hostname for your address