How to open a file which has name with unicode symbols - c++

I created an .exe file and associated .myFile extension to that .exe file. I want to double click on any .myFile file and get that file opened by the .exe. For that I have done the following:
int main(int argc, char *argv[])
{
QString fileName(QObject::tr(argv[1]));
if ( fileName != "" )
{
mainWin.loadFile(fileName);
}
..................
}
But when I have named my file in unicode characters (e.g. "Здравствуй.myFile"), the instead of "Здравствуй" you can see "????????". How to solve this problem? I know this is solved problem because, for example, MS Word does that.

The previous answers that focus on int main(int argc, char** argv) are needlessly complex. Qt has a better alternative.
From the Qt documentation: On Windows, the QApplication::arguments() are not built from the contents of argv/argc, as the content does not support Unicode. Instead, the arguments() are constructed from the return value of GetCommandLine().
So, the correct answer is to use qApp.arguments().at(1), which will give you the Unicode filename in a QString.

You have to use wmain instead of main on Windows:
int wmain(int argc, wchar_t** argv) {
QString fileName = QString::fromWCharArray(argv[1]); // untested
If you have to follow the C standard (which is all but useless on Windows), you can use GetCommandLineW and CommandLineToArgvW instead.

Assuming the unicode you pass in is actually stored as UTF-8, try using QString::fromUtf8 to read the argument, something like this:
int main(int argc, char *argv[])
{
QString fileName(QObject::trUtf8(argv[1]));
if ( fileName != "" )
{
mainWin.loadFile(fileName);
}
// stuff
}
QObject::trUtf8 is actually a wrapper that will utilize QString:fromUtf8 and still perform the translation (even though i dont understand why you want to translate file names)

Related

Application is unable to get command argument

I'm unable to get command line argument with int main(int argc, char* argv[]), No errors occurs but when further manipulation want to happen i see that argv[ ] and other related variables dont have any value and says Error reading characters of string then an Acces violation reading location error happens.
This is the sample code of my issue, I had to downsize it to make it readable:
#include "CommonHeaders.h"
void Start(char *input)
{
lstrcpyA(host, input);
// In this point i see in my Debugger "Locals/Autos" that nothing
// is passed to function then a "Access violation ... " happens.
// ...
}
int main(int argc, char *argv[])
{
Start(argv[1]);
return 0;
}
I always use this int main(int argc, char* argv[]) and pass command arg with ProjectProperties->Debugging->Command Argument and works perfect everytime. Is it possible that proper headers aren't included or any changes in project configuration could make a conflict?
Be sure to set the subsystem to console Linker -> SubSytem -> Console (/SUBSYSTEM:CONSOLE) and dont set an Entry point set the Whole Program Optimization to No Whole Program Optimization and turn SDL check to off.

C++ What is my own file name?

I have a piece of C++ code that calls the system command.. I want to pass the file name of my own C++ executable to the system command.. anyone know how to do this?
So for example my C++ code is called "switch-5".. what I want to do is something like;
system("./script.sh switch-5");
Anyone have any clue?
Your executable name is the first argument passed in argv.
To test this just run:
int main(int argc, char **argv)
{
printf("My program name: '%s'\n", argv[0]);
return 0;
}
(I am assuming you know how to combine it with your script name to get the string to pass to system().)
argv[0] as passed to main is the name of your executable.
Store it in a global variable during app startup:
static char *selfname;
int main(int argc, char **argv)
{
selfname = argv[0];
// etc.
}
Beware, argv[0] contains exactly what was used to start the program, including relative or absolute path, for example: "./programname".

cannot convert parameter 1 from 'char' to 'LPCWSTR'

I keep getting this error:
cannot convert parameter 1 from 'char' to 'LPCWSTR'
int main(int argc, char argv[])
{
// open port for I/O
HANDLE h = CreateFile(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(h == INVALID_HANDLE_VALUE) {
PrintError("E012_Failed to open port");
can someone help?
Go to the Properties for your Project and under Configuration Properties/General, change the Character Set to "Not Set". This way, the compiler will not assume that you want Unicode characters, which are selected by default:
It should be
int main(int argc, char* argv[])
And
HANDLE h = CreateFileA(argv[1],GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
This is the main function that Visual Studio creates by default:
int _tmain(int argc, _TCHAR* argv[])
Where _TCHAR is defined to be char or wchar_t depending if _UNICODE is defined or not. The same thing happens with API functions. I would advise you against using explicit CreateFileA. Change your main and use CreateFile.
Depending on your compiler setting for CharacterSet, you may need to perform a multibyte / widechar conversion, or change the CharacterSet if you don't care what it is.
For converting with MultiByteToWideChar, see the following...
http://www.codeguru.com/forum/showthread.php?t=231165
I guess you're compiling with Unicode enabled. Then with char argv[], argv is a char array, so argv[1] is a char, and CreateFile wants a const wchar_t* as first parameter, not a char.
That said, your main definition is also broken, it should have char* argv[]. With that change, you can call CreateFileA.

executable parameter c++

if i got a c++ executable file like this: executable.exe and i want to add some parameters like:
executable.exe +username = pino
how do i get in c++ that i filled in pino as my username?
Arguments to main. Your main is
int main(int argc, char **argv) {
...
}
All the command-line params are in argv. There are also Windows-specific APIs.

Printing all environment variables in C / C++

How do I get the list of all environment variables in C and/or C++?
I know that getenv can be used to read an environment variable, but how do I list them all?
The environment variables are made available to main() as the envp argument - a null terminated array of strings:
int main(int argc, char **argv, char **envp)
{
for (char **env = envp; *env != 0; env++)
{
char *thisEnv = *env;
printf("%s\n", thisEnv);
}
return 0;
}
#include <stdio.h>
extern char **environ;
int main() {
char **s = environ;
for (; *s; s++) {
printf("%s\n", *s);
}
return 0;
}
I think you should check environ. Use "man environ".
Your compiler may provide non-standard extensions to the main function that provides additional environment variable information. The MS compiler and most flavours of Unix have this version of main:
int main (int argc, char **argv, char **envp)
where the third parameter is the environment variable information - use a debugger to see what the format is - probably a null terminated list of string pointers.
LPTCH WINAPI GetEnvironmentStrings(void);
http://msdn.microsoft.com/en-us/library/ms683187%28VS.85%29.aspx
EDIT: only works on windows.
int main(int argc, char **argv, char** env) {
while (*env)
printf("%s\n", *env++);
return 0;
}
int main(int argc, char* argv[], char* envp[]) {
// loop through envp to get all environments as "NAME=val" until you hit NULL.
}
In most environments you can declare your main as:
main(int argc,char* argv[], char** envp)
envp contains all environment strings.
If you're running on a Windows operating system then you can also call GetEnvironmentStrings() which returns a block of null terminated strings.
Most of the answers here point out the possibility to pick the environment from an argument to main supported by most compilers. While Alex' answer:
#include <stdio.h>
extern char **environ;
int main() {
char **s = environ;
for (; *s; s++) {
printf("%s\n", *s);
}
return 0;
}
should work always, I wonder what happens to char **environ when you manipulate the environment in your C code (putenv, unsetenv). Then environ may point to somewhere else (when it was reallocated, may depend on the system implementation). If we stick to a parameter passed to main and pass it on to the function requiring it, this pointer may not point to the current environment any more.
More or less portable C code solution seems for me as follows:
#include <stdlib.h>
void printenv() {
char ** env;
#if defined(WIN) && (_MSC_VER >= 1900)
env = *__p__environ();
#else
extern char ** environ;
env = environ;
#endif
for (env; *env; ++env) {
printf("%s\n", *env);
}
}
Explanations:
Tested successfully on Linux, Windows, Solaris, AIX.
Tested successfully on new versions of Visual Studio as well. The point is that since at least VS 2017 (probably earlier) the environ global variable is not recognized anymore. If you open the header C:\Program Files\Windows Kits\10\Include\x.x.x.x\ucrt\stdlib.h you will see that this global variable was replaced with the function __p__environ(). Unfortunately it is not documented well. No word about it in https://learn.microsoft.com/en-us/cpp/c-runtime-library/environ-wenviron?view=msvc-170.
The advantage of this approach is that it is also appropriate if you are not allowed to modify the main() function adding there envp[].
Regarding GetEnvironmentStrings(), it returns me an empty list. Probably it works for C++ and not for C. I did not investigate it.