Help with WinMain()

Hello, I need to use WinMain() instead of main in order to utilize SW_HIDE function that it offers so I dont have to see a flashing console every time I start up my program. Problem is, is that I am having trouble with the WinMain function. The program I am building does not need anything like LRESULT CALLBACK, for windows, it is essentially a console application employing the SW_HIDE function of WinMain().

How come this does not work ?
INT WINAPI WinMain(NULL,NULL,NULL,SW_HIDE)
{
//some random thing here. eg.
std::ofstream file;
file.open("something.txt");
file << "hello";
file.close();
return 0;
}
Why do you think it doesn't work?
If you're developing a win32 project, you're working with windows.
If you're working with windows, that means that you need to do what is required to have them function.

The bare bones of a window require you to register a WNDCLASS struct, whose attributes you modify to your liking.
You then need to create the window, and begin a message queue loop. You'll need a "WndProc" callback function to handle messages (a pointer to this function is also required in the WNDCLASS struct).


so I dont have to see a flashing console every time I start up my program


What exactly do you mean? You see, if you could describe your problem in more detail, maybe one of us can give you a "console" oriented solution.
Basically my program is a console program, however I want to run it in the background everyday without having to see a brief moment of a console appearing with what I get when using ShowWindow. alas I try to use WinMain() so that it becomes a win32 program instead of a console one, and windows doesn't automatically generate a console each time it starts up. However this program does not need to communicate with windows to function. what are your recommendations ?

I have also heard about doing this by somehow setting the program as a system function. If this is possible, then how would I do so? (by using c++ of course, not manually!) would I be able to get a very bare bone sample code
Last edited on
To be honest, the following is about as bare boned as it can get:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <windows.h>

HINSTANCE hinst;
HWND hwnd;
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);

int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
	MSG msg;
	WNDCLASS wc;

	memset(&wc, 0, sizeof(WNDCLASS));
	wc.style = wc.cbClsExtra = wc.cbWndExtra = 0;
	wc.lpfnWndProc = (WNDPROC)WndProc;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon((HINSTANCE)NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor((HINSTANCE)NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wc.lpszMenuName = L"win32_menu_unique";
	wc.lpszClassName = L"win32_class_unique";
	if(!RegisterClass(&wc)) {return 0;}
	hinst = hInstance;
	hwnd = CreateWindow(wc.lpszClassName, L"title", WS_OVERLAPPED | WS_SYSMENU, CW_USEDEFAULT, CW_USEDEFAULT, 640, 480, (HWND)NULL, (HMENU)NULL, hinst, (LPVOID)NULL);
	if(!hwnd) {return 0;}

	ShowWindow(hwnd, SW_HIDE);//in your case, using nCmdShow would be counter productive for obvious reasons.
	UpdateWindow(hwnd);

	while(GetMessage(&msg, NULL, 0, 0) > 0) {
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	return msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
	switch(uMsg) {
		case WM_CLOSE:
			DestroyWindow(hwnd);
			break;
		case WM_DESTROY:
			PostQuitMessage(0);
			break;
		default:
			return DefWindowProc(hwnd, uMsg, wParam, lParam);
	}
	return 0;
}


In order to use ShowWindow(), you need a HWND handle.
To get a handle, you need to create a window by using CreateWindow().
To create a window, you need to register a window class by using RegisterClass().
To register a window class, you'll need to modify a WNDCLASS struct.

You also need a message queue, even if your program is not going to
communicate with windows to function
, because you need to be able to handle closing and destroying messages.
Last edited on
is closing and destroying messages those utilized in the program during start up and ending ?

also in your sample code, where would say cout << "helloworld" go ?
Last edited on
Not quite. WM_CLOSE and WM_DESTROY are both used when the window is killed. WM_CLOSE is the command sent by the user (or possibly some process), which indicates that the window should begin destructing itself. Right before when the WM_DESTROY message is sent is the point at which the program might ask the user if they want to save their work, etc.

As for your second question; You cannot use std::cout or any of the IO console stream objects, because obviously there's no console.
I think you may want to actually learn more about what it is you're trying to do. To me it looks like you're trying to "write a virus" or some such program.
actually its an updater for a game im making, and I just need it to run in the background without giving a pop up everytime. I didnt want to attach the updater to the actual game itself because obviously it would cause lag and inconvenience the player. any suggestions ? Is there a way I can set the console application as a system service so even though its console, the user wont see it running?
Topic archived. No new replies allowed.