I am copying my question from stackoverflow:
I wrote a simple application on Qt4 that modifier network adapter parameters, for that I have a slot called setInterfaceParams, implemented as so:
Qt Code:
  1. DWORD WinNetInterface::setInterfaceParams(QString index, QString ip, QString netmask, QString gateway)
  2. {
  3. DWORD res = NULL;
  4. HINSTANCE lib = (HINSTANCE) LoadLibrary((WCHAR *)"iphlpapi.dll");
  5. _SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");
  6.  
  7. PWSTR pszGUID = NULL;
  8. //char *szGUID = (char *)index.toStdString().c_str();
  9. QByteArray a = index.toLocal8Bit();
  10. char *szGUID = a.data();
  11. WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);
  12.  
  13.  
  14. // Method 01
  15. res = SetAdapterIpAddress(szGUID,
  16. 0,
  17. inet_addr(ip.toStdString().c_str()),
  18. inet_addr(netmask.toStdString().c_str()),
  19. inet_addr(gateway.toStdString().c_str()));
  20. // End of method 01
  21.  
  22. // Method 02
  23. /*res = SetAdapterIpAddress("{422C5689-A17B-402D-A6A2-22CE13E857B5}",
  24.   0,
  25.   inet_addr("192.168.1.10"),
  26.   inet_addr("255.255.255.0"),
  27.   inet_addr("192.168.1.1"));*/
  28. // End of method 02
  29. return res;
  30. }
To copy to clipboard, switch view to plain text mode 


When I click on button that connected to slot setInterfaceParams, I get segmentation fault. If I comment method01, nothing happen, the some thing happen when I use method02. I tried this function on a simple c++ application and it is work fine, test on Windows XP SP3.
Qt Code:
  1. #include <windows.h>
  2. #include <winsock2.h>
  3. #include <iphlpapi.h>
  4. #include <stdio.h>
  5. #include <iostream>
  6.  
  7.  
  8. typedef DWORD (WINAPI *_SetAdapterIpAddress )(char *szAdapterGUID,
  9. DWORD dwDHCP,
  10. DWORD dwIP,
  11. DWORD dwMask,
  12. DWORD dwGateway);
  13.  
  14.  
  15. int main()
  16. {
  17. HINSTANCE lib = (HINSTANCE) LoadLibrary("iphlpapi.dll");
  18. _SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");
  19.  
  20. PWSTR pszGUID = NULL;
  21. char szGUID[] = "{422C5689-A17B-402D-A6A2-22CE13E857B5}";
  22. DWORD dwSize = 0;
  23. WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);
  24.  
  25. DWORD res = SetAdapterIpAddress(szGUID,
  26. 0,
  27. inet_addr("192.168.1.10"),
  28. inet_addr("255.255.255.0"),
  29. inet_addr("192.168.1.1"));
  30.  
  31. std::cout << res;
  32.  
  33. return 0;
  34. }
To copy to clipboard, switch view to plain text mode