Windows unicode commandline argv - c++

So getting into the new millenia I rewrote my c++ code with:
int main(int argc, wchar_t **argv)
If built with either Unicode or MBCS options then when the app is run with a commandline arg, either directly or by dbl-click the filenames passed to argv[] are unreadable = in some mixture of chinese fonts.
Thanks for the comments - I will try and summaris(z)e them here for the search engine.
wmain(int argc,char **argv) can only be used for a commandline (subsystem:console) app
int winMain(int argc, wchar_t **argv) works for a gui (subsystem:windows) but the gui replaces it with it's own entry point. In the case of Qt this doesn't work
qtmaind.lib(qtmain_win.obj) : error LNK2019: unresolved external symbol _main referenced in function _WinMain#16
The solution seems to be use main(int arc,char **argv)
or main(int argc,wchar_t**argv) but ignore the argv. Then call QApplication with argv or NULL - the argv is ignored as Qt internally calls GetCommandLine().
Then use app.arguments to return the parsed arguments.
These can then be converted back into wchar with Qt's string functions if needed.
QApplication app(argc, (char**)argv); or QApplication app(argc,NULL);
QStringList args = app.arguments();
Sorry I didn't originally flag this Qt because I didn't think that was relevant.
If somebody wants to edit this to also include how to do this in MFC - please do.

You need to name your entry point wmain: http://msdn.microsoft.com/en-us/library/fzc2cy7w(VS.80).aspx

Try this:
#include <tchar.h>
int _tmain( int argc, TCHAR **argv )
{
return 0;
}
_tmain is defined as wmain when compiled with the UNICODE option and as main when compiled with the MBCS option.

You can use GetCommandLine function for that purpose.

Related

Multiple definition of main Error

when I compile my project, I get the following error.
error:Multiple definition of main()
what is the problem?
Here is the code:
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
myserver server;
server.startserver();
return a.exec();
}
That means in your program you have at least two functions named main. Search for main in your source files and eliminate one or more (by renaming/refactoring for example).
You can have only one function called main in a C/C++ program.

qt, using freeglut, error with 'glutInit'

I've been trying to use freeglut in a Qt project. Unfortunately when I use some glut function like 'glutWireSphere' or 'glutWireTorus' I get an error:
freeglut ERROR: Function called without first
calling 'glutInit'.
And when I try to run an application it immediately quits itself.
I don't know where exactly should I call 'glutInit'. I've installed freeglut according to this tutorial:
https://www.youtube.com/watch?v=M4fm-cHGoYU&index=1&list=LLkYBBRyDu3gfOojsRQOM3JQ
I've figured it out. It was quite simple actualy. I needed to use 'glutInit( & argc, argv )' in my main.cpp like this:
int main(int argc, char *argv[])
{
glutInit( & argc, argv );
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}

Main method is different to those in tutorials

So I am starting with c++ (i am trying to broaden my mind with new languages) but I came across a little issue which confuses me more than what I guess it should...
Using Visual Studio Express 2012, I created a console win32 application in C++ and this is my main method decleration:
// TestApp.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
return 0;
}
however, since I dont know anything about c++, I searched for some tuts online and all of them had there declerations setup in a different manner
#include <iostream>
using namespace std;
int main()
{
cout<<"HEY, you, I'm alive! Oh, and Hello World!\n";
cin.get();
}
and
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
}
I tried typing in the "std::cout", but it wouldnt accept it,
could someone just clarify why and the significance of the difference ?
The main method can be define with or without parameters. It all depends upon what you are using your application for.
Take a look at this: https://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fmainf.htm
Also for your program you need to have a return value
// my first program in C++
#include <iostream>
int main()
{
std::cout << "Hello World!";
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
is (at least I think so) a Windows only library and compiler depending way of declaring the main function.
Definitly not wrong is to declare main like this:
int main(int argc, char const *argv[])
{
//do something
return 0;
}
or like this:
int main()
{
//do something
return 0;
}
This is definitly proper C++ and you can use this universially.
C++ programs may have one of two beginnings:
int main(int argc, char *argv[])
or
int wmain(int argc, wchar_t *argv[])
The first of these gets its arguments (argv) as ANSI charachers, while the second gets "wide" characters -- generally UTF-16 or UTF-32, depending on the platform.
Microsoft define a framework to allow you make code that can compile with either ANSI or wide characters.
int _tmain(int argc, TCHAR *argv[])
Behind the scenes, they have something like this:
#if defined UNICODE
#define _tmain wmain
#define TCHAR wchar_t
#else
#define _tmain main
#define TCHAR char
#endif
They also have helper functions like _tprintf() and _tcscpy().
NOTE: as pointed out by others, the argc and argv params are optional, so you can also have
int main()
and
int wmain()
and (for Microsoft and compatible compilers)
int _tmain()
Also note that while _tmain() is not strictly portable, you can easily create your own #define macros if you want to be portable to other platforms.

Different output between release and Debug

I can't figure this one out. I have a c++ Application that works in Debug mode exactly as expected:
#include "stdafx.h"
#include <string>
#include <Windows.h>
#include <iostream>
using namespace std;
void truncateServer(std::string inString);
int _tmain(int argc, char *argv[])
{
char* server = argv[1];
truncateServer(server);
}
void truncateServer(std::string inString)
{
std::string server = "";
int whackCount = 0;
for (unsigned int i = 0; i < inString.length(); i++)
{
char c = inString[i];
if (whackCount < 3)
{
if (c == '\\') whackCount++;
else
server += c;
}
}
cout << server;
}
For example if I call the server I want via its UNC path \\serverName\Share\ in the debug it gives me exactly what I want: servername
However, if I use the release build I get nothing:
I deleted the release output folder, but the issue is exactly the same. I can only assume there is some other difference between the release and build applications that is exposing a major issue with my code? Or another difference between the outputs I need to account for.
What do I need to do to get the expected output?
It looks like your Debug build is set as Ansi and your release build as Unicode.
The _tmain declaration is a Visual Studio specific macro which changes the entry point of your application depending on the used charset.
For ANSI it maps to int main(int argc, char *argv[]) .
For Unicode it maps to int wmain(int argc, wchar_t *argv[]).
By using the char type as parameter to _tmain, you cause the compiler to use the wrong type when using an Unicode build, and so end up with a '\0' character as first byte, which std::string can't handle.
In your case, I recommend sticking to int main(int argc, char *argv[]) as it will work in all cases, especially with std::string which use chars.
Also, it is more portable across compilers and operating systems.

int _tmain(int argc, _TCHAR* argv[]) [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
What is the difference between _tmain() and main() in C++?
what is the difference between int _tmain(int argc, _TCHAR* argv[]) and int main(int argc, char** argv)?
I am not clear of the difference.
_tmain is the Microsoft-specific wrapper around "main()". You can use it with either 8-bit ASCII or 16-bit Unicode. Here's the MS documentation for it:
http://msdn.microsoft.com/en-us/library/6wd819wh%28v=vs.80%29.aspx
You can also use _tmain, which is defined in TCHAR.h. _tmain will
resolve to main unless _UNICODE is defined, in which case _tmain will
resolve to wmain.
_tmain is the unicode version of main. I think this is a MS only extension though.