Articles

Metasploit Framework Windows Tutorial
Remote Desktop Connection
Windows Processes That May Be Dangerous
How-To use NetCat a Tutorial
Common Linux Commands
Common Ports
Netcat Commands
HTTP Response Codes
War-Google Hack Terms
Wardriving
Avoiding Social Engineering and Phishing Attacks
Intrusion Detection on Linux
Linux Intrusion Detection
Penetration Testing Guide
Penetration Testing Tools
Social Engineering Fundamentals, Part I: Hacker Tactics
Social engineering (computer security)
The Psychology of Social Engineering

The Archives

General GSO
GovernmentSecurity.org News & Suggestions
In The News
Open Topic
General Security Information
Trash Can
Exploit & Vulnerability Mailing List Archives
Trial Member Forum
Product and Program Reviews GSO Tutorials
System Security
Windows Systems
Beginners Section
Linux & Unix Systems
File Downloads
Exploit Research & Discussion Trojan & Virus Errata
Networking Security / Firewall / IDS / VPN / Routers
System Hardening
E-Mail Security
Wifi Security
Trial Member Uploads
Upload discovered Trojans & Mal ware
GSO Programming Section
C , C++ , VC++
Visual Basic.NET
Perl /CGI
Java/Javascript
PHP/XML/ASP/HTML
Assembly + Other
The Cork Board
Network Security Consultant Directory
Network Security Jobs
The Archives
Encryption Information
General Network Security
Internet Anonymity
HTTP Protocol Security
Linux Security
MS IIS Information
Exploit Articles
Programming / Tool Design
GSO Software Projects
Public Downloads
Microsoft Security Questions and Papers

DumpZ
How can i code a program so i can use switches?

i used the amount of arguments first but that get really annoying when you wanna build in more options
usch
as you may know, functions can have parameters e.g.:
void myFunction(int someNUmber).
so can your main function. it goes like this:

CODE

main(int argc, char *argv[0])

argc is the number of given arguments, and argv is an array of all the arguments.
note: the app's .exe itself is always the first argument or program switch.
if u have myapp.exe /p
then argv[1] (note that array indexers begin with 0) is "/p"
and argc is 2

i'm pretty sure that there is no other way.

cheers
usch
plinius
[QUOTE]main(int argc, char *argv[0])[CODE] when you do it like that you only pass the first argument to the main function, and you can't use the others... I think.

You should do:[QUOTE]main(int argc, char *argv[])[CODE].

(I can't test it now, because I have no compiler installed)
DumpZ
usch i already used that one but i dont know how to put it that the switches can be random.

so

that is can be either

exe.exe -p 00 -n name or exe.exe -n name -p 00
ninar12
then u have to check the argv[1]
e.g

if ( strcmp(argv[1],"-install") == 0 )
{
///your code
}

at linux there is getopt() on windows u have to do this hardcoded


edit:
omg 3 minutes posted after he said "random"

now i understand what he really want
DumpZ
but if i want to have 10 switches for example i would have to do ten times

if ( strcmp(argv[1],"-install") == 0 )

with every option right?
usch
yeah you have to do that.
for(x=0;x<argc;x++)
...

usch
ninar12
#include <windows.h>
#include <stdio.h>
void main(int argc, char* argv[])
{
unsigned int i=0;
for (i=0;i<argc;i++)
{
if ( strcmp(argv[i],"-p") == 0 )
{
printf("port:%s\n",argv[i+1]);
///your code
}
else
if ( strcmp(argv[i],"-i") == 0 )
{
printf("ip:%s\n",argv[i+1]);
}
}
}


/*output

C:\Dokumente und Einstellungen\nina\Desktop\foo\Debug>2.exe -p 22 -i 127.0.0.1
port:22
ip:127.0.0.1

C:\Dokumente und Einstellungen\nina\Desktop\foo\Debug>2.exe -i 127.0.0.1 -p 23
ip:127.0.0.1
port:23



/*


edit: that was my first try jsut to show it can be done

just play around with it
[_-ViCiOuS-_]
getopt is easier
http://rootr.net/man/man/getopt/3
DumpZ
thanks ninar12 i didnt really try that but it seems to work fine thanks.


And [_-ViCiOuS-_] what im looking for in windows and not unix but i still appriciate the help thanks
cowsonfire
QUOTE(DumpZ @ Aug 10 2005, 09:24 PM)
And [_-ViCiOuS-_] what im looking for in windows and not unix but i still appriciate the help thanks
*


getopt works in windows too...
wiNGCom
QUOTE
getopt works in windows too...


indeed, moreover source code for windows getopt() can be found in the msdn library of vc6.
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2005 Invision Power Services, Inc.