Dmitry Timoshkov wrote:
"Nikolay Sivov" bunglehead@gmail.com wrote:
+UINT WINAPI GetRoleTextA(DWORD role, LPSTR lpRole, UINT rolemax) +{
- UINT length;
- WCHAR *roletextW;
- CHAR *roletextA;
- TRACE("%u %p %u\n", role, lpRole, rolemax);
- length = GetRoleTextW(role, NULL, 0);
- if(length == 0)
return 0;
- roletextW = HeapAlloc(GetProcessHeap(), 0, (length +
1)*sizeof(WCHAR));
- if(!roletextW)
return 0;
- GetRoleTextW(role, roletextW, length + 1);
- length = WideCharToMultiByte( CP_ACP, 0, roletextW, -1, NULL, 0,
NULL, NULL );
- if(!lpRole){
HeapFree(GetProcessHeap(), 0, roletextW);
return length - 1;
- }
- roletextA = HeapAlloc(GetProcessHeap(), 0, length);
- WideCharToMultiByte( CP_ACP, 0, roletextW, -1, roletextA,
length, NULL, NULL );
- if(rolemax < length)
length = rolemax;
- memcpy(lpRole, roletextA, length);
- lpRole[length-1] = '\0';
- HeapFree(GetProcessHeap(), 0, roletextA);
- HeapFree(GetProcessHeap(), 0, roletextW);
- return length - 1;
+}
Why do you need to allocate an intermediate roletextA?
WideCharToMultiByte requires to have a sufficient buffer. I think it's the easiest way to allocate necessary buffer instead of using local buffer of some hardcoded length. What do you think?