CaptainProg
CaptainProg

Reputation: 5690

C++ WIN32: Running a program without a command prompt window

I have written a program which triggers a relay switch on a serial port. The relay is closed for 10ms, after which the program closes. However, the program insists on running in a small command prompt window. I would like the program to run without stealing focus; either by running in the background or, even better, without opening a window at all.

Here is the complete program:

#include <windows.h>

//Initialise Windows module
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument, int nFunsterStil)
{
 //Define the serial port precedure
 HANDLE hSerial;

 //Open the port
 hSerial = CreateFile("COM1",GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, 0);

 //Switch on relay
 EscapeCommFunction(hSerial, SETDTR);

 //Wait 10ms
 Sleep(10);

 //Switch off relay
 EscapeCommFunction(hSerial, CLRDTR);

 //Close the port
 CloseHandle(hSerial);

 //End with error code 0
 return 0;
}

What must I change in order to prevent it running in a window?

Upvotes: 2

Views: 7688

Answers (2)

Jeremiah Gowdy
Jeremiah Gowdy

Reputation: 5622

What type of project did you create? If you selected console application, the compiler is doing it. Make an empty Win32 application with the above source. No window should be created. If it is, consider how you're launching the application (start, cmd /c, etc)

Upvotes: 0

AlefSin
AlefSin

Reputation: 1095

Try adding

#pragma comment(linker, "/SUBSYSTEM:WINDOWS")

If that does not work try to hide the window manually:

HWND hWnd = GetConsoleWindow();
ShowWindow( hWnd, SW_HIDE );

Upvotes: 5

Related Questions