I made a program using Win32Api. In the program, it has "ShellExecute" phrase in order to execute chrome for searching.
Following is a abstraction fo what i wrote.
#include <windows.h>
#include <shellapi.h>
int main () {
ShellExecute(NULL, L"open", searching_url, NULL, NULL, SW_SHOWMAXIMIZED);
}
It works in my computer. But, I sent this windows app to other people and they said it doesn't work. I mean only shellexecute doesn't work. I cannot catch where to start figuring out what's wrong here.
Would you please suggest How I figure it out in structured way?
Thanks.
Two things to try:
ShellExecute can require COM to be initialized under certain circumstances. The docs for the ShellExecute function explain why in more detail. You should call CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE) before the call to ShellExecute and CoUninitialize() afterwards.
Instead of using L"open" as the verb, you should generally use NULL. NULL will always give you the "default" action for an object (equivalent to double-clicking it) which is not always "open".
Related
Hi i Want to Add Window(Gui) to my ConsoleApplication in C++
How to do this?? I need something called Hwnd? I tried to use it and it does not work for me
Basically what I want to do is a MessageBox that will be every time somewhere else on the screen .. but I realized that you have to do it with HWND.
I need Tutorial How to create windows with hwnd
I try:
HWND GetWindow(HWND hWnd, UINT uCmd);
Check if the following code meet your requirement:
#include <windows.h>
int main()
{
MessageBox(nullptr, TEXT("Hello World!"), TEXT("Message"), MB_TOPMOST);
}
It will like this:
You've used multiple tags, I have a winapi solution, not a winforms one.
You can use a std. MessageBox by calling the function, but they can't be „evolved“ into your custom windows for any reasonable „price“ so they can only notify user and ask simple yes/no questions. For „real“ windows, let's forget MessageBox.
The simplest custom window is a std. windows Dialog. It uses the pre-defined window class #32770, so you don't have to register your window class, create a message loop etc. The simplest way to open it is DialogBoxParam function. You must make a .rc dialog script (there are lots of visual editors), compile it with a resource compiler, link into your .exe and pass it's name as the parameter for DialogBoxParam.
Here's an example of a window inside a console application (C++ part only), using this DialogBoxParam call
DialogBoxParam(GetModuleHandle(NULL),"EXAMPLE",NULL,ExampleWindowFunction,NULL);
It's here: https://pastebin.com/Crkdy5FB
It also contains image drawing (you probably don't need it yet, it's left from another winapi example). Use it as a sandbox, you'll probably quickly understand how it works and what role the hWnd plays here.
I understand your problems. GUI got so overcomplicated so it's hard to understand what exactly you don't understand. It prevents from asking a good question.
I've been trying to get the output handle to my console, but it doesn't seem to work. I got it to set the color of my text, but it's not changing.
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hOut, 0x0A)
I tried to debug it and I think I the handle isn't right. Is there any other way to do this, and is it normal that it doesn't work? Any fixes?
Thanks!
EDIT: Let me clarify, the handle that I'm trying to get is invalid. I've no idea how to fix it. I guess I'll start looking for alternatives; maybe something is wrong with my code.
The standard handlers are initialized during process creation, if you call AllocConsole the new console is created far later. AllocConsole can change the standard handles, but it's far too late for them to be used by startup code, such as the C runtime library initialization.
The best thing to do in this case is CreateFileW(L"CONOUT$", ...), which gets a console handle no matter whether you are attached to parent process's console, the OS created one for you because your PE header is /SUBSYSTEM:CONSOLE, or you called AllocConsole. And it gets the console handle even when standard handles are redirected.
And if you think you may call FreeConsole, you should be sure to close any handles returned by CreateFile first. In the general case where the console remains active until process exit, you can let the OS close the handle for you during process cleanup.
Since you specify hOut is INVALID_HANDLE_VALUE (or potentially NULL), try calling GetLastError to find out why. It is likely you don't have console session established.
Is this a win32 Console Application or is it a Windows SubSystem application (does it have WinMain?)
You could try AttachConsole(ATTACH_PARENT_PROCESS) instead of AllocConsole before GetStdHandle.
In either case, AllocConsole and AttachConsole return a BOOL which, if FALSE, indicates you can call GetLastError to find out why.
Make sure you aren't calling hOut = GetStdHandle(STD_OUTPUT_HANDLE) followed by CloseHandle(hOut) prior to the lines listed above. Unlike AllocConsole and FreeConsole, closing the std handle is not a good idea.
Finally, try:
#define _WIN32_WINNT 0x0501 before #include <windows.h>
So much drama for such a small thing... and by the way #Ben's answer is the proper answer is actually right.
For your problem, just do this:
freopen("CONIN$", "r", stdin);
freopen("CONOUT$", "w", stdout);
freopen("CONOUT$", "w", stderr);
This will allow you to get the handle of any console you have. Make sure you put this after AllocConsole();
Enjoy?!
Ok so I found an answer. Seems like a simple edit can fix it
#define setcsattr(clr) SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), clr)
I need a button I can put on the users desktop/process bar in Windows 8 (any sub menu is unacceptable), that will start the users screensaver (with password protection, custom parameters etc). This can for example be a shortcut to a batch script.
Some suggestions for solutions I have found:
Starting screensaver from /system32: Does not work
Starts a specific screensaver you choose, not the users screensaver.
Does not take parameters (ie wrong text/pic and no password protection).
WIN + L: Does not work.
It is not a button on the users Desktop, and SendKeys does not seem to be able to do the WIN key, so a batch script for this is not possible.
It goes to log in screen, not screensaver.
rundll32.exe user32.dll, LockWorkStation: Does not work.
It corrupts the stack - https://blogs.msdn.microsoft.com/oldnewthing/20040115-00/?p=41043/
C++ program: So far this is what I have that might work.
#include <windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int cmdShow)
{
HWND DeskWin = GetDesktopWindow();
Sleep(750);
PostMessage(DeskWin, WM_SYSCOMMAND, SC_SCREENSAVE, NULL);
return 0;
}
I have never worked with Windows system messages before, so any comments / thoughts on if this will always work is appreciated.
Or if someone just have a way they think is better.
--
Reasons for the need: IT-scared users. They will laugh at you if you suggest remembering a hotkey combination, or going through multiple menus (like Start -> userName -> Lock), the latter is also a time problem as they might have to leave computer fast (hence the need for password protection).
EDIT** C++ Code has been modified to reflect feedback. Sendmessage changed to Postmessage, don't want a blocking call in a codesnip like this (but don't see why it should be thrown at DefWindowProc when the window is known). Symbolic names found (http://wiki.winehq.org/List_Of_Windows_Messages , can't post more links, MSDN has the second parameters under WM_SYSCOMMAND), WinMain is a Visual Studio thing to avoid the console pop up, which some people find alarming.
There is no built-in batch command for running the user's screensaver (without using a 3rd party solution to facilitate it). The code you have come up with is the correct solution for starting the user's screen saver. However, don't use magic number literals like you have shown. There are defines available for those values, use them:
#include <windows.h>
int main(void)
{
SendMessage(GetDesktopWindow(), WM_SYSCOMMAND, SC_SCREENSAVE, 0);
return 0;
}
Compile your code into an .exe file, install it on the user's PC somewhere, and create a shortcut to it on the user's Desktop.
I'm learning C++ and I made a new program. I deleted some of my code and now my console window is not hidden. Is there a way to make it hide on startup without them seeing it?
If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.
If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't require that you have any user interface at all, though. The setting merely controls whether your program starts with a console.
If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?
Assuming you're on windows, configure your linker to make a gui-program, not a console program.
VS: Look in Linker ptions on project properties
LINK: add /SUBSYSTEM:WINDOWS
MinGW: -mwindows
#include <windows.h>
#include <iostream>
using namespace std;
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
int main()
{
cout<<"this sentence is visible\n";
Stealth(); //to hide console window
cout<<"this sentence is not visible\n";
system("PAUSE");
return EXIT_SUCCESS;
}
I used to use ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you no need console, so don't create console app project.
As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
g++ -mwindows winapp.c
it seems that the method
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a
command line console this will be closed as well!
To literally hide/show the console window on demand, you could use the following functions:
It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console.
IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}
You can create your window minimized. Or paint it outside the visible screen.
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).
You can try this
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.
How do you open a path in explorer by code in c++. I googled and couldn't find any thing but systems commands to do this, however, i dont want it to block or show the console window.
You probably are looking for the ShellExecute() function in shellapi.h. It is called with an "action verb", a path, and optional parameters. In your case this will want either "open" or "explore" as follows:
ShellExecuteA(NULL, "open", "C:\\", NULL, NULL, SW_SHOWDEFAULT);
This will open an unattached explorer window at C:. ShellExecute() will give basically the same action as typing in a command at the Run dialog. It will also handle URLs, so the following will open up the user's default browser:
ShellExecuteA(NULL, "open", "http://www.google.com", NULL, NULL, SW_SHOWDEFAULT);
Although make sure to pay attention to the note in the documentation that ShellExecute relies on COM (Although your code doesn't have to worry about any COM objects itself).
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE)
This does not show the command window, just opens the directory.
system("explorer C:\\");
I'm now using VS2017, using as follows works:
ShellExecute(NULL, L"open", L"YourFolderPath\\YourFile.xxx", NULL, NULL, SW_RESTORE);
also reference ShellExecute to open an .exe in C++