It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
AVG says that the executable compiled from the code below is a high priority "Trojan horse PSW.Agent.AYRW". Is something wrong here, or is this just a false alarm?
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:", MB_OK | MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Is this really creating a virus?
Edit: this is now that much more important because I can't even compile the program to its original location anymore without windows restricting my access. Grumble... AVG 2012 was so much better.
let AVG know that it's a false positive http://samplesubmit.avg.com/
Related
I just want to click the button and change the text of the button, however, I would get the WM_CLOSE message, it confused me.
const wchar_t *g_szClassName = L"myWindowClass";
HWND hwnd;
HWND hButtonLine;
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
{
hButtonLine = CreateWindowW(L"BUTTON", L"change title", WS_VISIBLE | WS_CHILD, 20, 50, 150, 25, hwnd, (HMENU)ID_CHANGE_TITLE, NULL, NULL);
break;
}
case WM_COMMAND:
switch (HIWORD(wParam)) {
case BN_CLICKED:
SetWindowText(hButtonLine, L"You clicked the button");
break;
default:
break;
}
case WM_CLOSE:
if (MessageBox(hwnd, L"Really quit?", L"My application", MB_OKCANCEL) == IDOK)
{
DestroyWindow(hwnd);
}
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, L"Window Registration Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
L"Happy Today",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 450, 250,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, L"Window Creation Failed!", L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
oh, thks #RaymondChen, I missed the break here.
A GUI app I'm writing does send a keydown event to another window, a cmd.exe.
PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
The event is sent just fine when it's done on events like WM_CREATE, WM_KEYUP, etc (a new line appears in cmd.exe).
Then I setup a global hotkey with RegisterHotKey. In a WM_HOTKEY handler I successfully receive key presses but PostMessage has no effect anymore.
How to fix that?
Full example, minified as possible:
#include <iostream>
#include <Windows.h>
using namespace std;
const char g_szClassName[] = "myWindowClass";
BOOL CALLBACK enumWindows(HWND hwnd, LPARAM lParam) {
char winTitle[1024*10];
GetWindowText(hwnd, winTitle, sizeof(winTitle));
if (strstr(winTitle, "cmd.exe") != NULL) {
cout << "Sending a message to window " << hwnd << ": " << winTitle << endl;
PostMessage(hwnd, WM_KEYDOWN, VK_RETURN, 0);
}
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
case WM_CREATE:
// register hotkey ctrl+alt+s
RegisterHotKey(hwnd, 100, MOD_ALT | MOD_CONTROL, 'S');
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
EnumWindows(enumWindows, 0);
PostQuitMessage(0);
break;
case WM_HOTKEY:
// hotkey ctrl+alt+s fired
EnumWindows(enumWindows, 0);
break;
case WM_KEYUP:
EnumWindows(enumWindows, 0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
// just init stuff, do not waste your time
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, g_szClassName, "Test", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 240, 120, NULL, NULL, hInstance, NULL);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
Windows 7 64-bit
Raymond Chen has documented why this doesn't work the way you expect (PostMessage IS working just fine, but the response of the other window depends on actual keyboard state, just as I surmised in my comment). See his blog post:
You can't simulate keyboard input with PostMessage
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have these tow errors and can't fix them what ever i try. i am running windows 8.1
Error 1 error C3861: 'InitMainWindow': identifier not found
Error 2 error C2440: '=' : cannot convert from 'LRESULT (__stdcall *)(HWND,WPARAM,LPARAM)' to 'WNDPROC'
code:
#include <windows.h> // include the basic windows header file
bool InitMainWindow(HINSTANCE, int);
LRESULT CALLBACK MsgProc(HWND UINT, WPARAM, LPARAM);
const int width = 800;
const int height = 600;
HWND hwnd = NULL;
// the entry point for any Windows program
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if (!InitMainWindow(hInstance, nCmdShow))
return 1;
MSG msg = { 0 };
while (WM_QUIT != msg.message)
{
if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return static_cast<int>(msg.wParam);
}
}
bool InitMainWindow(HINSTANCE hInstance, int nCmdShow)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(wcex);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.lpfnWndProc = MsgProc;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)GetStockObject(NULL_BRUSH);
wcex.lpszClassName = NULL;
wcex.lpszMenuName = NULL;
wcex.hIconSm = LoadIcon(NULL, IDI_WINLOGO);
if (!RegisterClassEx(&wcex))
return false;
hwnd = CreateWindow (
NULL,
NULL,
WS_OVERLAPPED | WS_SYSMENU | WS_CAPTION,
GetSystemMetrics(SM_CXSCREEN) / 2 - width / 2,
GetSystemMetrics(SM_CYSCREEN) / 2 - height / 2,
width,
height,
(HWND)NULL,
(HMENU)NULL,
hInstance,
(LPVOID*)NULL);
if (!hwnd)
return false;
ShowWindow(hwnd, nCmdShow);
return true;
}
LRESULT CALLBACK MsgProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_CHAR:
switch (wParam)
{
case VK_ESCAPE:
PostQuitMessage(0);
return 0;
}
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
You're missing a comma in your declaration of MsgProc.
You have
LRESULT CALLBACK MsgProc(HWND UINT, WPARAM, LPARAM);
which declares a function that takes an HWND named UINT.
You need
LRESULT CALLBACK MsgProc(HWND, UINT, WPARAM, LPARAM);
After this, it compiles fine.
The following program works on Windows 7 but not on Windows 7 Embedded Standard Service Pack 1. It does not prevent the shutdown (e.g. shutdown /r /t 0) from happening.
This is a similar question as the one which is linked as duplicate. But that question has the accepted answer of "it does not work". I also provide sourcecode to a working solution in Windows 7 which just needs some adjustment for Windows Embedded.
Any ides how to get it working on Embedded? Or at least a hint where the Windows Docu points that difference out?
#include <windows.h>
#include <stdio.h>
const char g_szClassName[] = "myWindowClass";
// Step 6: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_QUERYENDSESSION:
OutputDebugStr("Got WM_QUERYENDSESSION message\n");
char buf[1024];
sprintf(buf, "Callback %d %d\n", wParam, lParam);
OutputDebugStr(buf);
return FALSE; // FALSE should prevent reboot
break;
case WM_ENDSESSION:
OutputDebugStr("Got WM_ENDSESSION message\n");
Sleep(5000); // Should never get here!
break;
case WM_CLOSE:
OutputDebugStr("Got WM_CLOSE message\n");
DestroyWindow(hwnd);
break;
case WM_DESTROY:
OutputDebugStr("Got WM_DESTROY message\n");
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: We provide a reason for the shutdown prevention
BOOL ret = ShutdownBlockReasonCreate(hwnd, L"PreventShutdown running which prevents shutdown");
if(ret == FALSE)
{
MessageBox(NULL, "ShutdownBlockReasonCreate Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 4: We elevate the program to be asked as soon as possible to inhibit shutdown
ret = SetProcessShutdownParameters(0x4FF, SHUTDOWN_NORETRY)
if(ret == FALSE)
{
MessageBox(NULL, "ShutdownBlockReasonCreate Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
OutputDebugStr("Now starting message loop\n");
// Step 5: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
We already have a canonical answer for this question, you'll find it here. Seven hundred views and half a year of researching this make this the best known answer, it can't be done.
Keep an eye on your existing question on the MSDN Forums.
I'll close with a few graphical explanations why this kind of feature is not implemented on an embedded operating system:
This program works well when the left mouse button is pressed in the window of application, but what i need is that the program also registers when the button is pressed out of the application. I am running under windows 7.
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_LBUTTONDOWN:
{
char szFileName[MAX_PATH];
HINSTANCE hInstance = GetModuleHandle(NULL);
GetModuleFileName(hInstance, szFileName, MAX_PATH);
MessageBox(hwnd, szFileName, "This program is:",
MB_OK | MB_ICONINFORMATION);
}
break;
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
You can try the Windows Raw Input API, which ignores focus.