| View previous topic :: View next topic |
| Author |
Message |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jan 31, 2009 3:32 pm Post subject: Retrieving Command Line Arguments from Win32 App |
|
|
I'm trying to retrieve the command line arguments being passed to my Win32 application, but I can't seem to. This is my code:
| Code: |
//everything before
LPSTR CmdLine;
//command line check+message handler
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
CmdLine = lpCmdLine;
//...
|
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sat Jan 31, 2009 3:52 pm Post subject: |
|
|
| It's pretty easy. If it's multi-byte, you can just use lpCmdLine. It wont work with unicode. If it's unicode, you'll have to use GetCommandLine.
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Jan 31, 2009 3:57 pm Post subject: |
|
|
Or just use _tWinMain and use LPTSTR lpCmdLine, and then you can use that parameter to get them.
| Code: | BOOL WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd)
{
LPTSTR lpCommands = (LPTSTR)HeapAlloc(GetProcessHeap(), 0, sizeof(TCHAR)*(lstrlen(lpCmdLine)+1));
if (lpCommands != NULL)
{
lstrcpy(lpCommands, lpCmdLine);
// Do what you want with it...
HeapFree(GetProcessHeap(), 0, (LPVOID)lpCommands);
}
} |
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jan 31, 2009 4:29 pm Post subject: |
|
|
Lurc I'm not using Unicode. And killersamurai I am using multi-byte, so wouldn't just copying lpCmdLine to CmdLine work?
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Jan 31, 2009 5:24 pm Post subject: |
|
|
| Lurc wrote: |
lstrcpy(lpCommands, lpCmdLine);
|
That's copying, and should work fine.
You're setting the pointer to your string as the pointer to the parameter, which is likely to go away as its scope is left.
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jan 31, 2009 5:44 pm Post subject: |
|
|
Ok, I modified my code to:
| Code: |
//everything before
LPSTR lpCommands;
//stuff
WM_CREATE:
if(strcmp(lpCommands, "/c") == 0) {
//do stuff
}
if(strcmp(lpCommands, "/s") == 0) {
//do stuff
}
//...
int WINAPI WinMain (HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
strcpy(lpCommands, lpCmdLine);
//...
|
But now it's giving me runtime errors:
| Call Stack wrote: |
> IBIM Transfer.exe!strcat(unsigned char * dst=0x00161f4f, unsigned char * src=0xfffffffe) Line 223 Asm
IBIM Transfer.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00161f4f, int nCmdShow=1) Line 160 + 0x10 bytes C++
IBIM Transfer.exe!__tmainCRTStartup() Line 263 + 0x1b bytes C
kernel32.dll!7c817067()
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]
|
| Output wrote: |
'IBIM Transfer.exe': Loaded 'C:\Documents and Settings\OIB\My Documents\Visual Studio 2008\Projects\IBIM Transfer\Debug\IBIM Transfer.exe', Symbols loaded.
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\ntdll.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\kernel32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\user32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\gdi32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\comdlg32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\advapi32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\rpcrt4.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\secur32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\WinSxS\x86_Microsoft.Windows.Common-Controls_6595b64144ccf1df_6.0.2600.5512_x-ww_35d4ce83\comctl32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\msvcrt.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\shlwapi.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\shell32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\ws2_32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\ws2help.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\shimeng.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\imm32.dll'
'IBIM Transfer.exe': Loaded 'C:\WINDOWS\system32\wbsys.dll', Binary was not built with debug information.
First-chance exception at 0x7c91b1fa in IBIM Transfer.exe: 0xC0000005: Access violation writing location 0x00000010.
'IBIM Transfer.exe': Unloaded 'C:\WINDOWS\system32\wbsys.dll'
'IBIM Transfer.exe': Unloaded 'C:\WINDOWS\system32\shimeng.dll'
First-chance exception at 0x004168d0 in IBIM Transfer.exe: 0xC0000005: Access violation writing location 0x00000000.
Unhandled exception at 0x004168d0 in IBIM Transfer.exe: 0xC0000005: Access violation writing location 0x00000000.
|
| strcat.asm wrote: |
byte_0:
mov [edi],dl
mov eax,[esp+8] ; return in eax pointer to dest string
pop edi
ret
|
Something's going wrong on:
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
samuri25404 Grandmaster Cheater
Reputation: 7
Joined: 04 May 2007 Posts: 955 Location: Why do you care?
|
Posted: Sat Jan 31, 2009 6:14 pm Post subject: |
|
|
Did you allocate memory for your string?
_________________
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jan 31, 2009 6:20 pm Post subject: |
|
|
| samuri25404 wrote: | | Did you allocate memory for your string? |
Haha no I didn't. Well it's fixed. Thanks for all your help guys
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
rapion124 Grandmaster Cheater Supreme
Reputation: 0
Joined: 25 Mar 2007 Posts: 1095
|
Posted: Sat Jan 31, 2009 7:57 pm Post subject: |
|
|
I thought command-line was defined as PTSTR*:
| Code: |
int _tmain(int argc, _TCHAR argv[]) //int _tmain(int argc, PTSTR* argv)
{
...
}
|
Notice the brackets? They are interchangeable with a pointer. For example,
| Code: |
void DoSomething(DWORD aArguments[])
|
can be replaced by
| Code: |
void DoSomething(PDWORD aArguments)
|
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sat Jan 31, 2009 9:15 pm Post subject: |
|
|
Once again, I'm not programming in Unicode. Second, argv and argv are used for CLIs, not Win32. And I know arrays and pointers are interchangeable.
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
lurc Grandmaster Cheater Supreme
Reputation: 2
Joined: 13 Nov 2006 Posts: 1900
|
Posted: Sat Jan 31, 2009 9:38 pm Post subject: |
|
|
It doesn't matter if your programming in UNICODE oib, the TCHAR routine takes care of that for you. It still doesn't matter though, just get the string from lpCmdLine and parse it for whatever your looking for.
_________________
|
|
| Back to top |
|
 |
zoren123 Cheater
Reputation: 0
Joined: 28 Nov 2008 Posts: 39 Location: hdhdfgh
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Feb 01, 2009 10:18 am Post subject: |
|
|
One quick little question? Is there anyway you can have Visual Studio execute your program with Command Line Arguments?
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
killersamurai Expert Cheater
Reputation: 0
Joined: 10 Sep 2007 Posts: 197 Location: Colorado
|
Posted: Sun Feb 01, 2009 3:26 pm Post subject: |
|
|
| Project-><solution name> Properties...->Configuration Properties-> Debugging . You'll then see Command Arguments
|
|
| Back to top |
|
 |
oib111 I post too much
Reputation: 0
Joined: 02 Apr 2007 Posts: 2947 Location: you wanna know why?
|
Posted: Sun Feb 01, 2009 9:10 pm Post subject: |
|
|
| killersamurai wrote: | | Project-><solution name> Properties...->Configuration Properties-> Debugging . You'll then see Command Arguments |
Thanks
_________________
| 8D wrote: |
cigs dont make people high, which weed does, which causes them to do bad stuff. like killing |
|
|
| Back to top |
|
 |
|