Is any application have HWND? - c++

I'm learning C++. I wonder is any C++ application have HWND. Example bellow app, with no window created.
If it have, how I can get its HWND? Thank you very much!
#include <windows.h>
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow )
{
MSG msg;
while( GetMessage( &msg, NULL, 0, 0 ) )
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
}

"I'm learning C++. I wonder is any C++ application have HWND."
The shortest answer is no. HWND is a defined type in a library used to write Windows applications. C++ is a language that can be used to do that as long as you have library that gives you functions (including HWND type.)
You can write programs for CMD prompt or for Unix which have nothing to do with Windows.
Try this C style program. Copy text below to a.cpp file, and compile it to generate a.exe:
#include <stdio.h>
int main()
{
printf( "Hello world\n" ) ;
return 0 ;
}
When you run cmd, change directory to where a.exe is, and run a.exe then you will see:
Hello world
If you plan on learning C++ you don't need to write Windows applications. You can write CMD or Linux programs.
Find a good book on the C++ subject.
Good luck!

You need to create one.
check out CreateWindowEx and ShowWindow

Related

How to display cmd prompt after printing output to cmd.exe?

I have an winmain application that is executed from cmd.exe and prints output to it. I atach to the cmd.exe using AttachConsole(ATTACH_PARENT_PROCESS). After application is executed and output is printed to cmd.exe command line prompt is not displayed and it looks like application is stil running(while it is already closed). Before closing my application I release the console using FreeConsole().
#include <iostream>
#include <fstream>
#include <windows.h>
int wWinMain
(
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPWSTR lpCmdLine,
int nCmdShow
)
{
AttachConsole(ATTACH_PARENT_PROCESS);
std::wofstream console_out("CONOUT$");
std::wcout.rdbuf(console_out.rdbuf());
std::wcout << L"\nSome Output" << std::endl;
FreeConsole();
return 0;
}
Current result:
My goal:
How should I make prompt C:New folder> appear after myapp.exe has printed its output and is closed.
In case the question has not been answered yet (after such a long time), it is required to simulate the actual pressing of the 'Enter' key in the console window by sending (or, preferably, posting) the corresponding WM_KEYDOWN message to the console window, i.e.
after
std::wcout << L"\nSome Output" << std::endl;
and before calling
FreeConsole(),
insert the following:
HWND hWndCon_ = ::GetConsoleWindow();
if( hWndCon_ ) {
::PostMessage( hWndCon_, WM_KEYDOWN, VK_RETURN, 0 );
}
or simply
::PostMessage( ::GetConsoleWindow(), WM_KEYDOWN, VK_RETURN, 0 );

How to change window size in C++?

Hi I have to run a program in C++ and I want to make sure when the program is executed, it opens the console in a particular size/dimensions, so that the display in my program is proper. I need help as I don't know how to do it. I am using Dev C++ 5.42(Orwell). I tried using
#include<iostream>
#include<windows.h>
using namespace std;
SetWindowPos(hwnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
int main(){
cout<<"Hello World";
}
and got an error
[Error] expected constructor, destructor, or type conversion before '(' token
I'm a beginner and hence I don't know much about these things.
That function is useless for console applications, which don't own a window (unless they create one using the CreateWindow API, which is atypical for console apps). Instead their output is connected to csrss, which does have windows.
You should be using
SetConsoleScreenBufferSize
SetConsoleWindowInfo
instead.
There's an example at http://www.cplusplus.com/forum/windows/10731/
This works for me:
HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ MoveWindow(hwnd ,340,550 ,680,150 ,TRUE); }
If your looking for changing the screen buffer then :
HANDLE buff = GetStdHandle(STD_OUTPUT_HANDLE);
COORD sizeOfBuff;
sizeOfBuff.X=150;
sizeOfBuff.Y=100;
SetConsoleScreenBufferSize(buff,sizeOfBuff);
To resize the screen use DaveWalley's solution.
Or you could do this (only for resizing)
HWND hwnd = GetConsoleWindow();
if( hwnd != NULL ){ SetWindowPos(hwnd ,0,0,0 ,1000,300 ,SWP_SHOWWINDOW|SWP_NOMOVE); }
Be sure to include:
#define _WIN32_WINNT 0x0502
#include<windows.h>
at the begining of the file. Literally as the first lines.
Got the solution by reding about the functions mentioned by Ben Voigt.

C++ GetAsyncKeyState alternative

I was using Windows.h library's GetAsyncKeyState function to define which key was pushed on the keyboard . Here is the program which returns the int value of the pushed key for example.
#include <iostream>
#include <windows.h>
using namespace std;
int main (){
char i;
for(i=8;i<190;i++){
if(GetAsyncKeyState(i)== -32767){
cout<<int (i)<<endl;
}
}
return 0;
}
But now I am trying to make an openGl very simple game and this function appeared to be very slow for that . Is there a function which will not need a for cycle 180 times and an if statement .
Thanks
You're trying to make a opengl game, so you're probably going to use GLFW. If that's the case then you can get away with using glfwGetKey() as such:
if (glfwGetKey(GLFW_KEY_UP) == GLFW_PRESS) {...}
if (glfwGetKey(GLFW_KEY_DOWN) == GLFW_PRESS) {...}
That method is nicely explained in tutorial 6 from opengl-tutorial.org. But you should probably start from the first tutorial of beginners tutorials and later intermediate tutorials.
If you're going to make the win32 window by yourself you can listen for the WM_KEYDOWN or WM_KEYUP messages and get the virtual key code from wparam. After a minute of googling I found you these two sites for examples here and here.
You can use a combination of PeekMessage and TranslateMessage and DispatchMessage. It's quite simple to register your own windows procedure (WNDPROC) to process input messages from windows.
MSG msg;
while(PeekMessage( &msg, NULL, 0, 0 ))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
The DispatchMessage function calls your registered procedure. See: this for more info.

mouse hook broke all windows input

I'm new to c++ programming and am trying to make a small reporting tool that will detect when the mouse and keyboard haven't been touched over a period of time.
I have been searching for some example mouse hook code and found this
#define _WIN32_WINNT 0x0400
#pragma comment( lib, "user32.lib" )
#include <windows.h>
#include <stdio.h>
HHOOK hMouseHook;
__declspec(dllexport) LRESULT CALLBACK KeyboardEvent (int nCode, WPARAM wParam, LPARAM lParam)
{
MOUSEHOOKSTRUCT * pMouseStruct = (MOUSEHOOKSTRUCT *)lParam;
if (pMouseStruct != NULL)
printf("Mouse position X = %d Mouse Position Y = %d\n", pMouseStruct->pt.x,pMouseStruct->pt.y);
return CallNextHookEx(hMouseHook,
nCode,wParam,lParam);
}
void MessageLoop()
{
MSG message;
while (GetMessage(&message,NULL,0,0)) {
TranslateMessage( &message );
DispatchMessage( &message );
}
}
DWORD WINAPI MyMouseLogger(LPVOID lpParm)
{
HINSTANCE hInstance = GetModuleHandle(NULL);
if (!hInstance) hInstance = LoadLibrary((LPCSTR) lpParm);
if (!hInstance) return 1;
hMouseHook = SetWindowsHookEx (
WH_MOUSE_LL,
(HOOKPROC) KeyboardEvent,
hInstance,
NULL
);
MessageLoop();
UnhookWindowsHookEx(hMouseHook);
return 0;
}
int main(int argc, char** argv)
{
HANDLE hThread;
DWORD dwThread;
hThread = CreateThread(NULL,NULL,(LPTHREAD_START_ROUTINE)
MyMouseLogger, (LPVOID) argv[0], NULL, &dwThread);
if (hThread)
return WaitForSingleObject(hThread,INFINITE);
else return 1;
}
I compiled and ran this as a test which seems to do what I wanted. After that I started testing a couple of things. compiled on c++ and g++ for the hell of it. removed the pMouseStruct and tested "if(lParam)" instead. Everything seemed to be behaving and it was getting late so I decided to return to this the next day.
When I booted in to windows today, I was unable to move the mouse or input anything with the keyboard. It seems input inside windows (normal and safe mode) is not working anymore. Any ideas how to fix this?
Using Windows 7 64bit.
So far I have tried the following:
- reversed a recent overclocking tweak.
- returned the code to the way it was before i played with it, recompiled, and ran.
- Googling.
- chkdsk /f on the system drive.
- system restore (doesn't like raid)
- copied user32.dll off a friend.
Plz forgive my ignorance :-)
I ended up reinstalling windows 7 and soon ran into more strange problems. I have since discovered that standby was corrupting various things and have stopped using it. Now I'm running a fresh install of windows 7 that has never been put to sleep. Still don't have any bizarre problems to this day :-)

detect if application running on virtual box

I went thrrough some links in stackoverflow. But code given here doesnt work for virtual box. I have also tried redpill but thats not working too. my application will run on both linux and windows(preferably).
Please let me know if anyone has any solution.
Edit: Preet Sangha's link not working as well
VBox 1.0 uses a different method. Check http://spth.virii.lu/eof2/articles/WarGame/vboxdetect.html
from http://www.gedzac.com/rrlf.dr.eof.eZine/articles/WarGame/vboxdetect.html
Check if the pseudo-device \\.\VBoxMiniRdrDN exists in the system (you need CreateFile())
#include <windows.h>
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
if(CreateFile("\\\\.\\VBoxMiniRdrDN",GENERIC_READ,FILE_SHARE_READ,
NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL) != INVALID_HANDLE_VALUE)
{
MessageBox(NULL,"VBox detected!","Warning",MB_OK|MB_ICONWARNING);
}
else
{
MessageBox(NULL,"Not inside VBox","Info",MB_OK|MB_ICONINFORMATION);
}
}