Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
am very much new to windows currently trying to port a linux project to windows.

My application has an option to change IP-subNet-Gateway of the system with some search i was able to fina an API to it in C way

but the below mentioned works well with XP but win 7 it does not reflect.

C++
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <tchar.h>
typedef long _stdcall (*inpfuncPtr)(char *szAdapterGUID,
                                            DWORD dwDHCP,
                                            DWORD dwIP,
                                            DWORD dwMask,
                                            DWORD dwGateway);



int main(void) {
    HINSTANCE hLib;
    inpfuncPtr in;
    char c;

    hLib = LoadLibrary(TEXT("iphlpapi.dll"));
;
    if (hLib == NULL)
    {
        printf("LoadLibrary Failed.\n");
        return -1;
    }
    in = (inpfuncPtr) GetProcAddress(hLib, "SetAdapterIpAddress");
    if (in == NULL)
    {
        printf("GetProcAddress for SetAdapterIpAddress Failed.\n");
        return -1;
    }
    PWSTR pszGUID = NULL;
    char  szGUID[] = "{6FD68F0D-4656-4428-AF32-E877FA6C1721}";
    DWORD dwSize = 0;

    WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);

    in(szGUID,
                            0,
                            inet_addr("192.168.10.248"),
                            inet_addr("255.255.255.0"),
                            inet_addr("192.168.10.1"));


    printf("OK oK.\n");
    return 0;
}
Posted
Updated 23-Nov-13 22:05pm
v4
Comments
Richard MacCutchan 24-Nov-13 4:06am    
We cannot guess what happens on Windows 7, you need to provide full details.

1 solution

SetAdapterIpAddress() is an undocumented function. Using such functions will work with specific OS versions but they may be removed with newer versions.

You might use DeleteIPADdress() and AddIPAddress()[^] instead (see the example code at the AddIPAddress link).
 
Share this answer
 
Comments
venkat swaminathan 24-Nov-13 5:53am    
@jochen Arndt :: But this does not handle gateway..so for changing IP with gateway in a single execution is there a suggestion
Jochen Arndt 24-Nov-13 6:19am    
The IP helper library provides all necessary functions. See CreateIpForwardEntry() and SetIpForwardEntry(). I don't know of a documented single function to setup IP and routing with one call.

You can also execute system commands like 'route' in a command shell to change the routing and network setup.
venkat swaminathan 25-Nov-13 3:39am    
@jochen arndt : tanks for the support.i was able to access the interface but i am still searching a API for switching between static and DHCP.

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