Solved TCHAR and WinAPI functions setting in qmake
-
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?
-
change
char buffer[MAX_PATH] = { 0 };
tostd::remove_pointer<LPTSTR>::type buffer[MAX_PATH];
-
You can call GetCurrentDirectoryA directly. But why would you use MBCS in the first place?
-
This post is deleted! -
@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. -
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/
-
@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.
-
@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.
-
DEFINES += MBCS DEFINES -= UNICODE
works! But it was necessary to remove the build folder. Clean wasn't enough for some unclear reasons.
-
@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
-
@spaghetti.coder Actually
DEFINES -= UNICODE
is enough (: -
@JKSH I thought I had rerun qmake, but who knows...