How can i overload the WinMain()
function while using Unicode character set in Visual Studio? Thank you.
Asked
Active
Viewed 2,419 times
-2

Anon
- 1,274
- 4
- 17
- 44
-
Overload `WinMain()`? what? why? That seems Bad. – ssube Dec 31 '11 at 00:23
-
im trying to write a game engine, and, as far as i know, its a common practice. – Anon Dec 31 '11 at 00:26
-
For what purpose? What do you want to do? I can think of no practical use for this, considering it's called only once as the entry point. Maybe you're looking for `_tWinMain` which is the TCHAR version in case you want to compile as either Unicode or ANSI char? – tenfour Dec 31 '11 at 00:37
-
2Use wWinMain(), _tWinMain() is from the previous century. – Hans Passant Dec 31 '11 at 00:44
-
@Abdulali: The common practice is to declare `int WINAPI WinMain(HINSTANCE, HINSTANCE, LPTSTR, int)` and `#define`ing `UNICODE` before `#include`ing `windows.h` if you want to work with Unicode. There's no need to write separate non-Unicode and Unicode `WinMain()`s, and you can't anyway. – In silico Dec 31 '11 at 01:33
1 Answers
4
There is no point in overloading WinMain, this function is called only once at startup. If you want it to work in UNICODE and non UNICODE builds then you should make suse that it will accept wchar_t* and char* for pszCmdLine parameter:
int WINAPI _tWinMain(
HINSTANCE hInstanceExe,
HINSTANCE,
PTSTR pszCmdLine,
int nCmdShow);

marcinj
- 48,511
- 9
- 79
- 100
-
-
3@Abdulali : No, it can't. The Unicode entry point is named `wWinMain`; you can't overload two functions with different names. The `_tWinMain` merely expands to `WinMain` or `wWinMain` according to whether or not the `UNICODE` macro is defined. – ildjarn Dec 31 '11 at 00:38
-
I have working code with overloaded `WinMain()` function that works perfectly well. However once i import it into VS2010, it crashes. Ive looked it up on MSDN and found out that the reason is the `lpCmdLine` parameter of the WinMain(). however MSDN provides no sample code. – Anon Dec 31 '11 at 00:42
-
-
-
-
change it to PTSTR, LPSTR results always to CHAR *, and on UNICODE you want WCHAR*. PTSTR will be CHAR* on non-UNICODE, and WCHAR* on UNICODE builds. – marcinj Dec 31 '11 at 00:54
-
@luskan i just tried it, still wont compile. gives me error `C2731: 'WinMain' : function cannot be overloaded` again – Anon Dec 31 '11 at 01:01
-
probably you have WinMain duplicate somewhere in your code, or in any libraries you are including – marcinj Dec 31 '11 at 01:09
-
@Abdulali You're getting that error because it's not possible to overload WinMain. There can only be one. – ssube Dec 31 '11 at 01:15
-
"I know it CAN be overloaded". What makes you think that, exactly? Provide more details. – Remy Lebeau Dec 31 '11 at 01:17
-
no, i have no duplicates. just function member that catches: `(HANDLE hInstance, LPCTSTR lpCmdLine, int iCmdShow)`. – Anon Dec 31 '11 at 01:17
-
1
-
@Remy Lebeau - TeamB http://www.gameinstitute.com/courses.php?coursedisplay=49. the code is in this book. – Anon Dec 31 '11 at 01:19
-
1@Abdulali: No one here is willing to pay $215.00 just to verify what you read. And when writing questions on Stack Overflow in the future, just paste the code in the question and not link it to some kind of file dropbox service. – In silico Dec 31 '11 at 01:30
-
1@Abdulali: And I don't see any `WinMain()` "overloading" in the code you provided, so I don't even understand the premise of your question. The point still stands, you can't have two `WinMain()` functions, even if they have different names. The way you write a `WinMain()` function is to have a function [with this exact signature](http://msdn.microsoft.com/en-us/library/windows/desktop/ms633559.aspx) (ignore the `__in`s), and if you want Unicode you define `UNICODE` before including `windows.h`. That's it. – In silico Dec 31 '11 at 01:37
-
1If they have different names (like `WinMain()` and `wWinMain()` do), then by definition they are not overloads of each other. Overloads must have exact same name, and they have to both exist in the executable at the same time (`WinMain()` and `wWinMain()` do not coexist - an app has one or the other, not both). – Remy Lebeau Dec 31 '11 at 01:45