Je le code suivant qui obtient les données de l'entrée de zone de texte (winapi pur)GetWindowTextW ne marche pas saisir une grande chaîne
BOOL CALLBACK DlgProc(HWND hw, UINT msg, WPARAM wp, LPARAM lp) {
switch (msg)
{
case WM_INITDIALOG:
SendDlgItemMessage(hw, IDC_EDITMASK, EM_SETLIMITTEXT, 512, 0);
return true;
case WM_CLOSE:
DestroyWindow(hw);
return TRUE;
case WM_COMMAND:
HWND hCtrl;
int length;
wchar_t * text;
switch (LOWORD(wp))
{
case IDCPROCESS:
nElements = 1;
hCtrl = GetDlgItem(hw, IDC_EDITMASK);
length = GetWindowTextLengthW(hCtrl);
if (length == 0) {
MessageBox(hw, L"Неверная маска", L"Ошибка", 0);
return FALSE;
}
text = (wchar_t*)HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, length * sizeof(wchar_t) + sizeof(wchar_t));
GetWindowTextW(hCtrl, text, length + sizeof(wchar_t));
char *test = (char*)text;
int pos = 0;
int startPos = 0;
char dbg[2] = { 0 };
while (pos <= length) {
dbg[0] = text[pos];
OutputDebugStringA(dbg); // here i output the text by characters
if (text[pos] == ',' || pos == length) {
if(!szMasks)
szMasks = (wchar_t**)HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, sizeof(wchar_t*)*nElements);
else
szMasks = (wchar_t**)HeapReAlloc(hProcessHeap, HEAP_ZERO_MEMORY,szMasks, sizeof(wchar_t*)*nElements);
int bufferSize = pos - startPos;
szMasks[nElements - 1] = (wchar_t*)HeapAlloc(hProcessHeap, HEAP_ZERO_MEMORY, (bufferSize + 2) * sizeof(wchar_t));
if(bufferSize % sizeof(wchar_t) != 0)
bufferSize++;
int copyLength = bufferSize/sizeof(wchar_t);
wcsncpy(szMasks[nElements - 1], text + startPos, copyLength);
OutputDebugStringW(szMasks[nElements - 1]);
OutputDebugStringW(L"\r\n");
nElements++;
startPos = pos+1;
}
pos++;
}
searchMasks.count = nElements-1;
searchMasks.szMasks = szMasks;
HeapFree(hProcessHeap, 0, text);
DestroyWindow(hw);
return TRUE;
}
break;
case WM_DESTROY:
PostQuitMessage(0);
return TRUE;
}
return FALSE;
}
Donc, si j'entrer du texte russe par exemple, je reçois la chaîne large valide, tout c'est ok. Si je passe à l'anglais, et l'entrée disons "mot", je reçois le tampon dans le texte qu'il n'est pas formé comme une chaîne large, je m'attends à ce qu'il soit: "w \ 0o \ 0r \ 0d" mais je reçois "mot"
Mais j'obtiens une chaîne normale char*
à la place, ce qui est vraiment mauvais parce que j'ai besoin d'analyser le texte selon une règle, en recherchant le caractère ',' et en copiant les données dans l'autre tampon, en utilisant wcsncpy
, donc je dois toujours avoir une bonne chaîne formatée wchar_t*
. Y at-il un moyen de traiter cela, et pourquoi GetWindowTextW
ne forme pas une chaîne large appropriée? Je compile mon projet en utilisant UNICODE
jeu de caractères, et non multi-octets.
MISE À JOUR DU CODE
char * test = (char*) text
Donnez une chaîne ansi valide, si l'entrée seulement chracters latin dans la zone d'entrée, pas un bon formatage wchar_t*
Comment regardez-vous 'text' que vous pouvez dire que ce n'est pas' w \ 0o \ 0r \ 0d'? – andlabs
Nous ne savons pas ce que * hCtrl * est. S'il vous plaît fournir toutes les informations requises. Dans ce cas: le handle référence-t-il un contrôle? Dans le même processus ou un autre processus? Est-ce un contrôle standard ou un contrôle personnalisé? Dans le cas où il s'agit d'un contrôle personnalisé, affichez le code correspondant. – IInspectable
@andlabs i sortie dans une boucle for – Vlad