|
Hello,
I am sorry to disturb you too much but i am really confused about the system-wide hooks. I am now using a DLL to catch all keyboard events, as Michael Dunn said. But again it works only with my program. Here is the source code:
<br />
*********************SystemHook.h**********************<br />
#ifdef SYSTEMHOOK_EXPORTS<br />
#define SYSTEMHOOK_API __declspec(dllexport)<br />
#else<br />
#define SYSTEMHOOK_API __declspec(dllimport)<br />
#endif<br />
<br />
class SYSTEMHOOK_API CSystemHook {<br />
public:<br />
CSystemHook(void);<br />
};<br />
SYSTEMHOOK_API LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam);<br />
SYSTEMHOOK_API int WINAPI InstallHook (HWND hWnd, BOOL bCode );<br />
*******************************************************<br />
<br />
*********************SystemHook.cpp**********************<br />
#include "stdafx.h"<br />
#include "SystemHook.h"<br />
<br />
#define ID_CMD_KEYPRESSED WM_USER+200<br />
<br />
HWND ghWndMain;
<br />
static HHOOK hHook;
static BOOL bHookInstalled;
<br />
HINSTANCE ghDLLInst;
<br />
BOOL APIENTRY DllMain( HANDLE hModule, <br />
DWORD ul_reason_for_call, <br />
LPVOID lpReserved<br />
)<br />
{<br />
switch (ul_reason_for_call)<br />
{<br />
case DLL_PROCESS_ATTACH:<br />
case DLL_THREAD_ATTACH:<br />
case DLL_THREAD_DETACH:<br />
case DLL_PROCESS_DETACH:<br />
break;<br />
}<br />
ghDLLInst=(HINSTANCE)hModule;<br />
return TRUE;<br />
}<br />
<br />
CSystemHook::CSystemHook()<br />
{ <br />
return; <br />
}<br />
<br />
SYSTEMHOOK_API LRESULT CALLBACK KeyboardProc(<br />
int code,
WPARAM wParam,
LPARAM lParam)
{<br />
if ( code >= 0 ) <br />
{ <br />
if (GetKeyState(VK_SHIFT) < 0 && GetKeyState(VK_CONTROL) < 0) <br />
{ <br />
switch (wParam) <br />
{ <br />
case VK_F9:<br />
if (HIWORD(lParam) & 0x8000) <br />
PostMessage(ghWndMain, WM_COMMAND, ID_CMD_KEYPRESSED, 0L);<br />
return 1;<br />
} <br />
}<br />
}<br />
return (int)CallNextHookEx(hHook, code, wParam, lParam); <br />
}<br />
<br />
SYSTEMHOOK_API int WINAPI InstallHook (HWND hWnd, BOOL bCode )<br />
{ <br />
int nReturn = 1;<br />
ghWndMain = hWnd;
<br />
if (bCode == bHookInstalled) <br />
return 0; <br />
<br />
if (bCode) <br />
{ <br />
hHook=(HHOOK)SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,ghDLLInst,0); <br />
if (!hHook) <br />
return 0; <br />
bHookInstalled = TRUE; <br />
} <br />
else <br />
{ <br />
nReturn = UnhookWindowsHookEx(hHook); <br />
bHookInstalled = FALSE; <br />
} <br />
return nReturn; <br />
}<br />
*******************************************************<br />
Then in my main view:
<br />
void CWinSchedulerView::OnInitialUpdate() <br />
{<br />
CFormView::OnInitialUpdate();<br />
InstallHook(m_hWnd,TRUE);<br />
}<br />
void CWinSchedulerView::OnDestroy() <br />
{<br />
CFormView::OnDestroy();<br />
InstallHook(m_hWnd,FALSE);<br />
}<br />
<br />
BOOL CWinSchedulerView::OnCommand(WPARAM wParam, LPARAM lParam) <br />
{<br />
if(wParam==ID_CMD_KEYPRESSED)<br />
MessageBox("Ctrl+Shift+F9");<br />
<br />
return CFormView::OnCommand(wParam, lParam);<br />
}<br />
This works only on my window.If i press ctrl+shift+f9 on another window, it does not work.
Thanks for any helps in advance.
Mustafa Demirhan
|
|
|
|
|
At least shared data should be in a shared/readable/writeable section. Following a working code sample.
|
|
|
|
|
I'm searching around, for quite a while, for a function, that would be able to execute PE executable once it was loaded into memory (for example with CMemFile).
I would be very gratefull if someone would write article or give away some source code (Matt Pietrek where r u...?
|
|
|
|
|
May be no one has written such a such except what is in the OS, or may be it's too critical to their bussiness
to share.
Here are some of what I know:
1) You can't use memory-mapped file as a basically for PE execution, because PE file is normally stored using
512-byte block size, when in-memory image uses 4096-block size.
2) Simulate what LoadLibrary will do for a PE file.
3) Shut-down or make current process to be minimal and jump to the entry point of loaded EXE.
4) Debug in assembly code.
|
|
|
|
|
Since my post I have found some samples around this theme in virus community, the only problem is that all c++ sources are so hardly crippled that correcting them would mean to study thema so well i wouldn need that source any more This life sucks
|
|
|
|
|
How can I get a standard yesno messagebox to display in Spanish (Si/No) ?
I'm using Visual C++ Version 1.52 16-bit which doesn't support the messageboxex function.
|
|
|
|
|
i downloaded the program aimster, and has an interesting feature, as it interacts with AOL IM, it gets the buddy list, and if u click on it, it opens a AOL im window. It basically sends messages to the AOL IM application. How does it do this, does it use, FindWindow, then SendMessage, and sendtext to the AOL instant messanger app? How does it know AOL IM is running. Does anyone know how this works.
|
|
|
|
|
You've got some reading to do
Sending messages to another Window isn't a problem, you just have to figure out which messages you need to send. I don't know aimster, and I've not dug into the AOL's IM, but it should be pretty straightforward to figure out if a) AOL IM offers an API to automate the use of its messaging (which I think would be a good move for them), or b) which message ID's trigger which functions.
Problem with mapping to undocumented stuff is that it can change, which makes for unstable software. What I'm getting at here is that you might be able to 'hack' AOL IM for the current version, but there's no guarantee that all versions of AOL IM use the same messages, or that they will use the same messages in the future.
In any event, there are a few ways to enumerate the applications running on your system, depending on whether you are interested in Windowed and/or Windowless applications. A good place to start however is with the EnumWindows() Win32 API function, and you'll see links to Process and Thread functions that preform similar functions.
|
|
|
|
|
I am in trouble with hooks. I have entered the following code to my programs main view's .cpp file:
<br />
HHOOK m_hhk;<br />
static LRESULT CALLBACK KeyboardProc(int code,WPARAM wParam,LPARAM lParam);<br />
<br />
void CWinSchedulerView::OnInitialUpdate() <br />
{<br />
CFormView::OnInitialUpdate();<br />
m_hhk = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,NULL,GetCurrentThreadId());<br />
}<br />
<br />
static LRESULT CALLBACK KeyboardProc(<br />
int code,
WPARAM wParam,
LPARAM lParam)
{<br />
if (code < 0 || code != HC_ACTION)<br />
return CallNextHookEx(m_hhk, code, wParam, lParam);<br />
if (wParam == VK_F1)<br />
{<br />
AfxMessageBox("F1");<br />
}<br />
return CallNextHookEx(m_hhk, code, wParam, lParam);<br />
}<br />
<br />
void CWinSchedulerView::OnDestroy() <br />
{<br />
CFormView::OnDestroy();<br />
<br />
UnhookWindowsHookEx(m_hhk); <br />
}<br />
Well, this works well if i press F1 wheh the active window is my window. However, when i press F1 on another window it does not work. If someone tell me the problem, i will be very glad.
Thanks in advance for any helps
Mustafa Demirhan
|
|
|
|
|
Becuase you're only installing the hook for your app's thread (see the last param of SetWindowsHookEx() ).
If you want a system-wide hook, pass 0 for the thread ID. However, for system-wide hooks, the hook proc must be in a DLL. MSDN goes into depth on this - check out the docs.
|
|
|
|
|
I looked at your question in the CodeGuru web site and I tried to look at it closely. I saw no evidence there that you wanted a system-wide hook, sorry. If I had known you are trying to do a system-wide hook I would have said something there.
For best results, though, you will need to spend more time reading.
|
|
|
|
|
I guess it was not the CodeGuru web site; I guess it was your other question in this web site.
|
|
|
|
|
i have no idea but i wanna create a site with frames i used dreamweaver and like 10 other html programs, no luck yet i have no idea what to do
|
|
|
|
|
see http://planetwebdesign.com/frames.htm for samples
you can also do a internet search for
html frames
many examples should come up
|
|
|
|
|
I have tried to add a cstatic control to display a bitmap.
the background is made transparent (by using the grey color). it really appears to be transparent in my computer. But when i deploy the program in another person's computer, the background becomes not transaprent (becomes darker). Why is that?
Thanks all!
|
|
|
|
|
Hello,
I am trying to use hooks to get all keyboard events but i could not achieve to compile my code. Here is the code:
<br />
LRESULT CALLBACK CWinSchedulerView::KeyboardProc(<br />
int code,
WPARAM wParam,
LPARAM lParam)
{<br />
if (code < 0 || code != HC_ACTION)<br />
return CallNextHookEx(m_hhk, code, wParam, lParam);<br />
if (wParam == VK_F1)<br />
{<br />
MessageBox("F1 is pressed!");<br />
}<br />
return CallNextHookEx(m_hhk, code, wParam, lParam);<br />
}<br />
<br />
void CWinSchedulerView::OnDestroy() <br />
{<br />
CFormView::OnDestroy();<br />
UnhookWindowsHookEx(m_hhk); <br />
}<br />
This code is compiled with no errors. However at InitDialog() the following code gives error:
<br />
HOOKPROC lpfn = MakeProcInstance(KeyboardProc, hInst);<br />
m_hhk = SetWindowsHookEx(WH_KEYBOARD,lpfn,NULL, GetCurrentThreadId());<br />
I know that MakeProcInstance is obselete in Win32. So I tried the following:
<br />
m_hhk = SetWindowsHookEx(WH_KEYBOARD,(HOOKPROC)KeyboardProc,NULL, GetCurrentThreadId());<br />
But, this cannot be compiled to. The error is same:
<br />
D:\Visual C++ Projects\WinScheduler\WinSchedulerView.cpp(216) : error C2440: 'initializing' : cannot convert from '' to 'long (__stdcall *)(int,unsigned int,long)'<br />
None of the functions with this name in scope match the target type<br />
I think i cannot give the function pointer correctly. How can i correct this error?
Thanks in advance for any help.
Mustafa Demirhan
|
|
|
|
|
Is KeyboardProc a static member of CWinSchedulerView? It must be.
|
|
|
|
|
I got this error message too. Instead of using the KeyboardProc function as a member
function of your CWinSchedulerView class, you should define it as:
static LRESULT CALLBACK KeyboardProc (int code, WPARAM wParam, LPARAM lParam);
-------------------------------------------
Marcelo Guerra
|
|
|
|
|
Using VC++ 6 ?
How can i create user interface like MS Money.
Thanks in advance
Pankaj
|
|
|
|
|
I only used MS Money 2000 for a few days a while back, but I seem to remember it using an interface that looked very much like it was written in HTML. InstallSheild for windows installer(r) uses the same technique.
You could use the CHtmlView (or a WebBrowser based class of your own) to do this, and intercept all OnBeforeNavigate2(...) calls to see if you need to display an application-specific task (i.e. exit the program, or show a dialog) not available in HTML.
You can look through the MSJ archives (http://msdn.microsoft.com/msj) for an article by Paul DiLascia, that showed you how to intercept calls using the CHTMLView to close an about dialog. Try searching for 'DiLascia CHtmlView about' and you'll get it.
You could then use framesets, exactly the same way as you would with a web site, to create your UI, with Javascript menus and such like. Then just assign commands such as 'app:createnewdocument' to 'a href=' tags (see the article mentioned above to understand this) to create a new document in a certain frame.
You could use ActiveX controls if the standard html controls don't provide what you want.
This is just one suggestion.
Yours,
David Wulff
|
|
|
|
|
OK, next question. I know how to subclass a window using SetWindowLong. No prob. However, the only way that I can find to do it, is to define a function outside of any class. Is there a way to use a function belonging to a class as the callback procedure, and if so, how do I declare it? Would it be:
m_OldWndProc=(WNDPROC)::SetWindowLong(m_ControlToSubclass.GetSafeHwnd(), GWL_WNDPROC, (LONG)CMyClass::NewWndProc);
Or, would it be
m_OldWndProc=(WNDPROC)::SetWindowLong(m_ControlToSubclass.GetSafeHwnd(), GWL_WNDPROC, (LONG)NewWndProc);
And, how do I properly cast the function point to the LONG that SetWindowLong requires for its 3rd parameter?
Also, what's the proper method of UN subclassing the window?
Thanks in advance for any help.
|
|
|
|
|
I infer from your GetSafeHwnd() calls that you're using MFC. If so, there's an easier way to do this - CWnd::SubclassWindow() .
But to answer your questions, you can't use CMyClass::NewWndProc() as the callback because that function has this as the first parameter (even though you don't write the function that way) which makes it not match the required prototype. If you make that member function static it will work, because static methods do not have a this pointer.
Also, to undo the subclassing, call SetWindowLong() again and set the wnd proc to m_OldWndProc .
|
|
|
|
|
Thanks. That makes sense. The only problem is that now I can't seem to access any members of the class, private, protected, or public (I assume that it's due to the lack of the 'this' pointer). I've never really played with CWnd::SubclassWindow, but I'll give it a go. Thanks.
|
|
|
|
|
Right, static functions can only access static members and other static functions of the class.
|
|
|
|
|
That is what the documentation says, but this is an example where the documentation is totally misleading. Just as a static function can access members of other classes, a static function is entirely capable of accessing members of it's own class.
Now I can hear you saying that I am quack or something like that, but I do it all the time. The thing that most people just do not realize is that you simply must provide to the static function (that does not have the "this" pointer) the pointer that it does not have. Very simple, yet it seems that many people overlook this and take the documentation too literally.
The trick of course is passing the pointer. Most callbacks and such have a parameter and the pointer can be passed as a parameter or part of a parameter. In the case of the situation being discussed, the solution is probably not so easy. It might be possible to use a solution simialr to MFC's solution to the equivalent problem. It is possible to create a mapping from the window handle to the pointer for the class. Right?
|
|
|
|
|