我练习使用api32写了一个dll,在任意窗口点击鼠标左键可查看该窗口的类名,可顺利通过调试。但程序运行一会就出现‘未处理的“system.nullreferenceexception”类型的异常出现在 system.windows.forms.dll 中。其他信息: 未将对象引用设置到对象的实例。’谁能给分析一下,这是什么原因?
先将源码附上,请大家共同探讨。
//以下为dll类库源码
using system;
using system.runtime.interopservices;
using system.text;
namespace mhook
{
/// <summary>
/// class1 的摘要说明。
/// </summary>
public class mousehook
{
public mousehook()
{
//
// todo: 在此处添加构造函数逻辑
//
}
#region //********api32函数声明**************
// **********************************************************************
// win32: setwindowshookex()
[dllimport("user32.dll",charset=charset.auto,
callingconvention=callingconvention.stdcall)]
private static extern intptr setwindowshookex(hooktype code,
hookproc func,
intptr hinstance,
int threadid);
// win32: unhookwindowshookex()
[dllimport("user32.dll",charset=charset.auto,
callingconvention=callingconvention.stdcall)]
private static extern int unhookwindowshookex(intptr hhook);
// win32: callnexthookex()
[dllimport("user32.dll",charset=charset.auto,
callingconvention=callingconvention.stdcall)]
private static extern int callnexthookex(intptr hhook,
int ncode, int wparam, intptr lparam);
// win32: getmodulehandle()
[dllimport("kernel32.dll",charset=charset.auto,
callingconvention=callingconvention.stdcall)]
private static extern intptr getmodulehandle(string lpmodulename);
// win32: getforegroundwindow()
[dllimport( "user32.dll",charset=charset.auto,
callingconvention=callingconvention.stdcall)]
private static extern intptr getforegroundwindow();
// win32: getwindowthreadprocessid()
[dllimport("user32.dll",charset=charset.auto)]
private static extern int getwindowthreadprocessid (
intptr hwnd,
int lpdwprocessid
);
// win32: getcurrentthreadid
[dllimport("kernel32.dll", entrypoint="getcurrentthreadid")]
public static extern int getcurrentthreadid ();
// win32: attachthreadinput()
[dllimport("user32.dll",charset=charset.auto)]
private static extern int attachthreadinput (
int idattach,
int idattachto,
int fattach
);
// win32: getfocus()
[dllimport("user32.dll",charset=charset.auto)]
private static extern intptr getfocus ();
// win32: sendmessage()
[dllimport("user32.dll",charset=charset.auto)]
private static extern int sendmessage (
handleref hwnd,
int wmsg,
int wparam,
int lparam
);
[dllimport("user32.dll", entrypoint="sendmessage",charset=charset.auto)]
public static extern int sendmessagestring (
handleref hwnd,
int wmsg,
int wparam,
stringbuilder lparam
);
// win32: getwindowtext()
[dllimport("user32.dll",charset=charset.auto)]
private static extern int getwindowtext (
intptr hwnd,
stringbuilder lpstring,
int nmaxcount
);
// win32: getclassname()
[dllimport("user32.dll",charset=charset.auto)]
private static extern int getclassname (
intptr hwnd,
stringbuilder lpclassname,
int nmaxcount
);
#endregion
//enum hooktype
private enum hooktype : int
{
//wh_journalrecord = 0,
//wh_journalplayback = 1,