Erreur de conversion de type entre c++ et l'api windows.
Bonjour à tous.
Voici plusieurs jours que je me bats avec ces problèmes sans trouver de solution.
Selon l'environnement de développement que j'utilise, j'ai un morceau de code qui tourne correctement dans certains ou qui génère des erreurs. Je n'arrive pas à me sorti de cette situation.
Voici le morceau de code en question :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <string>
#include <vector>
#include <cstring>
typedef std::pair<std::string, std::string> NameValuePair;
void
loadIniFile( const std::string& iniFilePath,
const std::string& sectionName,
std::vector<NameValuePair>& nameValuePairs ) {
const int bufferSize = 10000;
char buffer[bufferSize] = "";
int charsRead = 0;
charsRead = GetPrivateProfileSection( sectionName.c_str(), //_In_ LPCTSTR lpAppName
buffer, //_Out_ LPTSTR lpReturnedString
bufferSize, //_In_ DWORD nSize
iniFilePath.c_str() ); //_In_ LPCTSTR lpFileName |
Si j'utilise code::blocks ou éclipse CDT, ce code compile sans difficulté (les deux doivent utiliser GCC).
Si j'utilise Qt (compilateur MinGW) j'obtiens cette erreur :
Citation:
erreur : cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' for argument '1' to 'DWORD GetPrivateProfileSectionW(LPCWSTR, LPWSTR, DWORD, LPCWSTR)'
iniFilePath.c_str() );//_In_ LPCTSTR lpFileName
^
Avec MS Visual studio 2010 j'ai cette erreur aussi :
Citation:
error C2664: 'GetPrivateProfileSectionW' : cannot convert parameter 1 from 'const char *' to 'LPCWSTR'
Et enfin sur MS Visual Studio 2015 (compilateur Visual Studio 2015 - Visual C++ 19.0) j'ai plusieurs erreurs :
Citation:
Erreur (active) l'argument de type "const char *" est incompatible avec le paramètre de type "LPCWSTR" GetPrivateProfileSection c:\Users\Serge\Documents\Visual Studio 2015\Projects\GetPrivateProfileSection\GetPrivateProfileSection\GetPrivateProfileSection.cpp 27
Erreur (active) l'argument de type "char *" est incompatible avec le paramètre de type "LPWSTR" GetPrivateProfileSection c:\Users\Serge\Documents\Visual Studio 2015\Projects\GetPrivateProfileSection\GetPrivateProfileSection\GetPrivateProfileSection.cpp 28
Erreur (active) l'argument de type "const char *" est incompatible avec le paramètre de type "LPCWSTR" GetPrivateProfileSection c:\Users\Serge\Documents\Visual Studio 2015\Projects\GetPrivateProfileSection\GetPrivateProfileSection\GetPrivateProfileSection.cpp 30
Erreur C2664 'DWORD GetPrivateProfileSectionW(LPCWSTR,LPWSTR,DWORD,LPCWSTR)'*: impossible de convertir l'argument 1 de 'const char *' en 'LPCWSTR' GetPrivateProfileSection c:\users\serge\documents\visual studio 2015\projects\getprivateprofilesection\getprivateprofilesection\getprivateprofilesection.cpp 30
En synthèse j'aurais ces questions :
1) Pourquoi ce code fonctionne-t-il correctement dans certains cas et pas dans d'autres ?
2) Visiblement, il y a - à minima - une erreur de conversion : ici
Code:
1 2 3 4
|
const std::string& iniFilePath,
....
iniFilePath.c_str() );//_In_ LPCTSTR lpFileName |
Comment correctement effectuer cette conversion ?
3) VS2015 rajoute aussi 2 erreurs de compatibilité :
en ligne 28
Code:
1 2 3 4
|
char buffer[bufferSize] = "";
...
buffer, //_Out_ LPTSTR lpReturnedString |
et en ligne 29
Code:
1 2 3 4
|
const int bufferSize = 10000;
...
bufferSize, //_In_ DWORD nSize |
Comment traiter aussi ces deux erreurs.
Plus généralement peut-on trouver un tableau de conversion entre les types C++ et ceux de l'api windows ?
Merci pour votre aide.