سؤال

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;
}
هل كانت مفيدة؟

المحلول

LoadLibrary((WCHAR *)"iphlpapi.dll");

That can't work, the literal string is in 8-bits, casting it without real conversion doesn't make it wide, so the dll loading probably failed.

You should use the TEXT or _T macro around most of the literal strings passed to WinAPI functions to make them regular or wide depending on the compilation options:

LoadLibrary(_T("iphlpapi.dll"));

which will translate to either LoadLibrary("iphlpapi.dll"); or LoadLibrary(L"iphlpapi.dll");.

Also you should always check the value returned by the LoadLibrary and GetProcAddress functions, which return NULL if the call is unsuccessful.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top