В общем, пока в меру своих знаний))
DLL
// HookDll.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#include "HookDll.h"
#include<WinUser.h>
#include<WINDEF.h>
#include<Windows.h>
#pragma comment(lib,"User32.lib")
HHOOK hHook;
HANDLE hIns;
HANDLE hPipe;
HWND hWnd;
DWORD NumWrite;
MOUSEHOOKSTRUCT *hMouse;
LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam);
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
hIns=hModule;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
hHook = SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, (HINSTANCE)hIns, 0);
break;
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
UnhookWindowsHookEx(hHook);
CloseHandle(hPipe);
break;
}
return TRUE;
}
//Отправляем данные
int ReciveData(){
hPipe = CreateFile("\\\\.\\pipe\\MFPl", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if(hPipe==INVALID_HANDLE_VALUE){
return 0;
}
if(!WriteFile(hPipe,hWnd,sizeof(HWND),&NumWrite,NULL))return 0;
return 1;
}
LRESULT MouseProc(int nCode, WPARAM wParam, LPARAM lParam){
hMouse=(MOUSEHOOKSTRUCT*)lParam;
if(nCode<0)CallNextHookEx(hHook,nCode,wParam,lParam);
else{
//Отлавливаем нажатие
switch(nCode){
case HC_ACTION:
switch(wParam){
case WM_LBUTTONDOWN:
hWnd=hMouse->hwnd;
break;
case WM_NCLBUTTONDOWN:
hWnd=hMouse->hwnd;
break;
}
break;
case HC_NOREMOVE:
switch(wParam){
case WM_LBUTTONDOWN:
hWnd=hMouse->hwnd;
break;
case WM_NCLBUTTONDOWN:
hWnd=hMouse->hwnd;
break;
}
break;
}
}
return CallNextHookEx(hHook,nCode,wParam,lParam);
}
Написал програмку на С++ (позже она будет на VB) для теста
#include "stdafx.h"
#include<WinUser.h>
#include<iostream>
#include<Windows.h>
int main(int argc, char* argv[])
{
HMODULE hDLL;
HANDLE hPip;
hDLL=LoadLibrary("C:\\HookDLL.dll");
if(!hDLL){
std::cout<<"Error Load: "<<GetLastError()<<endl;
return 0;
}
hPip=CreateNamedPipe("\\\\.\\pipe\\MFPl", PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE|PIPE_WAIT,1,0,0,INFINITE,NULL);
if (hPip==INVALID_HANDLE_VALUE){
std::cout<<"Error pipe: "<<GetLastError()<<endl;
FreeLibrary(hDLL);
return 0;
}
\\Останавливается тут
if(!ConnectNamedPipe(hPip,NULL)){
std::cout<<"Error connect: "<<GetLastError()<<endl;
CloseHandle(hPip);
FreeLibrary(hDLL);
return 0;
}
HWND bufhWnd;
DWORD buf;
if(!ReadFile(hPip, &bufhWnd,sizeof(bufhWnd),&buf,NULL)){
std::cout<<"Error read: "<<GetLastError()<<endl;
CloseHandle(hPip);
FreeLibrary(hDLL);
return 0;
}
//Тут еще дисконект д.б.
std::cout<<bufhWnd<<endl;
CloseHandle(hPip);
FreeLibrary(hDLL);
return 0;
}
Предсказуемо зависает на ожидании. Есть подозрении, что до CreateFile в dll не доходит вобще. делал локальный хук на мышь, номера команд выдавались именно в nCode, а не в wParam. в общем это..помогите))
Она же на VB и с тем же результатом
''Форма
Private Sub Form_Load()
hDLL = LoadLibrary("C:\HookDLL.dll")
GetHandleF
End Sub
Private Sub Form_Unload(Cancel As Integer)
If hDLL <> 0 Then FreeLibrary (hDLL)
End Sub
' Модуль
Public Type SECURITY_ATTRIBUTES
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Const INFINITE = &HFFFF
Public Const PIPE_ACCESS_INBOUND = &H1
Public Const PIPE_TYPE_MESSAGE = &H4
Public Const PIPE_WAIT = &H0
Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Public Declare Function CreateNamedPipe Lib "kernel32" Alias "CreateNamedPipeA" (ByVal lpName As String, ByVal dwOpenMode As Long, ByVal dwPipeMode As Long, ByVal nMaxInstances As Long, ByVal nOutBufferSize As Long, ByVal nInBufferSize As Long, ByVal nDefaultTimeOut As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES) As Long
Public Declare Function CreateFile Lib "kernel32" Alias "CreateFileA" (ByVal lpFileName As String, ByVal dwDesiredAccess As Long, ByVal dwShareMode As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, ByVal dwCreationDisposition As Long, ByVal dwFlagsAndAttributes As Long, ByVal hTemplateFile As Long) As Long
Public Declare Function ReadFil Lib "kernel32" (ByVal hFile As Long, lpBuffer As Any, ByVal nNumberOfBytesToRead As Long, lpNumberOfBytesRead As Long, lpOverlapped As Long) As Long
Public Declare Function ConnectNamedPipe Lib "kernel32" (ByVal hNamedPipe As Long, lpOverlapped As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Public Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Public hDLL As Long, hMap As Long, lBuf As Long, NumRead As Long
Public res As Long
Public Function GetHandleF()
Dim sa As SECURITY_ATTRIBUTES
sa.nLength = Len(sa)
Dim hMap As Long, hwndHandle As Long, hh As Long
Dim str As String
str = "HandleMap"
hMap = CreateNamedPipe("\\.\pipe\MPLp", PIPE_ACCESS_INBOUND, PIPE_TYPE_MESSAGE Or PIPE_WAIT, 1, 0, 0, INFINITE, sa)
If hMap =-1 Then Exit Function
res = ConnectNamedPipe(hMap, ByVal 0)
If res = 0 Then Exit Function
res = ReadFil(hMap, lBuf, Len(lBuf), numred, 0)
End Function