First I would like to bring up the question whether assigning a fixed IP address is a proper approach. There’s plenty of use cases where this leads to serious problems. To give you one example: If the DHCP server in a big network fails for a short period, you will assign a fixed IP which might conflict with another device in the same network (or even with multiple of your own devices).
Actually there’s already a standardized mechanism which probably fits for your what you want to do - each Ethernet device assigns itself an automatic IP address (Auto-IP, link-local address) in the range 169.254.x.x, if there is no DHCP server around.
All you need to do is broadcast your own IP on one side, and receive this broadcast on the other side, in order to learn the partner’s IP address and start the communication.
If your device has a unique name, you can even directly start a communication using this name.
If you still want to assign a fixed IP address, you need to implement this on your own. The basic steps are:
- Use GetAdaptersInfo() function to query whether you got a valid IP address. After a timeout…
- Change the IP address, as described in the community post:
Make sure you don’t save the registry. Otherwise your next boot will directly start with the fixed IP!