How to run a C++ code, without console ?
ShowWindow(GetConsoleWindow(), SW_HIDE); hide the window, but after it apear.
Before run the program, can I put a line witch hide completly console ?
You can set this pragma inside the file where main method is located, on top of your header files includes:
#pragma comment(linker, "/SUBSYSTEM:windows /ENTRY:mainCRTStartup")
This can also be done with linker options:
/SUBSYSTEM:windows
/ENTRY:mainCRTStartup
As alternative, in VS, change the project subsystem to Windows (/SUBSYSTEM:WINDOWS) in Project Properties-Linker-System-Subsystem. If you do that, use the WinMain signature instead of main signature:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
// Your code here.
return 0;
}
Related
I am trying to call InitCommonControlsEx() in the main entry of a window program, and although I included the header and linked to the ComCtl32.Lib, for some reason the compiler underlines that function as undefined. Following is the code...
#include <CommCtrl.h>
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
INITCOMMONCONTROLSEX initControls;
initControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
initControls.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&initControls);
}
I know that the inclusion of the is doing something, because if I delete the inclusion of that header, then the compiler doesn't recognise the class INITCOMMONCONTROLSEX.
I am really confused. I went into the CommCtrl.h file and found that many sections were underlined in red giving warnings by the compiler, including any reference to WINAPI. Could it be that I have a broken header? I know the functions declaration is in there, it's in the following picture...
I've been googling for hours and can't figure it out. There's not a DLL file I also need, is there? As I said I've already linked the ComCtl32.Lib, sometimes referred to as comctl32.lib (I don't think the capitals matter). Furthermore, on my computer I have 4 instances of this ComCtl32.Lib, two are for 64bit and two are for x86. But, my feeling is that the problem is it just can't find declaration of that function from that header. Thank you.
Edit: Thanks to Harry Johnson I fixed the problem. I had no idea that including standard header files requires that you include other header files before them. In this case the compiler didn't recognise anything because windows.h wasn't included BEFORE CommCtrl.h. An obvious mistake for anyone who knows what they're doing. You can delete this question entirely. I expected that when including a header that windows provides that all relevant dependencies are included in that header.
If you see LNK2019 in Visual Studio 2022, add the #pragma line.
#include <CommCtrl.h>
#pragma comment( lib, "comctl32.lib" )
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
{
INITCOMMONCONTROLSEX initControls;
initControls.dwSize = sizeof(INITCOMMONCONTROLSEX);
initControls.dwICC = ICC_BAR_CLASSES;
InitCommonControlsEx(&initControls);
}
First of all, sorry if the title is not very well chosen, but I don't know how to deal with this code showing something strange when compiled as C using Visual Studio 2013.
The code is:
#include <windows.h>
#include <stdafx.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
MessageBox(NULL, "Hello World!", "Note", MB_OK);
return 0;
}
And, the result is :
I have MS Visual Studio 2013 Original Genuine from MSDN.
Also, I'm not Chinese. The interface of IDE is in English.
I'm not entirely sure how you got that to compile but you're most likely in the wrong character set for that project.
Try:
MessageBox(NULL, L"Hello World!", L"Note", MB_OK);
If you're not intending to work in that character set you should change it as well.
Right-click on the project and go to Properties.
Configuration.
General.
Character set.
I'm writing some simple win32 stuff and I'm using the following wWinMain
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
PWSTR pCmdLine, int nCmdShow)
VS2012 express code analysis says
C28251 Inconsistent annotation for function: this instance has an error Inconsistent annotation for 'wWinMain': this instance has no annotations. See c:\program files (x86)\windows kits\8.0\include\um\winbase.h(2201). LeesSpaceShip main.cpp 6
I'm not clear on what an annotation even is. So what is an annotation and which part is wrong on my code?
Apart from this diagnostic the code compiles and runs just fine.
The declaration of wWinMain in winbase.h (mentioned in the error) is:
wWinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nShowCmd
);
Your implementation of wWinMain in main.cpp is missing the SAL annotations and Code Analysis is warning you about the mismatch.
I'm running this code, it's a simple code to lock Windows 8 screen on a Dell that has a button that can be assigned to a .exe, but it shows a cmd window before locking, how can I launch the .exe without showing the window?
Compiling using Visual Studio Dev Command Prompt
Command Line:
cl lockscreen.cpp
Code:
#include <string>
#include <Windows.h>
using namespace std;
int APIENTRY WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int cmdShow)
{
system("rundll32 user32.dll,LockWorkStation");
return 0;
}
Link against user32.dll (user32.lib actually, the import library, but you should already be doing that if this is a stock win32 project) and just invoke LockWorkstation directly. If you must, LoadLibrary() + GetProcAddress() + etc.. There is no need for you to invoke a rundll call for this to work.
Basically I agree with WhozCraig, but if there is a good reason to make the call via an external exe, try calling it with start /b.
You don't need to compile any code at all. Just connect your special keyboard button to
rundll32.exe user32.dll,LockWorkStation
I am making a win32 application using visual studio 2008.
When ever i compile my code which generates a simple dialog, all of the dialogs text show in Chinese language. I have not set Chinese anywhere. can some one elaborate what the issue is?
My code is
#include <windows.h>
int WINAPI WinMain(
HINSTANCE nowInstance,
HINSTANCE prevInstance,
LPSTR ipCmdLine,
int nCmdShow
)
{
MessageBox(NULL,"My First Program","Our University",MB_OK);
return 0;
}
It sounds like you're mixing Unicode and ANSI.
Have you tried
MessageBox(NULL, _T("My First Program"), _T("Our University"), MB_OK);
And does that give you the expected results?