Results 1 to 4 of 4

Thread: A slot cause program to finish unexpectedly

  1. #1
    Join Date
    Feb 2011
    Posts
    64
    Thanks
    16
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default A slot cause program to finish unexpectedly

    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 

  2. #2
    Join Date
    May 2011
    Posts
    239
    Thanks
    4
    Thanked 35 Times in 35 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Symbian S60

    Default Re: A slot cause program to finish unexpectedly

    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.

  3. #3
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: A slot cause program to finish unexpectedly

    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?

  4. #4
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: A slot cause program to finish unexpectedly

    Maybe this, line 11 should be :
    Qt Code:
    1. WideCharToMultiByte(CP_ACP, 0, pszGUID, -1, szGUID, strlen(szGUID)+1, NULL, NULL);
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. the program has unexpectedly finished
    By narlapavan in forum Qt Programming
    Replies: 9
    Last Post: 9th July 2012, 09:04
  2. Replies: 6
    Last Post: 24th June 2012, 18:32
  3. Program quits unexpectedly
    By MCFormax in forum Newbie
    Replies: 2
    Last Post: 9th November 2011, 02:31
  4. slot to execute (or initialize) my program
    By 21did21 in forum Newbie
    Replies: 2
    Last Post: 3rd September 2011, 22:59
  5. Program has unexpectedly finished
    By Maluko_Da_Tola in forum Newbie
    Replies: 5
    Last Post: 1st December 2010, 09:54

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.