EvilZone
Programming and Scripting => Beginner's Corner => : $Clone January 22, 2015, 12:53:15 AM
-
hey this is a simple program to create and start a wifi hotspot through basic system calls
note that it requires admin privileges to run!
# include<Windows.h>
#include <Stdio.h>
# include<string.h>
main()
{
char choice;
char ssidName[100],SSID[100],command[]="netsh wlan set hostednetwork mode=allow ssid=";
char Password[100],passwd[]=" key=";
printf("--------------------------------------------------------\n");
printf("--------------------------------------------------------\n");
printf("C program to create a Wifi hotspot based on sys calls\n");
printf("--------------------------------------------------------\n");
printf("--------------------------------------------------------\n");
printf("Enter Network SSID:");
fgets(SSID,100,stdin);
strcpy(ssidName,SSID);
printf("--------------------------------------------------------\n");
printf("Enter Network Password:");
fgets(Password,100,stdin);
printf("--------------------------------------------------------\n");
strcat(command,SSID);
strcat(passwd,Password);
strcat(command,passwd);
printf("---------------------------------------------------------\n");
printf("Output:\n");
printf("---------------------------------------------------------\n");
if(system(command)==0)
{ printf("---------------------------------------------------------\n");
printf("Hotspot successfully created!\n");
printf("Do you want to start connections to %s:",ssidName);
scanf("%c",&choice);
switch(choice)
{ case 'y':
system("netsh wlan start hostednetwork\n");
break;
case 'n':
//should not be there use Wlan fucntions...maybe next time
system("netsh wlan stop hostednetwork\n");
break;
default:
printf("Please choose choice y or n\n");
break;
}
}
else
printf("Error creating Wifi hotspot\n");
printf("--------------------------------------------------------\n");
printf("--------------------------------------------------------\n");
printf("End");
getchar();
return 0;
}
Nothing much just basics. :P
-
I'm not going to -1 cookie you because this is the noob programming forum :D but I almost did on accident.
Now, let me first say that your main function should return an integer. I don't care what tutorial site said what or why they said that, but this is convention. Your main function should return 0 if it was sucessful and if something messes up (like your program isn't given enough command line arguments or somthing) then you return a nonzero integer. There are "better" ways to do this with functions like exit, perror, etc.. but returning a nonzero integer will be fine for now.
Also you should look into what a proper coding style is, pick one you like, and follow it. I'd recommend looking at this wiki category (http://en.wikipedia.org/wiki/Category:Programming_rules_of_thumb) on general rules of thumb and this wiki article (http://en.wikipedia.org/wiki/Programming_style) on code style. (Although you might find more information on coding styles by googling them yourselves.)
Also, excessive printf's aren't the way to go all the time. Like I wrote above, if you'd like to impliment different "options" it's best to do them through command line switches. I'll write some basic command line argument code below just so you can kind of see what's going on.
#include <stdio.h>
/* Our example program is going to take 2 arguments "ip" and "port" */
int main(int argc, char **argv)
{
char *ip;
char *port;
if (argc != 3) {
/* Notice I'm writing to stderr instead of stdout (if you don't know what these are,
* you can google them very easily) this is to distinguish between regular and
* error messages. */
fprintf(stderr, "Usage: ./program <ip> <port>\n");
return 1;
}
/* argv is a list of the command line arguments. argc (as shown above)
* is the argument "count" and is useful for checking to see if the user has
* provided enough arguments. REMEMBER: you must verify the format of
* the arguments are correct. I haven't included this in this snip of code,
* but it'd definitely important if you plan on passing user input through system!
* (passing user defined data through system is a terrible idea, i'd recommend
* staying faaar away from system() unless you're just messing around with it
*/
ip = argv[1];
port = argv[2];
printf("Your two arguments are `%s` and `%s`\n", ip, port);
/* We've made it to the end of the program and we missed the argc error! We can safely
* return 0 and go on our way. */
return 0;
}
If you have any other questions about this sort of coding style (using command line arguments instead of an interactive UI) please ask. As far as the posted code goes, it's cool you're getting into syscalls, check out <unistd.h> for other syscall wrappers or alternatively <sys/types.h> and <sys/syscall.h> if you want raw syscall action. :P
I've also been looking into different linux syscalls and i noticed you included <Windows.h> so i can tell you're probably on windows. In that case I feel like everything I've typed might be incorrect (I really don't know how to program on windows) but I'd suggest getting a debian VM or something if you aren't dual booting at the moment just to begin using things like man pages and Linux syscalls.
I feel like I've rambled a bit but if I've been unclear on anything please ask. :)
-
And even if you don't want to follow the way Polyphony noted above, with the argv, argc usage... which is the best way for the program you were coding. You could also make it more appropriate like this:
int main(void)
{
// Code Code
return 0;
}
The thing is that now your main function is somewhat taken as a void type function if I am not mistaken when it actually returns an int at last. This is very wrong.
-
I like your strcpy()s $Clone-chan :3 I promise to be gentle...
-
You guys are really making a big deal out of this, it was written "noob fetish" ::) .....anyway points taken!
------------
I'm not going to -1 cookie you because this is the noob programming forum :D but I almost did on accident.Now, let me first say that your main function should return an integer. I don't care what tutorial site said what or why they said that, but this is convention. Your main function should return 0 if it was sucessful and if something messes up (like your program isn't given enough command line arguments or somthing) then you return a nonzero integer. There are "better" ways to do this with functions like exit, perror, etc.. but returning a nonzero integer will be fine for now.Also you should look into what a proper coding style is, pick one you like, and follow it. I'd recommend looking at this wiki category (http://en.wikipedia.org/wiki/Category:Programming_rules_of_thumb) on general rules of thumb and this wiki article (http://en.wikipedia.org/wiki/Programming_style) on code style. (Although you might find more information on coding styles by googling them yourselves.)Also, excessive printf's aren't the way to go all the time. Like I wrote above, if you'd like to impliment different "options" it's best to do them through command line switches. I'll write some basic command line argument code below just so you can kind of see what's going on.#include <stdio.h>/* Our example program is going to take 2 arguments "ip" and "port" */int main(int argc, char **argv){ char *ip; char *port; if (argc != 3) { /* Notice I'm writing to stderr instead of stdout (if you don't know what these are, * you can google them very easily) this is to distinguish between regular and * error messages. */ fprintf(stderr, "Usage: ./program <ip> <port>\n"); return 1; } /* argv is a list of the command line arguments. argc (as shown above) * is the argument "count" and is useful for checking to see if the user has * provided enough arguments. REMEMBER: you must verify the format of * the arguments are correct. I haven't included this in this snip of code, * but it'd definitely important if you plan on passing user input through system! * (passing user defined data through system is a terrible idea, i'd recommend * staying faaar away from system() unless you're just messing around with it */ ip = argv[1]; port = argv[2]; printf("Your two arguments are `%s` and `%s`\n", ip, port); /* We've made it to the end of the program and we missed the argc error! We can safely * return 0 and go on our way. */ return 0;}
If you have any other questions about this sort of coding style (using command line arguments instead of an interactive UI) please ask. As far as the posted code goes, it's cool you're getting into syscalls, check out <unistd.h> for other syscall wrappers or alternatively <sys/types.h> and <sys/syscall.h> if you want raw syscall action. :P I've also been looking into different linux syscalls and i noticed you included <Windows.h> so i can tell you're probably on windows. In that case I feel like everything I've typed might be incorrect (I really don't know how to program on windows) but I'd suggest getting a debian VM or something if you aren't dual booting at the moment just to begin using things like man pages and Linux syscalls.I feel like I've rambled a bit but if I've been unclear on anything please ask. :)
PolyPhony do you really code like this http://en.wikipedia.org/wiki/Programming_style (http://en.wikipedia.org/wiki/Programming_style) ??? ??
NO DOUBLE POSTING
-
All of Poly and SpaceCows points are valid, however a note for anyone who cares to leave it out, you can TECHNICALLY write main() without a return type, and for MOST compilers it will be implicitly set to int. (especially if you have a return int; statement anywhere)
HOWEVER. That is bad practice, for the same reason as not defining signedness of variables andnot returning anything from a function(even voids), plus a million other things people do that compilers fix for you.
And yes, if someone goes to compile your code on some other random backwoods compiler (assuming it is compliant with standards) then YOUR code is shitty/ the problem. Because YOUR code doesn't meet the standards.
Not saying this will happen here, and I actually +1'd OP for going outside his norm/expected coding level (I assume), but definitely something to make note of. The only way to get better at coding is to have your code critiqued and if you aren't at the point where you can look at it and say "fuck i did that in a shitty way" or "I could have optimized that loop a bit more" then you gotta accept it from others :)
So keep coding OP.
-
@HTH and everybody else.....point noted....thnx.
-
So can you compile and post an exe so I can try it out? I am in need of a Connectify alternative.
Also please add ability to stop hotspot and/or completely remove.
-
$Clone: Hey I understand it's noob code and like HTH said, as long as you're trying new things that's great. Also, if I sound harsh I really don't mean it, I think I'm just kind of bad at writing. :D
As far as coding style goes I don't think I've ever followed one too strictly, it kind of changes on little things day by day. I find that I agree most with the coding style described in the Geany coding style guide (https://github.com/geany/geany/blob/master/HACKING#L219) but like I said before, it's really a personal preference unless you want to write code for another project like geany, the kernel, etc, as they all have their own style guides.