Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. TCHAR and WinAPI functions setting in qmake
Forum Updated to NodeBB v4.3 + New Features

TCHAR and WinAPI functions setting in qmake

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 5 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    spaghetti.coder
    wrote on 3 Aug 2018, 11:54 last edited by
    #1

    Hello everyone!

    Let's consider an artificial example:

    main.cpp

    #include <QCoreApplication>
    
    #include <Windows.h>
    #include <tchar.h>
    
    int main(int argc, char *argv[]) {
        QCoreApplication a(argc, argv);
        char buffer[MAX_PATH] = { 0 };
        GetCurrentDirectory(sizeof(buffer), buffer);
        return a.exec();
    }
    

    sample.pro (autogenerated)

    QT += core
    QT -= gui
    
    CONFIG += c++11
    
    TARGET = winapi_sample
    CONFIG += console
    CONFIG -= app_bundle
    
    TEMPLATE = app
    
    SOURCES += main.cpp
    

    I have an error: error: C2664: 'DWORD GetCurrentDirectoryW(DWORD,LPWSTR)': cannot convert argument 2 from 'char [260]' to 'LPWSTR'

    How should I update the pro file to compile this sample in the MBCS configuration?

    1 Reply Last reply
    0
    • V Offline
      V Offline
      VRonin
      wrote on 3 Aug 2018, 14:40 last edited by
      #2

      change char buffer[MAX_PATH] = { 0 }; to std::remove_pointer<LPTSTR>::type buffer[MAX_PATH];

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      0
      • C Offline
        C Offline
        Christian Ehrlicher
        Lifetime Qt Champion
        wrote on 3 Aug 2018, 14:41 last edited by
        #3

        You can call GetCurrentDirectoryA directly. But why would you use MBCS in the first place?

        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
        Visit the Qt Academy at https://academy.qt.io/catalog

        A 1 Reply Last reply 3 Aug 2018, 17:01
        2
        • C Christian Ehrlicher
          3 Aug 2018, 14:41

          You can call GetCurrentDirectoryA directly. But why would you use MBCS in the first place?

          A Offline
          A Offline
          aha_1980
          Lifetime Qt Champion
          wrote on 3 Aug 2018, 17:01 last edited by
          #4
          This post is deleted!
          1 Reply Last reply
          0
          • S Offline
            S Offline
            spaghetti.coder
            wrote on 3 Aug 2018, 17:39 last edited by
            #5

            @VRonin @Christian-Ehrlicher You are right! But do you take into account that it is an artificial example? If it was my problem, I wouldn't ask it here. I have a huge legacy project, which was built with Qt 4. I would like to transfer it on Qt 5, but I have a lot of error which are similar with the one from the sample. I wouldn't like to make thousands char -> TCHAR replacements (or others), I would like to update existing pro file, if it is possible. It would be much simplier and much more effective.

            A 1 Reply Last reply 3 Aug 2018, 17:47
            0
            • S spaghetti.coder
              3 Aug 2018, 17:39

              @VRonin @Christian-Ehrlicher You are right! But do you take into account that it is an artificial example? If it was my problem, I wouldn't ask it here. I have a huge legacy project, which was built with Qt 4. I would like to transfer it on Qt 5, but I have a lot of error which are similar with the one from the sample. I wouldn't like to make thousands char -> TCHAR replacements (or others), I would like to update existing pro file, if it is possible. It would be much simplier and much more effective.

              A Offline
              A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on 3 Aug 2018, 17:47 last edited by
              #6

              @spaghetti.coder

              If I remember correctly the define UNICODE or _UNICODE is responsible for that. So you would need to un-define this symbol.

              You can read a bit more about that here: https://blogs.msdn.microsoft.com/oldnewthing/20040212-00/?p=40643/

              1 Reply Last reply
              1
              • S Offline
                S Offline
                spaghetti.coder
                wrote on 3 Aug 2018, 17:51 last edited by
                #7

                @aha_1980 Yes, you are right. I tried to use constructions like

                DEFINES += _MBCS
                DEFINES += MBCS
                DEFINES -= _UNICODE
                DEFINES -= UNICODЕ
                

                in the pro file, but it didn't help.

                A 1 Reply Last reply 3 Aug 2018, 18:01
                0
                • S spaghetti.coder
                  3 Aug 2018, 17:51

                  @aha_1980 Yes, you are right. I tried to use constructions like

                  DEFINES += _MBCS
                  DEFINES += MBCS
                  DEFINES -= _UNICODE
                  DEFINES -= UNICODЕ
                  

                  in the pro file, but it didn't help.

                  A Offline
                  A Offline
                  aha_1980
                  Lifetime Qt Champion
                  wrote on 3 Aug 2018, 18:01 last edited by
                  #8

                  @spaghetti.coder please show a compiler command line from the compile log. Maybe give a corresponding line from the project against Qt4.

                  I remember having done something like this some years ago, but I will not be able to look into it before next week.

                  PS: If you disable unicode, your program will have problems with non ASCII path names. It might work on your machine, but on a different language it could be complete garbage. But that should have been already the case before.

                  S 1 Reply Last reply 3 Aug 2018, 20:25
                  2
                  • A aha_1980
                    3 Aug 2018, 18:01

                    @spaghetti.coder please show a compiler command line from the compile log. Maybe give a corresponding line from the project against Qt4.

                    I remember having done something like this some years ago, but I will not be able to look into it before next week.

                    PS: If you disable unicode, your program will have problems with non ASCII path names. It might work on your machine, but on a different language it could be complete garbage. But that should have been already the case before.

                    S Offline
                    S Offline
                    spaghetti.coder
                    wrote on 3 Aug 2018, 20:25 last edited by
                    #9

                    @aha_1980

                    DEFINES += MBCS
                    DEFINES -= UNICODE
                    

                    works! But it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.

                    JKSHJ S 2 Replies Last reply 4 Aug 2018, 03:11
                    1
                    • S spaghetti.coder
                      3 Aug 2018, 20:25

                      @aha_1980

                      DEFINES += MBCS
                      DEFINES -= UNICODE
                      

                      works! But it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.

                      JKSHJ Offline
                      JKSHJ Offline
                      JKSH
                      Moderators
                      wrote on 4 Aug 2018, 03:11 last edited by
                      #10

                      @spaghetti.coder said in TCHAR and WinAPI functions setting in qmake:

                      it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.

                      The cleaner checks the Makefile to find out what files should be deleted. This means the cleaner can't (won't) delete the Makefile itself.

                      However, changing the .pro file renders the existing Makefile out-of-date, so you need to generate a new Makefile. The two ways to do so are:

                      • Delete the existing Makefile (which you did when you removed the build folder) and run the whole build process from scratch, or
                      • Run qmake

                      Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                      S 1 Reply Last reply 4 Aug 2018, 06:55
                      2
                      • S spaghetti.coder
                        3 Aug 2018, 20:25

                        @aha_1980

                        DEFINES += MBCS
                        DEFINES -= UNICODE
                        

                        works! But it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.

                        S Offline
                        S Offline
                        spaghetti.coder
                        wrote on 4 Aug 2018, 06:54 last edited by
                        #11

                        @spaghetti.coder Actually DEFINES -= UNICODE is enough (:

                        1 Reply Last reply
                        1
                        • JKSHJ JKSH
                          4 Aug 2018, 03:11

                          @spaghetti.coder said in TCHAR and WinAPI functions setting in qmake:

                          it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.

                          The cleaner checks the Makefile to find out what files should be deleted. This means the cleaner can't (won't) delete the Makefile itself.

                          However, changing the .pro file renders the existing Makefile out-of-date, so you need to generate a new Makefile. The two ways to do so are:

                          • Delete the existing Makefile (which you did when you removed the build folder) and run the whole build process from scratch, or
                          • Run qmake
                          S Offline
                          S Offline
                          spaghetti.coder
                          wrote on 4 Aug 2018, 06:55 last edited by
                          #12

                          @JKSH I thought I had rerun qmake, but who knows...

                          1 Reply Last reply
                          0

                          1/12

                          3 Aug 2018, 11:54

                          • Login

                          • Login or register to search.
                          1 out of 12
                          • First post
                            1/12
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • Users
                          • Groups
                          • Search
                          • Get Qt Extensions
                          • Unsolved