|
|
When an application's form is minmum to the taskbar or hide,how to hook the messages of the form?I have developed an application that can hook a form's messages when the form on the desktop.But when the form is minmum to the taskbar or the form is hidden,the hook message application could not hook nothing of the form.In fact ,the messages of the form happen,such as a message that a disable button is changed to an enable button.Please help me!
Thanks alot!
|
|
|
|
|
Are you using SetWindowsHookEx API? Once you install the hook with the thread ID of the application, the hook remains even though it is hidden.
How did you determine it's not working?
|
|
|
|
|
I use SetWindowsHookEx API,but I use SubClass to hook the other application's message.When the hooked form is on the desktop,the hook application can hook all messages of the destination application.But when the form is minmum,the hook can't hook any messages of the destination application.
|
|
|
|
|
I've been battling the hooking and subclassing for the last 3 or 4 weeks. I finally get it to work. You haven't answered the question.
How did you determine it's not working when the form is minimized?
If the destination application is running in another process, you won't be able to step your code in the debugger. Instead, you should open up a new visual studio instance, attach to the process of the destination application, open the file you want to debug, add breakpoints.
Or, use the OutputDebugString API to print out all the window messages and Sysinternals DebugView to see the debug messages.
Are you hooking to WH_CBT or WH_CALLWNDPROC? Perhaps you should paste portion of your code here (especially the part where you call SetWindowsHookEx and SetWindowLong) if you really need help.
|
|
|
|
|
Thanks your reply.I use Delphi7 not VC.My source code is as following.
library MyHook;
uses
SysUtils,
windows,
Messages;
const
WM_UNSUBCLASS = WM_USER + 1001;
WM_NEWMESSAGE = WM_USER + 1002;
HOOK_EVENT_NAME = 'MyHook';
type
PMyDLLVar = ^TMyDLLVar;
TMyDLLVar = record
SubClass: Boolean;
HookWindow, SpyWindow: LongWORD;
hHook: LongWORD;
OldWndProc: pointer;
MsgHwnd: LongWORD;
Msg: TMessage;
end;
var
DLLData: PMyDLLVar;
function NewWndProc(hHwnd, Msg, wParam, lParam: LongWORD): Longint; stdcall;
begin
Result := 0;
if Msg = WM_UNSUBCLASS then
begin
SetWindowLong(DLLData^.HookWindow, GWL_WNDPROC, longint(DLLData^.OldWndProc));
exit;
end;
DLLData^.Msg.Msg := Msg;
DLLData^.Msg.WParam := wParam;
DLLData^.Msg.LParam := lParam;
DLLData^.MsgHwnd := hHwnd;
SendMessage(DLLData^.SpyWindow, WM_NEWMESSAGE,Msg,lParam);
Result := CallWindowProc(DLLData^.OldWndProc, hHwnd, Msg, wParam, lParam);
end;
Function HookProc(nCode:integer; wParam:WPARAM; lParam: LPARAM):LRESULT;stdcall;
var
hEvent: THandle;
begin
if not DllData^.SubClass then
begin
hEvent := OpenEvent(Synchronize, False, HOOK_EVENT_NAME);
if hEvent <> 0 then
begin
WaitForSingleObject(hEvent, INFINITE);
CloseHandle(hEvent);
end;
DLLData^.OldWndProc := pointer(GetWindowLong(DLLData^.HookWindow, GWL_WNDPROC));
SetWindowLong(DLLData^.HookWindow, GWL_WNDPROC, integer(@NewWndProc));
DLLData^.SubClass := True;
end;
Result := CallNextHookEx(DLLData^.hHook, nCode, wParam, lParam);
end;
function InstallHook(HWindow, SWindow: LongWORD):Boolean;stdcall;
var
ThreadID: LongWORD;
hEvent: THandle;
begin
Result := False;
DLLData^.hHook := 0;
DLLData^.HookWindow := HWindow;
DLLData^.SpyWindow := SWindow;
ThreadID := GetWindowThreadProcessId(HWindow, nil);
hEvent := CreateEvent(nil, True, False, HOOK_EVENT_NAME);
DLLData^.hHook := SetWindowsHookEx(WH_GETMESSAGE, @HookProc, Hinstance, ThreadID);
SetEvent(hEvent);
CloseHandle(hEvent);
if DLLData^.hHook > 0 then Result := True; {是否成功HOOK}
end;
procedure UnHook;stdcall;
begin
SendMessage(DLLData^.HookWindow, WM_UNSUBCLASS, 0, 0);
DLLData^.SubClass := False;
{卸载Hook}
UnhookWindowsHookEx(DLLData^.hHook);
end;
procedure MyDLLHandler(Reason: Integer);
var
FHandle: LongWORD;
begin
case Reason of
DLL_PROCESS_ATTACH:
begin
FHandle := CreateFileMapping($FFFFFFFF, nil, PAGE_READWRITE, 0, $ff, 'MYDLLDATA');
if FHandle = 0 then
if GetLastError = ERROR_ALREADY_EXISTS then
begin
FHandle := OpenFileMapping(FILE_MAP_ALL_ACCESS, False, 'MYDLLDATA');
if FHandle = 0 then Exit;
end else Exit;
DLLData := MapViewOfFile(FHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0);
if DLLData = nil then
CloseHandle(FHandle);
end;
DLL_PROCESS_DETACH:
if Assigned(DLLData) then
begin
UnmapViewOfFile(DLLData);
DLLData := nil;
end;
DLL_THREAD_ATTACH:;
DLL_THREAD_DETACH:;
end;
end;
{$R *.res}
exports
InstallHook, UnHook, HookProc;
begin
DLLProc := @MyDLLHandler;
MyDLLhandler(DLL_PROCESS_ATTACH);
end.
|
|
|
|
|
Sorry I don't know Delphi and it's hard to read the code without the indention. You can first try the equivalent methods of attaching to a process and printing debug messaegs.
You still haven't answered the question I asked in the first place. You might want to read it again carefully.
Perhaps by definition WH_GETMESSAGE hook is not meant to be called when the window is minimized? Or the minimized application indeed has no messages in the queue? Or did you unhook or "unsubclass" prematurely? I don't know, you will have to research these.
|
|
|
|
|
yeah,The Delphi code is not same as VC.But Could you show me some code ,I can read the VC or C++ code.Thanks!
|
|
|
|
|
Because I use a timer in the form,I write some code to set a button's enable property at interval.When the form is on desktop, my hook application can hook the message,but when the form minmum my hook application can't.
|
|
|
|
|
The key here is WH_GETMESSAGE. Are you sure the minimized application should be getting messages? I could be wrong. I thought the minimized application would be in the idle loop. I used Spy++ to monitor all standard window messages of a minimized application and I don't see any. Do you expect custom control messages? If not, I think what you are seeing is the correct behavior. I don't think Spy++ will miss out any.
I used WH_CALLWNDPROC and WH_CBT and they behave differently from WH_GETMESSAGE. I have the similar code to hook and subclass, I don't think that part of your code is wrong.
|
|
|
|
|
At first,Thanks your reply!As you said,When the application is minimized the Spy++ could not hook the messages!But the application is runing.Of course,the application must produce some messages to notify it's child-component such as button,listbox,prograss bar and so on.If the application is in the idle loop,how does it's child-component get the notify_message?Please help me!Thanks!
|
|
|
|
|
I don't think you understand what I was saying. You can use Spy++ to log ALL messages of the window and ALL its child windows before you minimize the application, then only you minimize it.
I'm outta ideas to suggest to you (that you actually try). Good luck!
|
|
|
|
|
Sorry I didn't see your post about the timer. Why don't you use Spy++ to monitor it and see if it gets any timer messages? If it doesn't, then your code behaves correctly. If it does, I suggest you to add a bunch of debug messages where the timer starts, ends, etc and analyze them in DebugView.
|
|
|
|
|
When an application's form is minmum to the taskbar or hide,how to hook the messages of the form?I have developed an application that can hook a form's messages when the form on the desktop.But when the form is minmum to the taskbar or the form is hidden,the hook message application could not hook nothing of the form.In fact ,the messages of the form happen,such as a message that a disable button is changed to an enable button.Please help me!
Thanks alot!
|
|
|
|
|
please help me.I have searched on google and msdn,but all ways can only hook the application with forms.But when the form is hidden,how to hook the messages when the states of the components on the form is changing?Please help me!
|
|
|
|
|
Hello, I am new to programming C++. Can anyone help me with this problem:
C2248: cannot access protected struct declared in class.
It occurs when I want to fill a struct in another class, which is declared private (and should stay private).
Do I have to develop a public function in this class which acceses my struct ?
I do not know how to proceed...
Thanks for your help
|
|
|
|
|
externalize the declaration of the struct type outside the class, or set it to public/protected within the class.
don't confuse, i talk about the struct type declaration, not the members of that type.
|
|
|
|
|
Thanks for your reply,
I already set it protected.
I do not want to access it from a derrived class, but from a really different class.
|
|
|
|
|
if you access it from a class that is not in the inheritance, you have to declare the struct public, or global.
|
|
|
|
|
ok thank you, i declared it public
|
|
|
|
|
Checker2003 wrote: i declared it public
what's wrong ? it's just a type... no one will access your datas from outside the class !!! no ?!
|
|
|
|
|
Instead of declaring it public, you could also add a constructor to structure to initialize its values, or add a method to the outer class to initialize it:
class OuterClass
{
public:
OuterClass() {}
~OuterClass() {}
private:
struct InnerClass
{
InnerClass() { _data = 0; }
int _data;
};
InnerClass _MyData;
};
class OuterClass
{
public:
OuterClass() {}
~OuterClass() {}
void Initialize() { _MyData._data = 0; }
private:
struct InnerClass
{
int _data;
};
InnerClass _MyData;
};
If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week
Zac
|
|
|
|
|
Hi,
Probably your problem looks like this:
typedef struct TESTSTRUCT {
double a;
}
class testA
{
public:
TESTSTRUCT st;
...
};
class testB: private testA
{
public:
void MyFunc(double val) {
st.a = val;
};
...
};
int main (...) {
testB c;
c.st.a = 1.1;
Myfunc(1.1);
}
So, everything you have to do is to create publicly available function to access private member of your classes.
Hope that helps a bit.
Best regards
|
|
|
|
|
Thanks for your reply,
I already thought about this, but I do not want to access it from a derrived class, but from a really different class.
My problem is, that then I have to give my function the struct as parameter, and this does not work, because the compiler doesn't understand the struct as identifier...
Thanks a lot
|
|
|
|
|
You can always post a bit of your code , which would be very helpful.
Regards
|
|
|
|
|