|
|||||||
|
|
|
![]() |
|
|
Strumenti |
|
|
#1 |
|
Member
Iscritto dal: Mar 2006
Città: Molfetta
Messaggi: 239
|
[C++] Gestione stringhe unicode e multibyte
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 Codice:
#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 protected]") << endl;
cout << getUserId("[email protected]") << 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;
}
grazie infinite |
|
|
|
|
|
#2 |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
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.
|
|
|
|
|
|
#3 |
|
Bannato
Iscritto dal: Feb 2005
Città: Roma
Messaggi: 7029
|
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 |
|
|
|
|
|
#4 |
|
Member
Iscritto dal: Mar 2006
Città: Molfetta
Messaggi: 239
|
ho modificato la funzione getUserDataFolder e aggiunto la funzione LongToChar (evidenziato in blu):
Codice:
#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 protected]") << endl;
cout << getUserId("[email protected]") << 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;
}
|
|
|
|
|
|
#5 |
|
Utente sospeso
Iscritto dal: Oct 2002
Messaggi: 2156
|
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
__________________
sign editata dallo staff |
|
|
|
|
| Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 09:46.



















