Click here to Skip to main content
15,941,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
After getting my local IP address, I want set my IP to be the gateway. But there's something wrong the code, as follow:

#pragma comment(lib"ws2_32.lib")

// this function is to get local ip address
void CheckIP(void) //define CheckIP() function to get IP

    WSADATA wsaData;
    char name[255];
    char *ip;
    PHOSTENT hostinfo;

    //instruct MAKEWORD() get Winsock vision
    if ( WSAStartup( MAKEWORD(2,0), &wsaData ) == 0 )

        if( gethostname ( name, sizeof(name)) == 0) {

            if((hostinfo = gethostbyname(name)) != NULL) {

                ip = inet_ntoa (*(struct in_addr *)*hostinfo->h_addr_list);

                printf("\n\n    Your IP IS:%s\n\n",ip); 
        WSACleanup( );     
int main(void)
    system("route -p add mask ip");
    // Maybe it's wrong, but how can correct it ? help!
    /* for example: if the ip that I get is system   ("route -p add mask ")*/


    return 0;

Could you help me correct it ? Show your code after you corrected it.
Many thanks!
Updated 10-Jun-10 19:43pm

1 solution

From your code you're executing:

route -p add mask ip

through a command prompt. From the comments it looks like to expect to see:

route -p add mask


Return the IP address of the computer from CheckIP and then format the string you send to system to include the IP address, not the letters i and p and it might start working.


Share this answer
eagle_chen 11-Jun-10 1:51am    
Thanks. But When I add "return ip;", it's stil.Could you please show me your code after it is corrected? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900