How to set c console window title - c++

How to set the console window title in C?
printf("%c]0;%s%c", '\033', "My Console Title", '\007');
This works only under linux, not in windows.
Does anybody know a "cross-platform" solution? (of course not system ( title=blah ))

windows.h defines SetConsoleTitle().
You could use that everywhere, and declare your own function for linux platforms that does the same thing.

Sounds similar to this posting: (Which is for Java, but the accepted answer uses JNI [ie a C Native call].
How to change command prompt (console) window title from command line Java app?

You can do this by calling SetConsoleTitle.

Maybe you have to implement a "cross-playform" solution yourself.
For windows 2000+, you can use SetConsoleTitle(), more imformation can be found on MSDN.

The most easy way to achieve this in C is to use windows.h header and use the SetConsoleTitle function
Simple Script
#include <stdio.h>
#include <windows.h>
#include <conio.h>
int main()
{
HANDLE handleConsole = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTitle("Mini Desktop App"); // Here add the title of the window
while(1){
printf("Works as expected\n");
printf("Press any Key to exit :)\n");
getch();
break;
}
return 0;
}

Related

C++ findwindow with traditional chinese character [duplicate]

So Im trying to create a camo unlocker but I have never had trouble with getting process id through findwindow
but now Im trying to find black ops 2's proc id but the window name doesn't work
Call of Duty®: Black Ops II
CODE:
#include <iostream>
#include <Windows.h>
#include <tchar.h>
using namespace std;
int main(){
HWND hWnd = FindWindow(0, _T("Call of Duty®: Black Ops II - Multiplayer"));
if(hWnd){
cout << "window found" << endl;
}
return 0;
}
It looks like the registered symbol could be unicode, you'll want to use FindWindowW():
Unicode and ANSI names
FindWindowW (Unicode) and FindWindowA (ANSI)
Alternatively, you could use FindWindowEx() and search for the window class name.
FindWindow works correctly. The possible causes for your problem are:
You have an encoding error. You should use the Unicode API:
HWND hWnd = FindWindowW(NULL, L"Call of Duty®: Black Ops II - Multiplayer");
There is no top level window with that window text. Use a tool like Spy++ to check that.
You should also make sure that you read the documentation carefully. Specifically it states the following:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
You should do as it says and call GetLastError in event of failure.
I would try to find the window by class as an application may change its title and class names usually do not have fancy characters. If you do not know them look for some tool (Spy++ + i think it comes with Visual Studio) or create a list with a simple tool using EnumWindows and GetClassName
FindWindowA worked for me :) so I just changed from tchar to the normal HWND hWnd = FindWindowA(0, ("Call of Duty®: Black Ops II - Multiplayer"));

How to make a MessageBox with a box to type text in Eclipse C++?

I am currently looking for a way to type a text into a messagebox and then get it back as a String, but I have no idea if this is even possible in Eclipse C++, I'm not that familiar with c++. This is the code that i have at the moment:
#include<windows.h>
#include "Helloworldschrijver.hpp"
using namespace std;
int starting text () {
MessageBoxA(NULL, "Test 1",
"Insert Time",
MB_YESNO | MB_ICONQUESTION);
return 0;
}
I'm currently looking for a way to type a text into a messagebox and then get it back as a String
The Windows API does not have a single function for the purpose of prompting the user for text input, like it does for displaying a message box. So you will have to create your own UI dialog that has an edit field on it. Which means you will have to learn Win32 UI programming. Or else install and use a 3rd party UI framework that handles the details for you.

Use ShutdownBlockRequestCreate in Win32 Console application

What is the proper method of blocking premature termination of a Win32 Console Application running on Windows 7?
When Vista was introduced, there were changes regarding how Application Shutdown happened. Contrary to the behavior in XP, which was to open up a UI requesting whether the user wants to force close or not, Windows Vista (and 7) terminates the process if nothing is done programmatically to prevent it. Console apps and applications without a top level window visible must also use the new function ShutdownBlockRequestCreate to provide a reason for Vista to show in the UI that pops up or it will terminate the program after 5 seconds anyway.
Below is my attempt at using the ShutdownBlockRequestCreate function in a Win32 Console application; the precompiled header option was removed from the project after creation by the wizard. I get the error code of 5, corresponding to ERROR_ACCESS_DENIED, whenever I use the function. This is apparently (according to the Application Shutdown link) because I am not calling the function from the same thread as was used to create the window (the console window).
#include <iostream>
#include <tchar.h>
#include <conio.h>
#include <windows.h>
typedef BOOL (WINAPI *SBRCREATEFUNC)(HWND,LPCWSTR);
void RegisterShutdownBlockReason() {
SBRCREATEFUNC ShutdownBlockReasonCreate;
HWND hWnd = GetForegroundWindow();
HINSTANCE hinstLib = LoadLibrary(TEXT("user32.dll"));
if (hinstLib != NULL) {
ShutdownBlockReasonCreate = (SBRCREATEFUNC) GetProcAddress(hinstLib,"ShutdownBlockReasonCreate");
if(ShutdownBlockReasonCreate != NULL) {
if(!(ShutdownBlockReasonCreate) (hWnd, L"Terminating Communication Sessions")) {
printf("\nfailed To Register Reason, failure code: %d\n", GetLastError());
} else {
printf("\nRegistered Reason\n");
}
} else {
printf("\nCouldn't load ShutdownBlockReasonCreate procedure\n");
}
} else {
printf("\nFailed to LoadLibrary(\"user32.dll\")\n");
}
}
int _tmain(int argc, _TCHAR* argv[]) {
RegisterShutdownBlockReason();
printf("Type to terminate program.\n");
getch();
return 0;
};
As a workaround, could you create a message-only window at startup and use ShutdownBlockReasonCreate on its window handle?
It doesn't make sense to pass the console window handle from a console program because the console window is owned by the CSRSS subsystem, not your console program. Furthermore, what if your console is run in full-screen text mode? Regardless of who owns what, now there's not even a window around your console!
Will it let you pass a NULL hWnd?
EDIT: Okay then, what if your console program creates its own hidden window and uses that?

how do I use find window and sendmessage in empty visual c++ project (or console app)

I want to make the most simplest application that can communicate via windows send messages (and parse json). I have found a sample code:
CWnd* pWnd = FindWindow("old title");
pWnd->SendMessage(WM_SETTEXT,0,(LPARAM)"New title");
That works... but only if I use MS Visual Studios "create new MFC form application" wizard. How can I make a console application that sends messages to my program? Or can I? What do I need to include/link if I start an empty project or console application?
The goal in pseudocode:
a = ""
while !EOF
a += read(stdin)
commandArray = jsonToArray(a)
CWnd* pWnd = FindWindow("program");
pWnd->SendMessage(WM_COPYDATASTRUCT,0,commandArrayWrappedInCOPYDATASTRUCT);
exit
The annoyance is that the effective part of the code is roughly 20 lines (above), but the wizard generated part is hundreds of lines. And most of them is stuff that I don't understand. Plus, I get a window that I don't need.
EDIT
Final main.cpp (without the json stuff):
/*
This closes calculator
*/
#include <Windows.h>
#include <atlstr.h>
int main (void)
{
HWND HWnd = FindWindow(NULL, CStringW("Calculator"));
SendMessage(HWnd, WM_CLOSE, 0, 0);
return 0;
}
br,
Juha
If you want something so simple, then I'd just forget all about MFC and start with a basic console app from the New Project Wizard. MFC seems rather heavy duty for something so simple.

Launch web page from my application

Ok, this probably has a really simple answer, but I've never tried to do it before: How do you launch a web page from within an app? You know, "click here to go to our FAQ", and when they do it launches their default web browser and goes to your page. I'm working in C/C++ in Windows, but if there's a broader, more portable way to do it I'd like to know that, too.
#include <windows.h>
void main()
{
ShellExecute(NULL, "open", "http://yourwebpage.com",
NULL, NULL, SW_SHOWNORMAL);
}
I believe you want to use the ShellExecute() function which should respect the users choice of default browser.
Please read the docs for ShellExecute closely. To really bulletproof your code, they recommend initializing COM. See the docs here, and look for the part that says "COM should be initialized as shown here". The short answer is to do this (if you haven't already init'd COM):
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
For the record (since you asked for a cross-platform option), the following works well in Linux:
#include <unistd.h>
#include <stdlib.h>
void launch(const std::string &url)
{
std::string browser = getenv("BROWSER");
if(browser == "") return;
char *args[3];
args[0] = (char*)browser.c_str();
args[1] = (char*)url.c_str();
args[2] = 0;
pid_t pid = fork();
if(!pid)
execvp(browser.c_str(), args);
}
Use as:
launch("http://example.com");
You can use ShellExecute function.
Sample code:
ShellExecute( NULL, "open", "http://stackoverflow.com", "", ".", SW_SHOWDEFAULT );
For some reason, ShellExecute do not work sometimes if application is about to terminate right after call it. We've added Sleep(5000) after ShellExecute and it helps.