Using the IP Address

closed account (Gy7oizwU)
Hi

I have code that gets the IP address of a remote PC as a char. Now i need to use this IP Address. I want to run system("START \\\\IP\C$"). Does anyone have any ideas how i can go about implementing this?

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
int main()
{  
	GetHostIP();
	system("PAUSE");

}


void GetHostIP()
{
    char *Ip;
    WSADATA wsaData;
    struct hostent *pHostEnt; 
    struct sockaddr_in tmpSockAddr; //placeholder for the ip address

    // Not needed if it is already taken care by some other part of the application
    WSAStartup(MAKEWORD(2,0),&wsaData); 

    char hostname[25];
    strcpy(hostname,"PC1");
    //This will retrieve the ip details and put it into pHostEnt structure
    pHostEnt = gethostbyname(hostname);

    if(pHostEnt == NULL)
    {
        printf("Error occured: %s\n",GetLastError());
        return;
    }

    memcpy(&tmpSockAddr.sin_addr,pHostEnt->h_addr,pHostEnt->h_length);

    Ip = NULL;
    Ip = new char[17];
    strcpy(Ip,inet_ntoa(tmpSockAddr.sin_addr));
	std::cout << Ip;


    // Not needed if it is already taken care by some other part of the application
    WSACleanup();
    delete [] Ip;
}
Why \\<ip address>\c$ when you already have the host name?
Why not use \\<hostname>\c$?
closed account (Gy7oizwU)
Its hard to explain. There are multiple environments that are used. Using the IP address is safer because sometimes using the host name will use the IP address from the environment previously used.
The OS is just goint to use gethostbyname just like you. So if you can't use the UNC name in your environment because of DNS/WINS problems, you'll have the same issues in your app.
closed account (Gy7oizwU)
Thanks kbw. I managed to sort the problem out.

The program now connects to the network pc using system("START \\pc1\c$") and it prompts for the username and password. Is there a way to pass the username and password in the program instead of the user having to manually enter it?
you need to use the command line first and run:
net use \\<hostname>\<share> /u:<domain\userid> <password>
closed account (Gy7oizwU)
Sorted. Thanks!
Topic archived. No new replies allowed.