PDA

View Full Version : A slot cause program to finish unexpectedly



SIFE
7th July 2012, 02:00
I am copying my question from stackoverflow (http://stackoverflow.com/questions/11369597/segmentation-fault-when-changing-default-gateway):
I wrote a simple application on Qt4 that modifier network adapter parameters, for that I have a slot called setInterfaceParams, implemented as so:

DWORD WinNetInterface::setInterfaceParams(QString index, QString ip, QString netmask, QString gateway)
{
DWORD res = NULL;
HINSTANCE lib = (HINSTANCE) LoadLibrary((WCHAR *)"iphlpapi.dll");
_SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");

PWSTR pszGUID = NULL;
//char *szGUID = (char *)index.toStdString().c_str();
QByteArray a = index.toLocal8Bit();
char *szGUID = a.data();
WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);


// Method 01
res = SetAdapterIpAddress(szGUID,
0,
inet_addr(ip.toStdString().c_str()),
inet_addr(netmask.toStdString().c_str()),
inet_addr(gateway.toStdString().c_str()));
// End of method 01

// Method 02
/*res = SetAdapterIpAddress("{422C5689-A17B-402D-A6A2-22CE13E857B5}",
0,
inet_addr("192.168.1.10"),
inet_addr("255.255.255.0"),
inet_addr("192.168.1.1"));*/
// End of method 02
return res;
}


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.

#include <windows.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>


typedef DWORD (WINAPI *_SetAdapterIpAddress )(char *szAdapterGUID,
DWORD dwDHCP,
DWORD dwIP,
DWORD dwMask,
DWORD dwGateway);


int main()
{
HINSTANCE lib = (HINSTANCE) LoadLibrary("iphlpapi.dll");
_SetAdapterIpAddress SetAdapterIpAddress = (_SetAdapterIpAddress) GetProcAddress(lib, "SetAdapterIpAddress");

PWSTR pszGUID = NULL;
char szGUID[] = "{422C5689-A17B-402D-A6A2-22CE13E857B5}";
DWORD dwSize = 0;
WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, sizeof(szGUID), NULL, NULL);

DWORD res = SetAdapterIpAddress(szGUID,
0,
inet_addr("192.168.1.10"),
inet_addr("255.255.255.0"),
inet_addr("192.168.1.1"));

std::cout << res;

return 0;
}

mvuori
7th July 2012, 09:48
I guess you should first test with the very same arguments for SetAdapterIpAddress as in yout test program -- just copy and paste the section with hard-coded values -- to see whether the problem is in the calling environment or in some subtle bug in the arguments.

ChrisW67
7th July 2012, 10:36
This looks like a Windows API question and nothing to do with Qt.

What is the value of lib after line 4?

Compare line 4 in your first listing and line 17 in your second listing. There's an obvious difference. What do you think casting a "const char *" to "WCHAR *" does?

Lesiok
7th July 2012, 13:08
Maybe this, line 11 should be :

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