How can one hide the console window of a Windows program? - c++

I am using the wxWidget framework. When I set System/Subsystem: Console (/SUBSYSTEM:CONSOLE) then my program will start but will show both a GUI and a console. This is how it looks: https://i.stack.imgur.com/G83PR.png
When I change System/Subsystem: Windows (/SUBSYSTEM:WINDOWS) then it show errors:
LNK2019 unresolved external symbol _WinMain#16 referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
This is my code:
int main(int argc, char** argv) {
Gui_AutoPokemon* gui = new Gui_AutoPokemon();
wxApp::SetInstance(gui);
mainArgc = argc;
mainArgv = argv;
return wxEntry(argc, argv);
}
How can I hide the console window?

GUI Windows programs use a different entry point function than console. You're right to use /SUBSYSTEM:WINDOWS, but then you need to change to using WinMain:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR cmdLine, int nCmdShow) {
....
Then use the appropriate wxEntry overload:
return wxEntry(hInstance, NULL, cmdLine, nCmdShow);

I suggest calling FreeConsole(). It detaches the console from any window. As an example for my application:

Related

WinMain() not receiving enough args if opened by double-clicking the file

I used to be able to double-click files of my custom extension and open it via my C++ exe program (right click-> open with -> my program). The program was correctly receiving args.
Its WinMain() was receiving several args: the first argument was path to exe and the second argument was the path to the clicked file.
However, I no longer seem to be able to receive the second argument, it always launches my program with 1 argument: the path to this program.
Could it be because of the Windows Update I did today? Using Windows 10 and Visual Studio (same happens if compiled in Debug or in Release)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
int nArgs;
LPWSTR *szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs); //GetCommandLine() is a #define located inside processenv.h
//Now nArgs is your argc and szarglist is your argv
//first arg is path to exe, second arg is path to clicked file.
if(nArgs<=1){ LocalFree(szArglist); return EXIT_FAILURE; } //not enough args (1 or less)
LocalFree(szArglist);
return EXIT_SUCCESS;
}
It was because of how I was fetching the arguments.
I was using CommandLineToArgvW(), but when I instead started to use __argv and __argc it started working. Found this answer here
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow){
if(__argc<=1){ return EXIT_FAILURE; } //not enough args (1 or less)
/*use __argv[1] to fetch your second argument (in my case it's filepath) */
return EXIT_SUCCESS;
}

Error LINK2019, something about main? [duplicate]

Error:
Severity Code Description Project File Line Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
Severity Code Description Project File Line
Error LNK1120 1 unresolved externals
Code:
#include "windows.h"
#include "tchar.h"
#include "d3d9.h"
#pragma comment(lib, "d3d9.lib")
LPDIRECT3D9 pDirect3D=NULL;
LPDIRECT3DDEVICE9 pDirect3DDevice=NULL;
const int segment = 50;
const int NV = segment*13;
struct CUSTOMVERTEX
{
float x, y, z, rhv;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
LPDIRECT3DVERTEXBUFFER9 pVertexBuffer=NULL;
HRESULT InitialDirect3D(HWND hvnd)
{
if((pDirect3D=Direct3DCreate9(D3D_SDK_VERSION))==NULL)
return E_FAIL;
D3DDISPLAYMODE Display;
if(FAILED(pDirect3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &Display)))
return E_FAIL;
D3DPRESENT_PARAMETERS Direct3DParameter;
ZeroMemory(&Direct3DParameter, sizeof Direct3DParameter);
Direct3DParameter.Windowed=TRUE;
Direct3DParameter.SwapEffect=D3DSWAPEFFECT_DISCARD;
Direct3DParameter.BackBufferFormat=Display.Format;
if(FAILED(pDirect3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hvnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&Direct3DParameter, &pDirect3DDevice)))
return E_FAIL;
return S_OK;
}
HRESULT RenderingDirect3D()
{
if(pDirect3DDevice==NULL)
return E_FAIL;
pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(90, 150, 100), 1.f, 0);
pDirect3DDevice->BeginScene();
pDirect3DDevice->SetStreamSource(0, pVertexBuffer, 0, sizeof(CUSTOMVERTEX));
pDirect3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
pDirect3DDevice->DrawPrimitive(D3DPT_LINELIST, 0, NV);
pDirect3DDevice->EndScene();
pDirect3DDevice->Present(NULL, NULL, NULL, NULL);
return S_OK;
}
void DeleteDirect3D()
{
if(pVertexBuffer)
pVertexBuffer->Release();
if(pDirect3DDevice)
pDirect3DDevice->Release();
if(pDirect3D)
pDirect3D->Release();
}
HRESULT InitialVertexBuffer()
{
float u = 0.0;
int z = 0;
float points[][2] = {
{150,150},
{125,100},
{150,50},
{200,50},
{225,55},
{285,60},
{325,25},
{342,30},
{340,35},
{340,40},
{325,75},
{300,125},
{300,175},
{305,250},
{295,287},
{257,347},
{200,350},
{150,325},
{140,290},
{142,282},
{160,280},
{165,286},
{175,325},
{215,340},
{250,335},
{275,300},
{275,250},
{255,200},
{250,150},
{275,100},
{305,65},
{275,85},
{240,95},
{215,85},
{170,65},
{140,90},
{160,135},
{160,150},
{152.5,150},
{150,150}
};
CUSTOMVERTEX Vertexes[NV*2];
int i = 0;
while( i < NV*2 )
{
u = 0.f;
for(int j = 0; j < segment; j++)
{
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
u += (float)1/segment;
i++;
Vertexes[i].x = (1-3*u+3*u*u-u*u*u)*points[z][0]+(3*u-6*u*u+3*u*u*u)*points[z+1][0]+(3*u*u-3*u*u*u)*points[z+2][0]+(u*u*u)*points[z+3][0];
Vertexes[i].y = (1-3*u+3*u*u-u*u*u)*points[z][1]+(3*u-6*u*u+3*u*u*u)*points[z+1][1]+(3*u*u-3*u*u*u)*points[z+2][1]+(u*u*u)*points[z+3][1];
Vertexes[i].z = 0.5f;
Vertexes[i].color = 0x00ffffff;
Vertexes[i].rhv = 1.f;
i++;
}
z += 3;
}
if(FAILED(pDirect3DDevice->CreateVertexBuffer(NV*2*sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &pVertexBuffer, NULL)))
return E_FAIL;
void *pVB=NULL;
if(FAILED(pVertexBuffer->Lock(0, sizeof Vertexes, (void**)&pVB, 0)))
return E_FAIL;
memcpy(pVB, Vertexes, sizeof Vertexes);
pVertexBuffer->Unlock();
return S_OK;
}
LRESULT CALLBACK MainWinProc(HWND hwnd,
UINT msg,
WPARAM wparam,
LPARAM lparam)
{
switch(msg)
{
case WM_PAINT:
RenderingDirect3D();
ValidateRect(hwnd, NULL);
break;
case WM_DESTROY:
DeleteDirect3D();
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
int WINAPI WinMain(HINSTANCE hinstance,
HINSTANCE hprevinstance,
LPSTR lpcmdline,
int ncmdshow)
{
WNDCLASSEX windowsclass;
HWND hwnd;
MSG msg;
//
windowsclass.cbSize=sizeof(WNDCLASSEX);
windowsclass.style=CS_DBLCLKS|CS_OWNDC|CS_HREDRAW|CS_VREDRAW;
windowsclass.lpfnWndProc=MainWinProc;
windowsclass.cbClsExtra=0;
windowsclass.cbWndExtra=0;
windowsclass.hInstance=hinstance;
windowsclass.hIcon=LoadIcon(NULL, IDI_APPLICATION);
windowsclass.hCursor=LoadCursor(NULL, IDC_ARROW);
windowsclass.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);
windowsclass.lpszMenuName=NULL;
windowsclass.lpszClassName=_T("WINDOWSCLASS");
windowsclass.hIconSm=LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&windowsclass))
return 0;
if(!(hwnd=CreateWindowEx(
NULL,
_T("WINDOWSCLASS"),
_T("Desenam litera G"),
WS_OVERLAPPEDWINDOW|WS_VISIBLE,
CW_USEDEFAULT, 0,
CW_USEDEFAULT, 0,
NULL,
NULL,
hinstance,
NULL)))
return 0;
if(SUCCEEDED(InitialDirect3D(hwnd)))
{
if(SUCCEEDED(InitialVertexBuffer()))
{
ShowWindow(hwnd, SW_SHOWDEFAULT);
UpdateWindow(hwnd);
ZeroMemory(&msg, sizeof msg);
while(msg.message!=WM_QUIT)
{
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
RenderingDirect3D();
}
}
}
return msg.wParam;
}
Check project configuration. Linker->System->SubSystem should be Windows.
If you use CMake you have to set WIN32 flag in add_executable
add_executable(${name} WIN32 ${source_files})
See CMake Doc for more information.
I had same Problem when i was trying to create executable from program that having no main() method. When i included sample main() method like this
int main(){
return 0;
}
It solved
Right click on project. Properties->Configuration Properties->General->Linker.
I found two options needed to be set.
Under System: SubSystem = Windows (/SUBSYSTEM:WINDOWS)
Under Advanced: EntryPoint = main
I faced the same problem too and I found out that I selected "new Win32 application" instead of "new Win32 console application". Problem solved when I switched. Hope this can help you.
just add:
int main()
{
//you can leave it empty
return 0;
}
to your project and everything will work
its causes because of visual studio try to find the main function to start the project and didn't found it
Similar to #仲耀晖 I had the wrong application type configured for a dll. I guess that the project type changed due to some bad copy pasting, as #Daniel Struhl suggested.
How to verify:
Right click on the project -> properties -> Configuration Properties -> General -> Project Defaults -> Configuration Type.
Check if this field contains the correct type, e.g. "Dynamic Library (.dll)" in case the project is a dll.
This is an edge case, but you can also get this error if you are building an MFC application with CMake.
In that case, you need to add the following definitions:
ADD_DEFINITIONS(-D_AFXDLL)
SET(CMAKE_MFC_FLAG 2) # or 1 if you are looking for the static library
If you are compiling with unicode, the following properties also need to be added:
set_target_properties(MyApp PROPERTIES
COMPILE_DEFINITIONS
_AFXDLL,_UNICODE,UNICODE,_BIND_TO_CURRENT_CRT_VERSION,_BIND_TO_CURRENT_MFC_VERSION
LINK_FLAGS "/ENTRY:\"wWinMainCRTStartup\""
)
Soure: FAQ: How to use MFC with CMake
Select the project. Properties->Configuration Properties->Linker->System.
My problem solved by setting below option.
Under System: SubSystem = Console(/SUBSYSTEM:CONSOLE)
Or you can choose the last option as "inherite from the parent".
This worked for me:
(I don't have enough rep to embed pictures yet -- sorry about this.)
I went into Project --> Properties --> Linker --> System.
IMG: Located here, as of Dec 2019 Visual Studio for Windows
My platform was set to Active(Win32) with the Subsystem as "Windows".
I was making a console app, so I set it to "Console".
IMG: Changing "Windows" --> "Console"
Then, I switched my platform to "x64".
IMG: Switched my platform from Active(32) to x64
Solution for me is: I clean both the solution and the project. And just rebuild the project.
This error happens because I tried to delete the main file (only keep library files) in the previous build so at the current build the old stuff is still kept in the built directory. That's why unresolved things happened.
"unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ) "
I had to #include <tchar.h> in my windows service application.
I left it as a windows console type subsystem.
The "Character Set" was set to UNICODE.
I deleted the file with main() function in my project when I wanted to change project type to static library.
I Forgot to change Properties -> General -> Configuration Type
from
Application(.exe)
to
Static Library (.lib)
This too gave me the same error.
Old thread but for me it started working (after following all advise above) when i renamed int main(void) to int wmain(void) and removed WIN23 from cmake's add_executable().
If it is a windows system, then it may be because you are using 32 bit winpcap library in a 64 bit pc or vie versa. If it is a 64 bit pc then copy the winpcap library and header packet.lib and wpcap.lib from winpcap/lib/x64 to the winpcap/lib directory and overwrite the existing
The problem might originate from a macro instruction in SDL_main.h
In that macro your main(){} is renamed to SDL_main(){} because SDL needs its own main(){} on some of the many platforms they support, so they change yours. Mostly it achieves their goal, but on my platform it created problems, rather than solved them. I added a 2nd line in SDL_main.h, and for me all problems were gone.
#define main SDL_main //Original line. Renames main(){} to SDL_main(){}.
#define main main //Added line. Undo the renaming.
If you don't like the compiler warning caused by this pair of lines, comment both lines out.
If your code is in WinApp(){} you don't have this problem at all. This answer only might help if your main code is in main(){} and your platform is similar to mine.
I have: Visual Studio 2019, Windows 10, x64, writing a 32 bit console app that opens windows using SDL2.0 as part of a tutorial.
I got the same error because I created a new object from a templated class using the template name without specifying the type explicitly like this:
int main()
{
MyClass<T> Test2(5.60, 6.6); <- This is wrong
^^^
return 0;
}
The correct way to do it was to specify exactly what T was like this:
int main()
{
MyClass<double> Test2(5.60, 6.6); <- This is right
^^^^^^
return 0;
}
for me adding .dll file to my project folder solved this error.
but game me another error: "unresolved external symbol main referenced in function ..." and I solved this by following this answer. https://stackoverflow.com/a/50087608/10903596 or in short adding #define SDL_MAIN_HANDLED in my main file or file containing main function.
You can get this error, if you have your main entry point enclosed in a namespace.
So, don't put your main entry point in a namespace, leave it at the global level.
I wanted to start working with SDL library. After setting and checking the include and library paths for numerous times, I came across the answer by Rinus Verdult here. I added this line after the #include lines and it worked.
#define main main
I have visual studio 2019, windows 10 64bit.

Open TDecoratedMDIFrame Maximized

I'm writing an application with Embarcadero RAD 2 Studio (borland) and the owlNext library.
There I create a TDecoratedMDIFrame Object, which I open by using
class TMDIApp : public owl::TApplication { // -------------------------------
public:
TMDIApp() : owl::TApplication(ProgName) {}
TMDIApp(const char far* name, HINSTANCE instance, HINSTANCE prevInstance, const char far* cmdLine, int cmdShow) : owl::TApplication(name, instance, prevInstance, cmdLine, cmdShow) {}
void InitMainWindow();
};
void TMDIApp::InitMainWindow() { // --------------------------
PMAcMDI = new TMAcMDI;
PMW = new TDecoratedMDIFrame(ProgName, MAC_MENU, *PMAcMDI);
MainWindow = PMW;
}
int OwlMain(int, char* []) { // ------------------------
return TMDIApp().Run();
}
So, in the end it get's opened by aplicat.cpp of the owlnext library using MainWindow->ShowWindow(nCmdShow);.
However, Now I want to open it maximized.
I made some research and came upon
PMW->WindowProc(WM_SIZE,SIZE_MAXIMIZED,0);
Which doesn't seem to do anything...
Does anyone of you know how to maximize this window or starting it maximized?
I found information about this problem at an old archive.org copy of the "OWL Programmer's Guide"
http://archive.org/stream/bitsavers_borlandborindowsforCVersion2.0ProgrammersGuideOct9_19152845/Borland_ObjectWindows_for_C_Version_2.0_Programmers_Guide_Oct93_djvu.txt
There it says:
~ ~ You can change how your application's main window is displayed by
Specifying the mam setting the T Application data member nCmdShow,
which corresponds to the ^ / WinMain parameter nCmdShow. You can set
this variable as soon as the Run
function begins, up until the time you call T Application:
:lnitlnstance. This effectively means you can set nCmdShow in either
the InitApplication or InitMainWindow function.
For example, suppose you want to display your window maximized
whenever the user runs the application. You could set nCmdShow in your
InitMainWindow function:
include ttinclude
class TMyApplication : public TApplication { public :
TMyApplication (char far *name) : TApplication (name) {}
void InitMainWindow () ;
1; ■ ■
void TMyApplication: : InitMainWindow () {
SetMainWindow(new TFrameWindow ( , "Maximum Window") ) ; nCmdShow =
SW_SHOWMAXIMIZED;
} int '
OwlMain(int argc> char* argv[]) {
, return TMyApplication("Wow!").Run(); } '
nCmdShow can be set to any value appropriate as ( a parameter to the
ShowWindow Windows function or the TWindow::Shozv member function,
such as SW_HIDE, SW_SHOWNORMAL, SW_NORMAL, and so on.
So, in my case, adding nCmdShow = SW_SHOWMAXIMIZED; inside my InitMainWindow () definition was enough.

Unhandled Exception - Access violation if /SUBSYSTEM parameter is changed

I have an application, in which I use a console to see some of the values being output. Now some of the requirements have changed, and I do not need the console during runtime anymore.
I tried to change that, by toggling the /SUBSYSTEM parameter, found under Project Properties->Linker->System->Subsystem from Console to Windows, as I'd done the same for an earlier thing, and it had worked.
On this occasion, it gives me an Unhandled Exception, in mfc110u.dll, as the object cannot be instantiated.
Why does this exception occur, and how else can I turn off the console with the running program? I'm using VS2012 as the dev environment.
If you don't want a console, declare a winmain. This is the non-unicode version
#include <windows.h>
#include <iostream>
#include <cstdio>
int main (int, char**);
// If we just start with main, we will always get a console window
int WINAPI WinMain (
HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow
)
{
int argc = __argc;
char** argv = __argv;
#ifdef DEBUG
// If we are running in debug mode, open a console window
AllocConsole();
freopen("conin$", "r", stdin);
freopen("conout$", "w", stdout);
freopen("conout$", "w", stderr);
#endif
return main (argc, argv);
}
int main (
int argc,
char** argv
)
{
MessageBox(NULL, "Whoo hoo", "It Works!!!", MB_OK);
return 0;
}

I've created a new console in win32 window-only app, console is created but nothing gets print on it

this code is compiled with -mwindows under gcc , there is no winapi error message.
#include <windows.h>
#include <stdio.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPTSTR, int nCmdShow) {
AllocConsole();
printf("%s\n", "sample text");
return 0;
}
Result is that console is black empty, no text, no error message.
Use:
freopen("CONOUT$", "wb", stdout);
to reopen the stdout after you have created the console. If you plan on using it for input too, then you need:
freopen("CONIN$", "rb", stdin);
and stderr may need opening too:
freopen("CONOUT$", "wb", stderr);