Never ending Win32 Message loop [closed] - c++

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 13 years ago.
Improve this question
I have the following code:
MSG mssg;
// run till completed
while (true) {
// is there a message to process?
while(PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
// dispatch the message
TranslateMessage(&mssg);
DispatchMessage(&mssg);
}
if(mssg.message == WM_QUIT){
break;
}
// our stuff will go here!!
Render();
listeners->OnUpdate();
}
Once it enters the inner loop with peekmessage it does not exit until the application is closed. Thus if I place a breakpoint at Render() and OnUpdate(), they will never be called during the lifetime of the application.
This runs contrary to what I'm being told here and here. How do I do this properly?

A typical game loop has this form:
MSG mssg;
bool notdone = true;
// run till completed
while ( notdone ) {
// is there a message to process?
if (PeekMessage( &mssg, NULL, 0, 0, PM_REMOVE)) {
if (mssg.message == WM_QUIT) notdone = false;
// dispatch the message
TranslateMessage(&mssg);
DispatchMessage(&mssg);
} else {
// our stuff will go here!!
Render();
listeners->OnUpdate();
}
}

Related

how can we Hijack DLL to lock all directories in windows to verify [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I mean when we double click a directory,in requests us to verify.I think it can be done by dll-injection.Hope can give some ideas or tutorial.Thanks
Hijack DLL is not required. It use Window Message Hook.
At the first, create DLL that call SetWindowsHookEx.
hHookMsg = SetWindowsHookEx(WH_GETMESSAGE, (HOOKPROC)MsgProc, hInstance, 0);
For example, called install_hook, And MsgProc here
LRESULT CALLBACK MsgProc(INT nCode, WPARAM wp, LPARAM lp) {
CHAR className[256];
MSG *pmsg;
LVHITTESTINFO htif;
POINT pt;
pmsg = (MSG*)lp;
GetClassName(pmsg->hwnd, className, sizeof(className));
if (!strcmp(className, "SysListView32")) {
if (pmsg->message == WM_LBUTTONDBLCLK) {
GetCursorPos((LPPOINT)&pt);
htif.pt = pt;
ScreenToClient(pmsg->hwnd, &htif.pt);
SendMessage(pmsg->hwnd, LVM_HITTEST, 0, (LPARAM)&htif);
if ((htif.flags & LVHT_ONITEM) != 0) {
// you can write action here
}
}
}
return CallNextHookEx( hHookMesg, nCode, wp, lp );
}
And create EXE that call this install_hook.

FindWindowA Can't find process [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I tried to check value from a game but it seems FindWindowA can't find this game process I tried with other games and it worked. I checked in Process Hacker how the window is called and it still the same as in code but it still didn't work.
First project.cpp : main project file.
#include <iostream>
#include <Windows.h>
#include <string>
using namespace std;
DWORD pid;
DWORD Ammo = 0x01E7A938;
int MyAmmo;
int main()
{
HWND hWnd = FindWindowA(0, ("War Thunder Client"));
GetWindowThreadProcessId(hWnd, &pid);
HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, pid);
while (true)
{
cout << MyAmmo << endl;
Sleep(100);
system("CLS");
}
system("Pause");
}
First, you should check if FindWindowA does actually find your window. If it does not than hWnd will have the value of NULL.
Second, the value of MyAmmo is not set everywhere. It is initalised to 0 because it is a global variable, but otherwise it's value is not changed anywhere.

Win32 Main Message Loop for OpenGL [closed]

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 6 years ago.
Improve this question
My main message loop in a Win32 OpenGL application looks like this:
// Inside wWinMain
bool bQuit = false;
while( bQuit == false )
{
DWORD dwReturn = ::MsgWaitForMultipleObjects( 0, NULL, FALSE, 12, QS_ALLINPUT );
if( dwReturn == WAIT_OBJECT_0 )
{
MSG msg;
BOOL bReturn = ::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE );
if( bReturn != FALSE )
{
if( msg.message == WM_QUIT )
bQuit = true;
::TranslateMessage( &msg );
::DispatchMessage( &msg );
}
}
if( dwReturn == WAIT_OBJECT_0 || dwReturn == WAIT_TIMEOUT )
{
RenderFrame();
::SwapBuffers( hDc );
}
}
It works almost fine, I have only one problem: if I press Alt+F4 to close the window, it does not quit right after I release the key, however, if I hover the mouse over the window, it quits instantly.
A) Why is this? How should I modify my loop?
B) The original code I found did not use MsgWaitForMultipleObjects but called RenderFrame continuously. I think this way too much CPU time is wasted on redrawing the screen. Am I right? What is the usual way, do you spend all your excess capacity on drawing?
your error that you call PeekMessage only once per WAIT_OBJECT_0 but you need run it in loop while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE )) because we can have several messages here. and better use MsgWaitForMultipleObjectsEx instead - try this code:
bool bQuit = false;
while( !bQuit )// for (;;)
{
MSG msg;
switch(::MsgWaitForMultipleObjectsEx( 0, NULL, 12, QS_ALLINPUT, 0))
{
case WAIT_OBJECT_0:
while (::PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ))
{
if( msg.message == WM_QUIT )
bQuit = true;// return;
::TranslateMessage( &msg );
::DispatchMessage( &msg );
}
case WAIT_TIMEOUT:
RenderFrame();
::SwapBuffers( hDc );
}
}
if I press Alt+F4 to close the window, it does not quit right after I
release the key
several messages posted to your thread queue when you press Alt+F4, MsgWaitForMultipleObjects return, but you process not all it but only one
however, if I hover the mouse over the window, it quits instantly
new messages (WM_MOUSEMOVE) placed, but main MsgWaitForMultipleObjects again return and you at the end process all messages related to close process

C++: How might one draw an ASCII character at a specific location in command prompt [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Basically, I want to be able to create a function where I can go:
draw('C', x, y);
draw('h', x+1, y);
draw('a', x+2, y);
draw('r', x+3, y);
and command prompt will display Char at the given x and y position
All I could figure out in my research is that I will have to include <windows.h>, which I have very minimal experience with, and use the pre-defined content in that library.
This might help you get started. See https://msdn.microsoft.com/en-us/library/windows/desktop/ms682073(v=vs.85).aspx for more info.
#include <windows.h>
#include <stdio.h>
void main()
{
HANDLE screenBuffer = CreateConsoleScreenBuffer(
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL,
CONSOLE_TEXTMODE_BUFFER,
NULL);
if (screenBuffer == INVALID_HANDLE_VALUE)
{
printf("CreateConsoleScreenBuffer failed - (%d)\n", GetLastError());
return;
}
if (!SetConsoleActiveScreenBuffer(screenBuffer))
{
printf("SetConsoleActiveScreenBuffer failed - (%d)\n", GetLastError());
CloseHandle(screenBuffer);
return;
}
for (int x = 0; x < 10; ++x)
{
DWORD numCharsWritten;
WriteConsoleOutputCharacter(screenBuffer, "X", 1, COORD{(short)x, 1}, &numCharsWritten);
Sleep(1000);
}
CloseHandle(screenBuffer);
}

core dump in SDL, where? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
Just in the first lines happened the ** core dump.
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen;
SDL_Surface *background;
SDL_Surface *tablero;
SDL_Surface *x;
SDL_Surface *o;
SDL_Rect posFondo;
//SDL_Rect posPlayerX[5];
//SDL_Rect posPlayerO[5];
SDL_Event suceso;
// Definir
x = SDL_LoadBMP("players.bmp");
o = SDL_LoadBMP("players.bmp");
tablero = SDL_LoadBMP("tablero.bmp");
/* Clear bg colors */
SDL_SetColorKey(x, SDL_SRCCOLORKEY,
SDL_MapRGB(x->format, 255, 0, 255));
SDL_SetColorKey(o, SDL_SRCCOLORKEY,
SDL_MapRGB(o->format, 255, 0, 255));
SDL_SetColorKey(tablero, SDL_SRCCOLORKEY,
SDL_MapRGB(tablero->format, 255, 0, 255));
/* END */
screen = SDL_SetVideoMode(600, 600, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL) { return 0; }
/* Positions */
posFondo.x = 0;
posFondo.y = 0;
while (true) {
SDL_BlitSurface(tablero, NULL, screen, &posFondo);
//SDL_BlitSurface
while(SDL_PollEvent(&suceso)) {
if (suceso.type == SDL_QUIT) {
break; break;
}
}
SDL_Flip(screen);
SDL_Delay ( 5 );
}
I can't find the problem, I try to delete some things and add dynamic variables (new, delete types) and nope...
I only see two errors in your code. For starters, there is no need to call SDL_Init() twice. When you call SDL_Init(SDL_INIT_EVERYTHING), you're already initializing video. Get rid of the second call to SDL_Init(). The second issue in your code would be in your:
while(SDL_PollEvent(&suceso)){
if(suceso.type == SDL_QUIT){
break; break;
}
}
When you call the first break, it exits the while loop. The second break is never used. Because of this, I'm going to guess you encounter a situation in which the loop never ends. Here is what I would recommend for your loop:
bool quit = false;
while (!quit) {
SDL_BlitSurface(tablero, NULL, screen, &posFondo);
//SDL_BlitSurface
while(SDL_PollEvent(&suceso)) {
if (suceso.type == SDL_QUIT) {
quit = true;
}
}
SDL_Flip(screen);
SDL_Delay ( 5 );
}
EDIT: You should probably also initialize your SDL_Surface*'s to NULL.
EDIT#2: There's really no need to call SDL_BlitSurface() and SDL_Flip() every frame unless your tablero surface is going to change. If you use SDL_WaitEvent() instead of SDL_PollEvents(), you can update your screen and such whenever the user does something, rather than all the time for no real reason.