WinMain and argv/argc
-
Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; }
And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance! -
Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; }
And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance!I don't quite understand your question, but have you taken a look at
WinMain()
's third parameter,lpCmdLine
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
I don't quite understand your question, but have you taken a look at
WinMain()
's third parameter,lpCmdLine
?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
Im trying to get the current file name from argv using WinMain(). But since i cant put argv[] or argc in the function arguments, i have to use another method. Thats what im saying
-
Im trying to get the current file name from argv using WinMain(). But since i cant put argv[] or argc in the function arguments, i have to use another method. Thats what im saying
dellthinker wrote:
Im trying to get the current file name from...WinMain().
So how about
GetModuleFileName()
?dellthinker wrote:
But since i cant put argv[] or argc in the function arguments, i have to use another method.
And that way is fine. There's nothing wrong with using
__argv[0]
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi all, im trying to make an application that requires me to use the API WinMain() and it just so happens i need to use argv and argc. Problem is that WinMain takes 4 arguments in it and doesnt have room for argc/argv[]. So i looked up the issue and as it turns out MS didnt allow the usage for whatever reason (which really isnt important to me because i need to make it work.) So i figured if i cant put it in the calling function then make it apart of a if statement. So this is what i did.
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){ string file2="C:\\Debug\\test.exe"; if(__argv[0] == file2){ ofstream file; file.open("test.txt"); file << "Sponcer brought to you by - Subway! Eat Fresh! :) " << endl; file.close(); system(buffer); exit(1); } else{ Sleep(1000); //cout << "Hello World " << endl; exit(1); } return 0; }
And it works. But my problem is that i dont want to have to specify the name of the program and file path etc etc. Thats what argv[0] is for. So what im trying to do is this.... Use argv within the WinMain function so that i can print out file/path info. That way i can work my way around other problems. Thanx in advance!You need to use GetCommandLine and CommandLineToArgvW like
#include #include #include int __cdecl main() { LPWSTR *szArglist; int nArgs; int i; szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs); if( NULL == szArglist ) { wprintf(L"CommandLineToArgvW failed\n"); return 0; } else for( i=0; i