MinGW : accessing __argv[0] produces SegmentationFault (sometimes...)

242 Views Asked by At

I'm in trouble when trying to access from a CPP file the command line arguments. Using __argv used to work fine for years but now, I'm getting a different response when using Dev-CPP (ver. 5.5.3) and Code::Blocks (svn build rev 9248 patched to work with FORTRAN). Below I've pasted a 3 statements example and commented above the lines troubling me, while below the final brace I've added compile and link commands. What I stopping me from still using __argv ?

#include <stdlib.h>
#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
char cmd[1024];

// line below work OK with Dev-CPP and TDM-GCC 4.7.1 
// (and all previous versions I've used ...) but got a
// "Program received signal SIGSEGV, Segmentation fault" 
// when using C::B and GCC 4.8.1-4
strcpy(cmd, __argv[0]);

// line below work fine with C::B and GCC 4.8.1-4 but 
// won't compile with Dev-CPP and TDM-GCC 4.7.1
//  strcpy(cmd, _argv[0]);

MessageBoxA(NULL, cmd, "argv[0]", MB_OK | MB_ICONINFORMATION | MB_TASKMODAL);

return 0;
}

/*
C::B compiler commands :

mingw32-g++.exe -march=i586 -Wextra -Wall -g -fpermissive -Wno-write-strings -D_WIN32_IE=0x0501 -D_WIN32_WINNT=0x0500 -DWINVER=0x0500 -g -march=i586 -Wextra -Wall  -c C:\Dev\Src\C_B\trivialtests\wtest1\wtest1_main.cpp -o obj\Debug\wtest1_main.o
mingw32-g++.exe  -o bin\Debug\wtest1.exe obj\Debug\wtest1_main.o  -static  -lgdi32 -luser32 -lkernel32 -lcomctl32

Dev-C++ compiler commands :
g++.exe -c wtest1_main.cpp -o wtest1_main.o -I"C:/Dev/Dev-Cpp/MinGW64/include" -I"C:/Dev/Dev-Cpp/MinGW64/x86_64-w64-mingw32/include" -I"C:/Dev/Dev-Cpp/MinGW64/lib/gcc/x86_64-w64-mingw32/4.7.1/include/c++" -march=pentium -m32 -Wall -Wextra -D_WIN32_IE=0x0501 -D_WIN32_WINNT=0x0500 -DWINVER=0x0500 -fpermissive -Wno-write-strings
g++.exe wtest1_main.o -o wtest1.exe -L"C:/Dev/Dev-Cpp/MinGW64/lib32" -L"C:/Dev/Dev-Cpp/MinGW64/x86_64-w64-mingw32/lib32" -static-libgcc -mwindows -m32

full GDB message :
Program received signal SIGSEGV, Segmentation fault.
At C:\Dev\Src\C_B\trivialtests\wtest1\wtest1_main.cpp:10
> backtrace
#0  0x004016c0 in WinMain@16 (hInstance=0x400000, hPrevInstance=hPrevInstance@entry=0x0, lpCmdLine=lpCmdLine@entry=0x583282 "", nCmdShow=nCmdShow@entry=10) at C:\Dev\Src\C_B\trivialtests\wtest1\wtest1_main.cpp:10
#1  0x0041117b in main (argc=1, argv=0x3e17f0, __p__environ=0x3e1c10) at ../mingwrt-4.0.3-1-mingw32-src/src/libcrt/crt/main.c:91

*/
2

There are 2 best solutions below

0
On

You should probably file a bug upstream.

Not sure, about what is causing this to differ and also lack the environments. I'd assume it is due to the way different version of the mingwrt or mingw-w64 runtimes declare/define __argv/_argv. You might want to check the corresponding includes and/or preprocessed sources.

If you don't mind using some Windows API, you could still use CommandLineToArgvW(GetCommandLineW(), ...) and convert from widechar to narrow char yourself, if needed.

0
On

Firstly, after deleting everything and re-installing, I discovered that everything was OK and worked as expected. So the matter was simply overconfidence regarding Dev-C++ and C::B setup when sitting side by side... Secondly, I've appreciated the valid reply, but... using Win API CommandLineToArgvW(...)is just redoing what the startup code is already performing. Finally, just for reference, I actually found (and used) __argv and __argc in Borland Builder, Open Watcom and GCC. Don't know if this is requested by standard, or just "de facto" standard (inheritage from MS libraries).