Results 1 to 6 of 6

Thread: URLDownloadToFile, TCHAR, string, wrong type.

  1. #1
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default URLDownloadToFile, TCHAR, string, wrong type.

    I have sth like that

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QApplication>
    5. //#include <config.h>
    6.  
    7. #include <qt_windows.h>
    8.  
    9. typedef HRESULT (*P_URLDownloadToFile)(
    10. LPUNKNOWN pCaller,
    11. LPCTSTR szURL,
    12. LPCTSTR szFileName,
    13. DWORD dwReserved,
    14. void*
    15. );
    16.  
    17.  
    18. TCHAR path[] = TEXT("http://webpage.com/index.php");
    19. TCHAR filew[] = TEXT("file.name");
    20. wchar_t ur[] = TEXT("urlmon.dll");
    21.  
    22. P_URLDownloadToFile _URLDownloadToFile;
    23.  
    24. int main(int argc, char *argv[])
    25. {
    26. QApplication a(argc, argv);
    27. MainWindow w;
    28.  
    29. HRESULT uRet = 1;
    30. HINSTANCE hLib;
    31. HMODULE urlmon=LoadLibrary(ur);
    32. if(urlmon){ _URLDownloadToFile=(P_URLDownloadToFile)GetProcAddress(urlmon,"URLDL2F"); }
    33. uRet = _URLDownloadToFile(NULL,path,filew,0,NULL);
    34. if (uRet == S_OK)
    35. { qDebug() << "OK" << endl; }
    36. else { qDebug() << "Wrong" << endl;}
    37.  
    38. FreeLibrary(hLib);
    39.  
    40. // Configsettings set_settings;
    41. // set_settings.loadConfig();
    42. w.show();
    43. return a.exec();
    44. }
    To copy to clipboard, switch view to plain text mode 


    When I was using this code in DevC++ it was working because insted of TCHAR and wchar_t I had "string". In QtCreator I had got errors like " cannot convert 'std::string' to 'const TCHAR*' in argument passing"

  2. #2
    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: URLDownloadToFile, TCHAR, string, wrong type.

    Where do you get this error? Line 32? Try putting L"URLDL2F" in the GetProcAddress call. The simple "URLDL2F" is a char * string, and Qt expects a Unicode TCHAR (wchar_t) string.

  3. The following user says thank you to d_stranz for this useful post:

    darkrptyp (9th December 2011)

  4. #3
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: URLDownloadToFile, TCHAR, string, wrong type.

    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QtGui>
    4. #include <QApplication>
    5. #include <config.h>
    6.  
    7. #include <qt_windows.h>
    8.  
    9. using namespace std;
    10.  
    11. typedef HRESULT (*P_URLDownloadToFile)(
    12. LPUNKNOWN pCaller,
    13. LPCTSTR szURL,
    14. LPCTSTR szFileName,
    15. DWORD dwReserved,
    16. void*
    17. );
    18. //TCHAR path[] = TEXT("http://webpage.com/index.php");
    19. //TCHAR filew[] = TEXT("file.name");
    20.  
    21. //wchar_t ur[] = TEXT("urlmon.dll");
    22.  
    23. string path = "http://dl.dropbox.com/u/18389601/prob.png";
    24. string filew = "f.f";
    25.  
    26.  
    27.  
    28. P_URLDownloadToFile _URLDownloadToFile;
    29.  
    30. int main(int argc, char *argv[])
    31. {
    32. QApplication a(argc, argv);
    33. MainWindow w;
    34.  
    35. HRESULT uRet = 1;
    36. HINSTANCE hLib;
    37. HMODULE urlmon=LoadLibrary("urlmon.dll"); // error for this line - cannot convert 'const char*' to 'const WCHAR*' for argument '1' to 'HINSTANCE__* LoadLibraryW(const WCHAR*)'
    38. if(urlmon)
    39. _URLDownloadToFile=(P_URLDownloadToFile)GetProcAddress(urlmon,"URLDownloadToFileA");
    40.  
    41.  
    42. uRet = _URLDownloadToFile(NULL,path,filew,0,NULL); // error for this line - cannot convert 'std::string' to 'const TCHAR*' in argument passing
    43. if ( uRet == S_OK )
    44. { qDebug() << "OK" << endl; }
    45. else { qDebug() << "Wrong" << endl;}
    46. // qDebug() << adres_new;
    47.  
    48. FreeLibrary(hLib);
    49.  
    50. // Configsettings set_settings;
    51. // set_settings.loadConfig();
    52.  
    53. w.show();
    54. return a.exec();
    55. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    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: URLDownloadToFile, TCHAR, string, wrong type.

    This is, of course, absolutely nothing to with Qt.

    Line 37, you are passing a "const char *" where the parameter type is "const WCHAR*". d_stranz has given you an approach for this.
    Line 42, you are passing a std::string where the function is expecting a "const TCHAR*". There is no cast operator that the compiler can find. Try the c_str() member of std::string (better yet use a wstring).

    When compiled with UNICODE (and/or _UNICODE) defined, as it is here, Windows functions expect wchar* arguments and the xyzW variant is called. When UNICODE is not defined the Windows functions require char* and the xyzA variant is called. The TCHAR and TEXT macros were used to make switching from standard to wide characters less tiresome. However, in general you should use the wide variants on Windows.
    Read this: http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

  6. The following user says thank you to ChrisW67 for this useful post:

    darkrptyp (9th December 2011)

  7. #5
    Join Date
    Nov 2011
    Posts
    6
    Thanks
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: URLDownloadToFile, TCHAR, string, wrong type.

    Qt Code:
    1. uRet = _URLDownloadToFile(0,TEXT("http://www.google.com/index.html"),
    2. TEXT("C:\\index.html"), 0,NULL);
    To copy to clipboard, switch view to plain text mode 

    I've tried also this method - L"some_text" - and uRet is the same.

    uRet returns "-2146697203" (INET_E_UNKNOWN_PROTOCOL)
    The same code from my second post works under DevCpp (add c_str(); to path, filew). I dont get it.
    Is the problem that when I return TEXT("http://www.google.com/index.html") I get sth like that "0x75390000"?

    edit://
    Qt Code:
    1. std::wstring myurl(L"http://www.google.com/index.html" ); // or TEXT()
    2. std::wstring myfile(L"C:\\index.html" ); // or TEXT()
    3. uRet = _URLDownloadToFile(0,myurl.c_str(),myfile.c_str(), 0,NULL);
    To copy to clipboard, switch view to plain text mode 
    the same result.
    Last edited by darkrptyp; 8th December 2011 at 21:29.

  8. #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: URLDownloadToFile, TCHAR, string, wrong type.

    The same code from my second post works under DevCpp (add c_str(); to path, filew). I dont get it.
    Clearly. When you build that code with DevCpp, without any of the Qt stuff presumably, the UNICODE macro is not set and all the function macros are defined to point to the ANSI versions and expect standard C char arguments. When you build it in a Qt project the UNICODE macro is set, because Qt needs it, and all the Windows API functions default to their wide character versions and expect wchar arguments. This is the sort of thing you see in the Windows header files:
    Qt Code:
    1. #ifdef UNICODE
    2. #define LoadLibrary LoadLibraryW
    3. #else
    4. #define LoadLibrary LoadLibraryA
    5. #endif
    To copy to clipboard, switch view to plain text mode 
    So, when you use LoadLibrary() you are actually using LoadLibraryA() or LoadLibraryW() depending on UNICODE. There is no real reason to use the ANSI versions, or the TEXT and associated macros, any longer (as advised in the Microsoft page I linked to).

    At the moment you are trying to mix the 8-bit (ANSI) version of URLDownloadToFileA, which is what you explicitly looked up from the DLL , with the arguments for the wide char version, i.e. URLDownloadToFileW. Look up the wide version or provide narrow char arguments. Your call.

    Is there a reason you want to dynamically load a Windows DLL, rather than link to it at compile time or simply use Qt network classes without any of these hassles?

  9. The following user says thank you to ChrisW67 for this useful post:

    darkrptyp (9th December 2011)

Similar Threads

  1. QTableView, sqlite, wrong data type
    By bzqt in forum Newbie
    Replies: 1
    Last Post: 10th October 2011, 23:56
  2. Replies: 2
    Last Post: 17th December 2010, 08:26
  3. Convert QString to TCHAR
    By sheeeng in forum Qt Programming
    Replies: 1
    Last Post: 8th January 2010, 07:37
  4. Replies: 2
    Last Post: 10th December 2009, 20:51
  5. Using ANSI string type
    By kahahn in forum Newbie
    Replies: 1
    Last Post: 20th June 2009, 23:52

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.