#define DllExport __declspec( dllexport ) #define USER_MESSAGE_CREATE (WM_USER + 0x774) DllExport void CALLBACK sethook(void);//FocusProc); DllExport void CALLBACK freehook(void);
#include <windows.h> #include "main.h" HHOOK hHookWnd = 0; HINSTANCE hdll; BOOL WINAPI DllMain (HINSTANCE hInstance, DWORD reason, LPVOID lpReserved) { (void)lpReserved; if(reason==DLL_PROCESS_ATTACH){ hdll=hInstance; //DLLのハンドルを保存する } return TRUE; } LRESULT CALLBACK CBTProc(int nCode,WPARAM wParam,LPARAM lParam) { if(nCode == HCBT_CREATEWND){ PostMessage(FindWindow(NULL,TEXT("VB中学校")),USER_MESSAGE_CREATE,wParam,lParam); } return CallNextHookEx(NULL, nCode, wParam, lParam); //次のフックを呼ぶ } //フックを組み込む void CALLBACK sethook(void)//FocusProc focusProc) { hHookWnd = SetWindowsHookEx(WH_CBT,CBTProc, hdll, 0); } //フックを解除する void CALLBACK freehook(void) { UnhookWindowsHookEx(hHookWnd); }
LIBRARY "WindowHook"; EXPORTS sethook freehook
Imports System.Runtime.InteropServices Imports System.Text Public Class Form1 Private Const WM_USER As Integer = &H400 Private Const USER_MESSAGE_CREATE As Integer = WM_USER + &H774 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Me.Text = "VB中学校" sethook() End Sub Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message) Select Case m.Msg Case USER_MESSAGE_CREATE Dim newWndHandle As Integer = m.WParam Dim windowText As New StringBuilder(256) GetWindowText(newWndHandle, windowText, windowText.Capacity) Me.ListBox1.Items.Add(windowText.ToString()) End Select MyBase.WndProc(m) End Sub Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed freehook() End Sub <DllImport("WindowHook.dll")> _ Private Shared Sub sethook() End Sub <DllImport("WindowHook.dll")> _ Private Shared Sub freehook() End Sub <DllImport("User32")> _ Private Shared Function GetWindowText(ByVal hWnd As IntPtr, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Integer End Function End Class