HWND hWindowHandle = FindWindow(NULL, L"Untitled - Блокнот");
HWND EditClass = FindWindowEx(hWindowHandle, NULL, L"Edit", NULL);
PostMessage(EditClass, WM_KEYDOWN, 0x5A, 0x002C0001);
PostMessage(EditClass, WM_CHAR, 0x7A, 0x002C0001);
PostMessage(EditClass, WM_KEYUP, 0x5A, 0xC02C0001);
I used the code above on Windows 10 64-bit and everything worked. But on Windows 7 64-bit Ultimate, it doesn't work. It doesn't send a message to Notepad, although if I compare messages in Spy++ when I click and what I send, they are the same. But when I try to send a message, it is not even visible in Spy++.
Related
I'm making application that listens for device notifications(through WndProc, DBT_DEVICEREMOVECOMPLETE and DBT_DEVICEARRIVAL). When I process DBT_DEVICEARRIVAL in WndProc, I get device path from PDEV_BROADCAST_DEVICEINTERFACE structure. When I try to create device handle with that path, it succeeds.
HANDLE device_handle = CreateFile(device_path.c_str(),
GENERIC_WRITE | GENERIC_READ,
FILE_SHARE_WRITE | FILE_SHARE_READ | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0,
NULL);
if (device_handle == INVALID_HANDLE_VALUE)
return false;
But when I handle DBT_DEVICEREMOVECOMPLETE, I do the same thing with PDEV_BROADCAST_DEVICEINTERFACE structure, and the path is same as in DBT_DEVICEARRIVAL, but CreateFile always returns INVALID_HANDLE_VALUE
The funniest thing is, when I try to execute in debug mode, creating device handle works normally, both from DBT_DEVICEARRIVAL and DBT_DEVICEREMOVECOMPLETE.
I would appreciate any help.
It is improper to create handle when receiving DBT_DEVICEREMOVECOMPLETE message.
The system broadcasts the DBT_DEVICEREMOVECOMPLETE device
event when a device or piece of media has been physically removed.
You can use RegisterDeviceNotification to receive more specific device event messages. There is a MSDN:sample.
My App (Windows 7, Visual C++, Release-Build) needs to write some data when windows is shutting down (restarting, user logs off). After all I want to do the same stuff as on receiving WM_CLOSE-Message which gets called during a regular close of my App (Alt-f4, Closing the window,...)
I don't need any User-Input, Dialogs and so on. Just silent writing. The writing itself should last less than a second.
To do so I do the following:
LRESULT CMainFrame::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if (message == WM_CLOSE){
TRACE(_T("got WM_CLOSE"));
SaveMyData();
}
switch (message) {
case WM_QUERYENDSESSION:
TRACE(_T("findme: WM_QUERYENDSESSION"));
{
BOOL bShutdownBlocked = ShutdownBlockReasonCreate(theApp.m_pMainWnd->GetSafeHwnd(), _T("Save data"));
TRACE(_T("blocked: %d, GetlastError: %d"), bShutdownBlocked, GetLastError());
}
//continue shutdown-sequence
return TRUE;
case WM_ENDSESSION:
TRACE(_T("findme: WM_ENDSESSION. Store? %d"), FALSE != (BOOL)wParam);
if (FALSE != (BOOL)wParam)
{
long lTickStart = GetTickCount();
TRACE(_T(">>> Sleep..."));
::Sleep(1000); //just a Test: do something while shutting down
TRACE(_T("<<< Sleep: %d ms"), GetTickCount()-lTickStart);
BOOL bUnblockShutdown = ShutdownBlockReasonDestroy(theApp.m_pMainWnd->GetSafeHwnd());
TRACE(_T("unblock: %d"), bUnblockShutdown);
}
return 0L;
default:
return CMDIFrameWnd::WindowProc(message, wParam, lParam);
}
}
All Traces are redirected into a file which I examine.
I tested this with the Restart Manager from the Logo Testing Tools for Windows under Win7 (rmtool -S -pid) and it works fine. I get the following Trace-Output:
findme: WM_QUERYENDSESSION
blocked 1, GetlastError: 0
findme: WM_ENDSESSION. Store? 1
>>> Sleep...
<<< Sleep: 1015 ms
unblock: 1
got WM_CLOSE
Notice the "got WM_CLOSE" which calls code to save my Data.
But it does not work, when I actually shut windows down or logoff or use rmtool with the -lr option. In this case I just get the following output:
findme: WM_QUERYENDSESSION
blocked 1, GetlastError: 0
findme: WM_ENDSESSION. Store? 1
>>> Sleep...
<<< Sleep: 1000 ms
unblock: 1
Here the WM_CLOSE-Message is no received.
Is it wrong to rely on that Message when shutting down or what to I make wrong?
As MSDN says
When an application returns TRUE for WM_QUERYENDSESSION, it receives the WM_ENDSESSION message and it is terminated, regardless of how the other applications respond to the WM_QUERYENDSESSION message.
MSDN does not specify that Windows terminates your application by sending a WM_CLOSE to it. You should do the saving in the WM_ENDSESSION handler to be on the safe side.
BTW, you should remove the call to ShutdownBlockReasonCreate. What is the use of it? If it worked you would not get a WM_ENDSESSION anymore. That function should be called in advance if you want to prevent a shutdown.
I think the trick is do do everything right in WM_ENDSESSION without sending or receiving any Windows-Messages.
Also you shouldn't rely too much on the rmtool.exe, at least when used without the -l-Parameter. It just behaves diffent than shutting down windows. So to test shutdown-scenarios you really have to shutdown (logoff, restart) instead of just simulating it.
The following is a classic message loop. The TranslateMessage(const MSG*), as MSDN says, does translate the virtual-key message(WM_KEYDOWN) into character message(WM_CHAR). Then it will post this just translated WM_CHAR message into the thread message queue.
AFAIK the message queue should be a FIFO structure, and the message WM_CHAR will be sent at the end of queue when TranslateMessage returns. I make a experiment that pressing multiple keys down at the same time, e.g. 'a', 's', and 'd'. And I put a sleep(1000) to make these 3 WM_KEYDOWN messages to be queued first in the message queue before invoked TranslateMessage() .
while (GetMessage(&msg, NULL, 0, 0))
{
Sleep(1000) // make message queue receives all the WM_KEYDOWN before Translated
TranslateMessage(&msg);
DispatchMessage(&msg);
}
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
print_the_message(uMsg, wParam);
}
I expect the ordering should be
WM_KEYDOWN('a')
WM_KEYDOWN('s')
WM_KEYDOWN('f')
/* Queued before sleep wake */
WM_CHAR('a')
WM_CHAR('s')
WM_CHAR('f')
But print_the_message actually shows this ordering
WM_KEYDOWN('a')
WM_CHAR('a')
WM_KEYDOWN('s')
WM_CHAR('s')
WM_KEYDOWN('f')
WM_CHAR('f')
Does the character message WM_CHAR created by TranslateMessage have special priority or handling to make it can follow the previous WM_KEYDOWN message and cut in the queue?
The documentation says, with my emphasis:
The character messages are posted to the calling thread's message queue, to be read the next time the thread calls the GetMessage or PeekMessage function.
This matches the observed behaviour.
Hello to all stackoverflow great minds!! I need some explanation and suggestion on how to determine what have cause this problem. I hope nobody would be harass and will judge directly. If there are not clear here please be kind to reply.
I have an application that i am investigating right now. The application will shutdown the Windows if a power-off message was posted from one of its child processes.
Scenario:
When i start the application and post a power-off message nothing happens to windows only the application was exited. The second time I start the application then post again a power off message the windows shutdown or reboot depending on the condition i had send.
HANDLE hToken;
TOKEN_PRIVILEGES tp;
OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken);
LookupPrivilegeValue(NULL,
SE_SHUTDOWN_NAME, &(tp.Privileges[0].Luid));
tp.PrivilegeCount = 1;
tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tp, sizeof(tp), NULL, NULL);
if ( iApplicationProblemOccured == 1 ){
ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN | EWX_POWEROFF, 0L);
} else {
if(wShutdownType != 1) {
ExitWindowsEx(EWX_FORCE | EWX_REBOOT, 0L);
} else {
ExitWindowsEx(EWX_FORCE | EWX_SHUTDOWN | EWX_POWEROFF, 0L);
}
}
AdjustTokenPrivileges(hToken, TRUE, &tp, sizeof(tp), NULL, NULL);
ExitProcess(0);
The problem is why the execution of the ExitWindowsEx from the first power off does not works?
I have checked the tokenpriveleges from the first poweroff but it was enabled.
I have also change the dwReason to planned in the ExitWindowsEx but nothing happens.
From what i read in
https://msdn.microsoft.com/en-us/library/windows/desktop/aa376868(v=vs.85).aspx
if the return of ExitWindowsEx is 0, it does not necessarily determines that the execution of windows shutdown will be successful because the function will only initiate the shutdown process.
I am thinking that there is something process/services that blocks stops the windows shutdown process from the first power off..
Is there a way to debug the windows shutdown process after my application send the shutdown request?
I am hoping someone can help me with this issue!!
Thanks a lot..
i'm working on an application in MS Visual C++ using Windows API that must download a file and place it in a folder.
I have already implemented the download using URLDownloadToFile function, but i want to create a PROGRESS_CLASS progress bar with marquee style while the file is being downloaded, but it doesn't seems to get animated in the process.
This is the function I use for downloading:
BOOL SOXDownload()
{
HRESULT hRez = URLDownloadToFile(NULL,
"url","C:\\sox.zip", 0, NULL);
if (hRez == E_OUTOFMEMORY ) {
MessageBox(hWnd, "Out of memory Error","", MB_OK);
return FALSE;
}
if (hRez != S_OK) {
MessageBox(hWnd, "Error downloading sox.", "Error!", MB_ICONERROR | MB_SYSTEMMODAL);
return FALSE;
}
if (hRez == S_OK) {
BSTR file = SysAllocString(L"C:\\sox.zip");
BSTR folder = SysAllocString(L"C:\\");
Unzip2Folder(file, folder);
::MessageBoxA(hWnd, "Sox Binaries downloaded succesfully", "Success", MB_OK);
}
return TRUE;
}
Later I call inside WM_CREATE (in my main window's message processor):
if (!fileExists("C:\\SOX\\SOX.exe")) {
components[7] = CreateWindowEx(0, PROGRESS_CLASS,
NULL, WS_VISIBLE | PBS_MARQUEE,
GetSystemMetrics(SM_CXSCREEN) / 2 - 80,
GetSystemMetrics(SM_CYSCREEN) / 2 + 25,
200, 50, hWnd, NULL, NULL, NULL);
SetWindowText(components[7], "Downloading SoX");
SendMessage(components[7], PBM_SETRANGE, 0, (LPARAM) MAKELPARAM(0, 50));
SendMessage(components[7], PBM_SETMARQUEE, TRUE, MAKELPARAM( 0, 50));
SOXDownload();
SendMessage(components[7], WM_CLOSE, NULL, NULL);
}
And as I want, I get a tiny progress bar... But it's not animated, and when I place the cursor over the bar, the cursor indicates that the program is busy downloading the file.
When the download is complete, the window closes as i requested: SendMessage(components[7], WM_CLOSE, NULL, NULL);
So the question is how can I make the bar move while downloading the file? Considering that i want it done with marquee style for simplicity.
Thanks in advance.
Create a class that implements the IBindStatusCallback interface and then pass it to the last parameter of URLDownloadToFile(). You will then receive OnProgress events during the download, which you can use to update your UI as needed, pump the message queue for pending messages, etc.
I think I'd use InternetReadFile (or InternetReadFileEx). This will let you read a small amount at a time (e.g., 4 kilobytes) so you can update your status bar periodically (and handle any other messages as well).
To maximize UI responsiveness, you can do an asynchronous read. This will let you process messages immediately during the download instead of waiting for the next 4K (or whatever) block to finish. Over a fast connection, it probably won't make a noticeable difference (4K doesn't normally take long) but over a slow or undependable connection, it could be a fairly major help. Doing asynchronous downloading also gives you a chance to cleanly cancel the transaction if it takes too long.