Porting a C programm to windows.

p4p1 [lexostras]
9 years ago

0

Hy so i just made a program in C that gets the ip of the computer

struct information {  
    char * Usb_port;  
    char * ip_wlan0;  
    char * ip_eth0;  
    char * IP;  
    char * Date_Time;  
    char * host;  
} ;  

void get_Ip(struct information * ads)  
{  
    int fd;  
    struct ifreq ifr;  

    ///Tis code is for wlan0  

    fd = socket(AF_INET, SOCK_DGRAM, 0);  
    ifr.ifr_addr.sa_family = AF_INET;  
    snprintf(ifr.ifr_name, IFNAMSIZ, "wlan0");  
    ioctl(fd, SIOCGIFADDR, &ifr);  

    //define in the struct the correct ip  
    ads->ip_wlan0 = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);  

    close(fd);  

    //This code is for eth0  

    fd = socket(AF_INET, SOCK_DGRAM, 0);  
    ifr.ifr_addr.sa_family = AF_INET;  
    snprintf(ifr.ifr_name, IFNAMSIZ, "eth0");  
    ioctl(fd, SIOCGIFADDR, &ifr);  

    //define in the struct the correct ip  
    ads->ip_eth0 = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);  

    close(fd);  
}  

this works fine on my linux system but i am having problem to make the get_Ip() for windows, does anyone know a way for me to get the current machine ip in C for windows

3replies
3voices
175views
Mugi [Mugiwara27]
9 years ago

0

This might interest you ;

cURL for C programming

( Just do a curl request to a website / server that return the IP of the one who launched the request )

p4p1 [lexostras]
9 years ago

0

ok thanks @Mugiwara27 ill have a look

ghostZero
9 years ago

0

On windows, sockets are win_sockets…
So, you could follow the suggestion of @Mugiwara27 or you could use cygwin (a sort of sample but quite complete linux shell for windows [or pacman to install a lot of stuff]) or use the processor directives to find the correct OS.

For instance:

ifdef unix

/ to do/

endif

ifdef WIN32

/to do/

endif

and so on :-)

You must be logged in to reply to this discussion. Login
1 of 4

This site only uses cookies that are essential for the functionality of this website. Cookies are not used for tracking or marketing purposes.

By using our site, you acknowledge that you have read and understand our Privacy Policy, and Terms of Service.

Dismiss