- 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,15 +468,14 @@ 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
{ {
@ -484,9 +483,7 @@ CreateDesktopA(LPCSTR lpszDesktop,
} }
if (pDevmode) if (pDevmode)
{
DevmodeW = GdiConvertToDevmodeW(pDevmode); DevmodeW = GdiConvertToDevmodeW(pDevmode);
}
hDesktop = CreateDesktopW(DesktopNameU.Buffer, hDesktop = CreateDesktopW(DesktopNameU.Buffer,
NULL, NULL,
@ -495,8 +492,10 @@ CreateDesktopA(LPCSTR lpszDesktop,
dwDesiredAccess, dwDesiredAccess,
lpsa); lpsa);
RtlFreeUnicodeString(&DesktopNameU); /* Free the string, if it was allocated */
return(hDesktop); if (lpszDesktop) RtlFreeUnicodeString(&DesktopNameU);
return hDesktop;
} }
@ -580,24 +579,26 @@ 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);
}
else
{
RtlInitUnicodeString(&DesktopNameU, NULL); 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,15 +32,13 @@ 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
{ {
@ -52,7 +50,8 @@ CreateWindowStationA(LPSTR lpwinsta,
dwDesiredAccess, dwDesiredAccess,
lpsa); lpsa);
RtlFreeUnicodeString(&WindowStationNameU); /* Free the string, if it was allocated */
if (lpwinsta) RtlFreeUnicodeString(&WindowStationNameU);
return hWinSta; return hWinSta;
} }
@ -291,15 +290,13 @@ 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
{ {
@ -310,7 +307,8 @@ OpenWindowStationA(LPSTR lpszWinSta,
fInherit, fInherit,
dwDesiredAccess); dwDesiredAccess);
RtlFreeUnicodeString(&WindowStationNameU); /* Free the string, if it was allocated */
if (lpszWinSta) RtlFreeUnicodeString(&WindowStationNameU);
return hWinSta; return hWinSta;
} }