- Rewrite David Welch's Ansi function wrappers to reduce their code size, improve readability, and fix a bug of freeing of a non-allocated string. Spotted by Christoph vW.

svn path=/trunk/; revision=34096
This commit is contained in:
Aleksey Bragin 2008-06-26 11:49:47 +00:00
parent 00334059ac
commit 968033dd41
2 changed files with 65 additions and 66 deletions

View file

@ -468,35 +468,34 @@ CreateDesktopA(LPCSTR lpszDesktop,
ACCESS_MASK dwDesiredAccess, ACCESS_MASK dwDesiredAccess,
LPSECURITY_ATTRIBUTES lpsa) LPSECURITY_ATTRIBUTES lpsa)
{ {
ANSI_STRING DesktopNameA; UNICODE_STRING DesktopNameU;
UNICODE_STRING DesktopNameU; HDESK hDesktop;
HDESK hDesktop; LPDEVMODEW DevmodeW = NULL;
LPDEVMODEW DevmodeW = NULL;
if (lpszDesktop != NULL) if (lpszDesktop)
{ {
RtlInitAnsiString(&DesktopNameA, (LPSTR)lpszDesktop); /* After conversion, the buffer is zero-terminated */
RtlAnsiStringToUnicodeString(&DesktopNameU, &DesktopNameA, TRUE); RtlCreateUnicodeStringFromAsciiz(&DesktopNameU, lpszDesktop);
} }
else else
{ {
RtlInitUnicodeString(&DesktopNameU, NULL); RtlInitUnicodeString(&DesktopNameU, NULL);
} }
if (pDevmode) if (pDevmode)
{ DevmodeW = GdiConvertToDevmodeW(pDevmode);
DevmodeW = GdiConvertToDevmodeW(pDevmode);
}
hDesktop = CreateDesktopW(DesktopNameU.Buffer, hDesktop = CreateDesktopW(DesktopNameU.Buffer,
NULL, NULL,
DevmodeW, DevmodeW,
dwFlags, dwFlags,
dwDesiredAccess, dwDesiredAccess,
lpsa); lpsa);
RtlFreeUnicodeString(&DesktopNameU); /* Free the string, if it was allocated */
return(hDesktop); if (lpszDesktop) RtlFreeUnicodeString(&DesktopNameU);
return hDesktop;
} }
@ -580,26 +579,28 @@ OpenDesktopA(
BOOL fInherit, BOOL fInherit,
ACCESS_MASK dwDesiredAccess) ACCESS_MASK dwDesiredAccess)
{ {
ANSI_STRING DesktopNameA; UNICODE_STRING DesktopNameU;
UNICODE_STRING DesktopNameU; HDESK hDesktop;
HDESK hDesktop;
if (lpszDesktop != NULL) { if (lpszDesktop)
RtlInitAnsiString(&DesktopNameA, lpszDesktop); {
RtlAnsiStringToUnicodeString(&DesktopNameU, &DesktopNameA, TRUE); /* After conversion, the buffer is zero-terminated */
} else { RtlCreateUnicodeStringFromAsciiz(&DesktopNameU, lpszDesktop);
RtlInitUnicodeString(&DesktopNameU, NULL); }
} else
{
RtlInitUnicodeString(&DesktopNameU, NULL);
}
hDesktop = OpenDesktopW( hDesktop = OpenDesktopW(DesktopNameU.Buffer,
DesktopNameU.Buffer, dwFlags,
dwFlags, fInherit,
fInherit, dwDesiredAccess);
dwDesiredAccess);
RtlFreeUnicodeString(&DesktopNameU); /* Free the string, if it was allocated */
if (lpszDesktop) RtlFreeUnicodeString(&DesktopNameU);
return hDesktop; return hDesktop;
} }

View file

@ -32,29 +32,28 @@ CreateWindowStationA(LPSTR lpwinsta,
ACCESS_MASK dwDesiredAccess, ACCESS_MASK dwDesiredAccess,
LPSECURITY_ATTRIBUTES lpsa) LPSECURITY_ATTRIBUTES lpsa)
{ {
ANSI_STRING WindowStationNameA; UNICODE_STRING WindowStationNameU;
UNICODE_STRING WindowStationNameU; HWINSTA hWinSta;
HWINSTA hWinSta;
if (lpwinsta != NULL) if (lpwinsta)
{ {
RtlInitAnsiString(&WindowStationNameA, lpwinsta); /* After conversion, the buffer is zero-terminated */
RtlAnsiStringToUnicodeString(&WindowStationNameU, &WindowStationNameA, RtlCreateUnicodeStringFromAsciiz(&WindowStationNameU, lpwinsta);
TRUE);
} }
else else
{ {
RtlInitUnicodeString(&WindowStationNameU, NULL); RtlInitUnicodeString(&WindowStationNameU, NULL);
} }
hWinSta = CreateWindowStationW(WindowStationNameU.Buffer, hWinSta = CreateWindowStationW(WindowStationNameU.Buffer,
dwReserved, dwReserved,
dwDesiredAccess, dwDesiredAccess,
lpsa); lpsa);
RtlFreeUnicodeString(&WindowStationNameU); /* Free the string, if it was allocated */
if (lpwinsta) RtlFreeUnicodeString(&WindowStationNameU);
return hWinSta; return hWinSta;
} }
@ -291,28 +290,27 @@ OpenWindowStationA(LPSTR lpszWinSta,
BOOL fInherit, BOOL fInherit,
ACCESS_MASK dwDesiredAccess) ACCESS_MASK dwDesiredAccess)
{ {
ANSI_STRING WindowStationNameA; UNICODE_STRING WindowStationNameU;
UNICODE_STRING WindowStationNameU; HWINSTA hWinSta;
HWINSTA hWinSta;
if (lpszWinSta != NULL) if (lpszWinSta)
{ {
RtlInitAnsiString(&WindowStationNameA, lpszWinSta); /* After conversion, the buffer is zero-terminated */
RtlAnsiStringToUnicodeString(&WindowStationNameU, &WindowStationNameA, RtlCreateUnicodeStringFromAsciiz(&WindowStationNameU, lpszWinSta);
TRUE);
} }
else else
{ {
RtlInitUnicodeString(&WindowStationNameU, NULL); RtlInitUnicodeString(&WindowStationNameU, NULL);
} }
hWinSta = OpenWindowStationW(WindowStationNameU.Buffer, hWinSta = OpenWindowStationW(WindowStationNameU.Buffer,
fInherit, fInherit,
dwDesiredAccess); dwDesiredAccess);
RtlFreeUnicodeString(&WindowStationNameU); /* Free the string, if it was allocated */
if (lpszWinSta) RtlFreeUnicodeString(&WindowStationNameU);
return hWinSta; return hWinSta;
} }