PDA

View Full Version : [C++] Gestione stringhe unicode e multibyte


whiles_
29-08-2006, 15:10
mi sto avvicinando a c++ per fare un'applicazione che mi serve in particolar modo. Praticamente ho fatto un casino: la funzione API SHGetFolderPath(); ritorna nell'ultimo argomento una stringa unicode che riesco a gestire solo con wcout e wchar_t.


La funzione getUserId, presa come argomento una stringa, la "cripta" con un metodo tutto suo. La funzione GetGlobalDataFolder restituisce la cartella "Dati Applicazioni", mentre GetUserDataFolder unisce le due funzioni restituendo un percorso formato da [GetGlobalDataFolder]/percorso/[getUserId].


Ho provato a convertire tutti i char e i cout in char_t e wcout, ho provato a convertire la stringa di ritorno dall'api in un char ma non ci sono riuscito :cry: Cosa posso fare? non ci capisco più niente , non riesco proprio a farla funzionare :cry: :help:


#define NULL ((void*)0)
#define WIN32_LEAN_AND_MEAN
#include <windows>
#include <stdafx.h>
#include <iostream> // per std::cout e std::endl
#include <pdh.h>
#include <shlobj.h>
#include <string>

using namespace std;

long getUserId(const char *);
wchar_t* getUserDataFolder(const char *);
wchar_t* getGlobalDataFolder();



int main()
{
wcout << getUserDataFolder("email@hotmail.it") << endl;
cout << getUserId("email@hotmail.it") << endl;
wcout << getGlobalDataFolder() << endl;

system("pause");
return 0;
}

long getUserId(const char * user)
{
unsigned int x = 0;
for (int i = 0; i < strlen(user); i++) {
x = x * 101;
x = x + towlower(user[i]);
}
return x;
}


wchar_t* getUserDataFolder(const char* user) {
return getGlobalDataFolder() + L"Microsoft\\MSN Messenger\\" + wchar_t(getUserId(user)) + endl;


}

wchar_t* getGlobalDataFolder() {
TCHAR path[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path);
return path;
}

l'errore che mi da è "error C2110: '+' : cannot add two pointers", e non riesco ad unire quelle stringhe al return... anche riuscire a convertire un char in un wchar_t potrebbe essere un grande passo :(


grazie infinite

71104
29-08-2006, 15:46
in C++ non puoi usare l'operatore + per concatenare due stringhe. per concatenare due stringhe o fai alla vecchia maniera (allochi un buffer abbastanza grande, fai lstrcpy con la prima e lstrcat con la seconda) oppure usi le facilities messe a disposizione da qualche classe, come string delle STL o, meglio ancora, CString delle MFC.

71104
29-08-2006, 15:48
ah, e hai frainteso quello che ti ho detto ieri su MSN: la macro NULL non devi definirla tu, è già definita in quel modo negli headers che hai incluso ^^
credo che stia in stdlib.h

ed inoltre vedo un

#include <windows>

e un

#include <string>

ad entrambi devi mettere l'estensione: i due files si chiamano rispettivamente windows.h e string.h

whiles_
29-08-2006, 16:55
ho modificato la funzione getUserDataFolder e aggiunto la funzione LongToChar (evidenziato in blu):


#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdafx.h>
#include <iostream> // per std::cout e std::endl
#include <pdh.h>
#include <shlobj.h>
#include <string.h>

using namespace std;

long getUserId(const char *);
wchar_t* getUserDataFolder(const char *);
wchar_t* getGlobalDataFolder();
wchar_t* LongToChar(wchar_t *);


int main()
{
wcout << getUserDataFolder("email@hotmail.it") << endl;
cout << getUserId("email@hotmail.it") << endl;
wcout << getGlobalDataFolder() << endl;

system("pause");
return 0;
}

long getUserId(const char * user)
{
unsigned int x = 0;
for (int i = 0; i < strlen(user); i++) {
x = x * 101;
x = x + towlower(user[i]);
}
return x;
}


wchar_t* getUserDataFolder(const char* user) {
wchar_t* rValue;
rValue=getGlobalDataFolder();
const wchar_t* partialPath = L"Microsoft\\MSN Messenger\\";
lstrcat(rValue, partialPath);
lstrcat(rValue, LongToChar(getUserId(user)));
return rValue;


}

wchar_t* LongToChar(long value) {
wchar_t* rValue;
_snwprintf_s(rValue,30,_TRUNCATE,L"%d",value);
return rValue;
}

wchar_t* getGlobalDataFolder() {
TCHAR path[MAX_PATH];
SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, 0, path);
return path;
}

non funziona ancora, mi da un errore a cui non riesco a risalire sul secondo lstrcat (in rosso): error C2664: 'LongToChar' : cannot convert parameter 1 from 'long' to 'wchar_t *'.

BountyKiller
30-08-2006, 11:57
non puoi utilizzare le funzioni di string.h per manipolare stringhe unicode. devi usare le corrispondenti funzioni che trovi in Tchar.h (vado a memoria).

byez ;)