Results 1 to 14 of 14

Thread: Invoking dll function with argument crashed

  1. #1
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Invoking dll function with argument crashed

    My Qt app needs to invoke a function with arguments from my owned dll, which was created with VS2010.
    It runs as expected if the imported function without any argument.
    But with arguments it will crash, although there is no problem when compiling.

    Any advise will be helpful!
    Thanks.

    //.pro
    Qt Code:
    1. LIBS += -L ./ -lop
    To copy to clipboard, switch view to plain text mode 

    //op.h
    Qt Code:
    1. #ifndef OP_H
    2. #define OP_H
    3. #include <string>
    4. extern "C" __declspec(dllexport) void excute(std::wstring url);
    5. #endif // OP_H
    To copy to clipboard, switch view to plain text mode 

    //invoke code
    Qt Code:
    1. std::wstring url=qstr_url.toStdWString();
    2. excute(url);
    To copy to clipboard, switch view to plain text mode 

    //dll in vs2010
    // op.cpp
    Qt Code:
    1. #include "stdafx.h"
    2. extern "C" __declspec(dllexport) void excute(std::wstring url )
    3. {
    4. STARTUPINFO start_info;
    5. PROCESS_INFORMATION proc_info;
    6. ::ZeroMemory(&start_info, sizeof(start_info));
    7. start_info.cb = sizeof(start_info);
    8. ::ZeroMemory(&proc_info, sizeof(proc_info));
    9.  
    10. std::wstring browser = L"C:/Program Files/Internet Explorer/iexplore.exe";
    11. //std::wstring url = L"http://google.com";
    12. std::wstring command = browser + L" " + url;
    13. LPWSTR command_line = new WCHAR[command.size() + 1];
    14. wcscpy_s(command_line, command.size() + 1, command.c_str());
    15. command_line[command.size()] = L'\0';
    16. ::CreateProcess(NULL, command_line, NULL, NULL, FALSE, 0, NULL, NULL, &start_info, &proc_info);
    17. if (proc_info.hThread != NULL) {
    18. ::CloseHandle(proc_info.hThread);
    19. }
    20. if (proc_info.hProcess != NULL) {
    21. ::CloseHandle(proc_info.hProcess);
    22. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Invoking dll function with argument crashed

    Run it in a debugger, and step through, and see on which line it crashes.
    Then we can start looking with some idea on what to look for.
    It doesn't sound like a problem with the lib, but more like a problem in the code in self, in execute() - probably a pointer issue or similar.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    thanks for advise!

    debug result point to the invoking code line:
    excute(url);
    but there is only this line doing the invoking from dll.

    i don't know what's wrong with it.

    i use the implicit invocation in qt codes.
    i tried explicit invoking but the result is the same.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Invoking dll function with argument crashed

    Add a check to make sure the url parameter is not null - before calling execute() - in in execute it self - it should be able to exit gracefully on null string url.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    thanks for advise!
    i added one line like you said:

    Qt Code:
    1. if(url.length()!=0)
    2. excute(url);
    To copy to clipboard, switch view to plain text mode 

    but it stiil crashed.

  6. #6
    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: Invoking dll function with argument crashed

    Single step your code inside excute(), it's failing among that non-Qt code somewhere.

    Does ieexplore.exe exist at that path? The space embedded in the path to iexplore.exe is possibly causing issues: see the CreateProcess docs.

    Or... use Qt because QDesktopServices::openUrl() is a much easier way to launch the system default browser.

  7. #7
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    yes, maybe merging browser path and link is the issue. so i modified the codes:
    Qt Code:
    1. #include "stdafx.h"
    2.  
    3. extern "C" __declspec(dllexport) void excute() //removed arguments to open google page, but it opened a blank page
    4. {
    5. STARTUPINFO start_info;
    6. PROCESS_INFORMATION proc_info;
    7.  
    8. ::ZeroMemory(&start_info, sizeof(start_info));
    9. start_info.cb = sizeof(start_info);
    10. ::ZeroMemory(&proc_info, sizeof(proc_info));
    11.  
    12. LPCTSTR browser = L"G:/Program Files/Internet Explorer/iexplore.exe";
    13. LPTSTR command_line = L"http://google.com";
    14.  
    15. ::CreateProcess(browser, command_line, NULL, NULL, FALSE, 0, NULL, NULL, &start_info, &proc_info);
    16.  
    17. if (proc_info.hThread != NULL) {
    18. ::CloseHandle(proc_info.hThread);
    19. }
    20.  
    21. if (proc_info.hProcess != NULL) {
    22. ::CloseHandle(proc_info.hProcess);
    23. }
    To copy to clipboard, switch view to plain text mode 
    this time, it did not crash, but ie opened with a blank page, not google.
    i must be crazy...

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Invoking dll function with argument crashed

    Why don't you do what we suggested to you?
    Step through exectue() - and follow your url variable and all other values - and you will see exactly what and when is going wrong.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    sorry but i did the debug and it pointed to that the issue code line is "excute(url);" , and the url is right.
    so what i think is the dll has some wrong codes.
    in post #7, i modified some codes, it did not crash now, but it opened a blankpage instead of the assigned url.

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Invoking dll function with argument crashed

    maybe you don't understand what is meant:
    by stepping through, you step one line at a time during execution in the debugger - IN your execute() function.
    This way you can examine all the values of your variables, and see which ones do not match what you expect.
    You might want to escape the slashes in the path to iexploere.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    thanks for advise!
    do you mean running vs2010's debugger to check the dll function?
    then i will do.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Invoking dll function with argument crashed

    Yes - what ever debugger you are using will be fine.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    But remember that you must build a DEBUG version of your DLL in order to step into its execute() method and debug it. You can't link to a release version of the DLL and debug it.

  14. #14
    Join Date
    Dec 2010
    Posts
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Invoking dll function with argument crashed

    thanks for all you guys's help!
    i must learn more, more...

Similar Threads

  1. onclick event passing argument to js function
    By dyngoman in forum Qt Programming
    Replies: 0
    Last Post: 10th September 2011, 13:51
  2. QVector as function argument
    By stefan in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2011, 12:40
  3. QMap as function argument...
    By cydside in forum Qt Programming
    Replies: 5
    Last Post: 18th April 2009, 17:59
  4. Replies: 1
    Last Post: 12th January 2009, 18:05
  5. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46

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.