Implement and use console-aware print functions to print strings and resources.

svn path=/trunk/; revision=64998
This commit is contained in:
Eric Kohl 2014-10-25 21:27:05 +00:00
parent 75d471db4b
commit 8caa060f52
7 changed files with 158 additions and 75 deletions

View file

@ -48,7 +48,7 @@ cmdAccounts(
if (_wcsicmp(argv[i], L"/domain") == 0) if (_wcsicmp(argv[i], L"/domain") == 0)
{ {
printf("The /DOMAIN option is not supported yet!\n"); PrintToConsole(L"The /DOMAIN option is not supported yet!\n");
#if 0 #if 0
Domain = TRUE; Domain = TRUE;
#endif #endif
@ -74,7 +74,7 @@ cmdAccounts(
value = wcstoul(p, &endptr, 10); value = wcstoul(p, &endptr, 10);
if (*endptr != 0) if (*endptr != 0)
{ {
printf("You entered an invalid value for the /FORCELOGOFF option.\n"); PrintToConsole(L"You entered an invalid value for the /FORCELOGOFF option.\n");
result = 1; result = 1;
goto done; goto done;
} }
@ -89,7 +89,7 @@ cmdAccounts(
value = wcstoul(p, &endptr, 10); value = wcstoul(p, &endptr, 10);
if (*endptr != 0) if (*endptr != 0)
{ {
printf("You entered an invalid value for the /MINPWLEN option.\n"); PrintToConsole(L"You entered an invalid value for the /MINPWLEN option.\n");
result = 1; result = 1;
goto done; goto done;
} }
@ -111,7 +111,7 @@ cmdAccounts(
value = wcstoul(p, &endptr, 10); value = wcstoul(p, &endptr, 10);
if (*endptr != 0) if (*endptr != 0)
{ {
printf("You entered an invalid value for the /MAXPWAGE option.\n"); PrintToConsole(L"You entered an invalid value for the /MAXPWAGE option.\n");
result = 1; result = 1;
goto done; goto done;
} }
@ -126,7 +126,7 @@ cmdAccounts(
value = wcstoul(p, &endptr, 10); value = wcstoul(p, &endptr, 10);
if (*endptr != 0) if (*endptr != 0)
{ {
printf("You entered an invalid value for the /MINPWAGE option.\n"); PrintToConsole(L"You entered an invalid value for the /MINPWAGE option.\n");
result = 1; result = 1;
goto done; goto done;
} }
@ -140,7 +140,7 @@ cmdAccounts(
value = wcstoul(p, &endptr, 10); value = wcstoul(p, &endptr, 10);
if (*endptr != 0) if (*endptr != 0)
{ {
printf("You entered an invalid value for the /UNIQUEPW option.\n"); PrintToConsole(L"You entered an invalid value for the /UNIQUEPW option.\n");
result = 1; result = 1;
goto done; goto done;
} }
@ -168,51 +168,51 @@ cmdAccounts(
RtlGetNtProductType(&ProductType); RtlGetNtProductType(&ProductType);
printf("Force logoff after: "); PrintToConsole(L"Force logoff after: ");
if (Info0->usrmod0_force_logoff == TIMEQ_FOREVER) if (Info0->usrmod0_force_logoff == TIMEQ_FOREVER)
printf("Never\n"); PrintToConsole(L"Never\n");
else else
printf("%lu seconds\n", Info0->usrmod0_force_logoff); PrintToConsole(L"%lu seconds\n", Info0->usrmod0_force_logoff);
printf("Minimum password age (in days): %lu\n", Info0->usrmod0_min_passwd_age / 86400); PrintToConsole(L"Minimum password age (in days): %lu\n", Info0->usrmod0_min_passwd_age / 86400);
printf("Maximum password age (in days): %lu\n", Info0->usrmod0_max_passwd_age / 86400); PrintToConsole(L"Maximum password age (in days): %lu\n", Info0->usrmod0_max_passwd_age / 86400);
printf("Minimum password length: %lu\n", Info0->usrmod0_min_passwd_len); PrintToConsole(L"Minimum password length: %lu\n", Info0->usrmod0_min_passwd_len);
printf("Password history length: "); PrintToConsole(L"Password history length: ");
if (Info0->usrmod0_password_hist_len == 0) if (Info0->usrmod0_password_hist_len == 0)
printf("None\n"); PrintToConsole(L"None\n");
else else
printf("%lu\n", Info0->usrmod0_password_hist_len); PrintToConsole(L"%lu\n", Info0->usrmod0_password_hist_len);
printf("Lockout threshold: "); PrintToConsole(L"Lockout threshold: ");
if (Info3->usrmod3_lockout_threshold == 0) if (Info3->usrmod3_lockout_threshold == 0)
printf("Never\n"); PrintToConsole(L"Never\n");
else else
printf("%lu\n", Info3->usrmod3_lockout_threshold); PrintToConsole(L"%lu\n", Info3->usrmod3_lockout_threshold);
printf("Lockout duration (in minutes): %lu\n", Info3->usrmod3_lockout_duration / 60); PrintToConsole(L"Lockout duration (in minutes): %lu\n", Info3->usrmod3_lockout_duration / 60);
printf("Lockout observation window (in minutes): %lu\n", Info3->usrmod3_lockout_observation_window / 60); PrintToConsole(L"Lockout observation window (in minutes): %lu\n", Info3->usrmod3_lockout_observation_window / 60);
printf("Computer role: "); PrintToConsole(L"Computer role: ");
if (Info1->usrmod1_role == UAS_ROLE_PRIMARY) if (Info1->usrmod1_role == UAS_ROLE_PRIMARY)
{ {
if (ProductType == NtProductLanManNt) if (ProductType == NtProductLanManNt)
{ {
printf("Primary server\n"); PrintToConsole(L"Primary server\n");
} }
else if (ProductType == NtProductServer) else if (ProductType == NtProductServer)
{ {
printf("Standalone server\n"); PrintToConsole(L"Standalone server\n");
} }
else else
{ {
printf("Workstation\n"); PrintToConsole(L"Workstation\n");
} }
} }
else else
{ {
printf("Backup server\n"); PrintToConsole(L"Backup server\n");
} }
} }

View file

@ -49,12 +49,12 @@ INT cmdHelpMsg(INT argc, WCHAR **argv)
0, 0,
NULL)) NULL))
{ {
printf("\n%S\n", lpBuffer); PrintToConsole(L"\n%s\n", lpBuffer);
LocalFree(lpBuffer); LocalFree(lpBuffer);
} }
else else
{ {
printf("Unrecognized error code: %ld\n", errNum); PrintToConsole(L"Unrecognized error code: %ld\n", errNum);
} }
return 0; return 0;

View file

@ -38,8 +38,8 @@ EnumerateLocalGroups(VOID)
if (Status != NERR_Success) if (Status != NERR_Success)
return Status; return Status;
printf("\nAliases for \\\\%S\n\n", pServer->sv100_name); PrintToConsole(L"\nAliases for \\\\%s\n\n", pServer->sv100_name);
printf("------------------------------------------\n"); PrintToConsole(L"------------------------------------------\n");
NetApiBufferFree(pServer); NetApiBufferFree(pServer);
@ -58,12 +58,10 @@ EnumerateLocalGroups(VOID)
sizeof(PLOCALGROUP_INFO_0), sizeof(PLOCALGROUP_INFO_0),
CompareInfo); CompareInfo);
// printf("dwRead: %lu dwTotal: %lu\n", dwRead, dwTotal);
for (i = 0; i < dwRead; i++) for (i = 0; i < dwRead; i++)
{ {
// printf("%p\n", pBuffer[i].lgrpi0_name);
if (pBuffer[i].lgrpi0_name) if (pBuffer[i].lgrpi0_name)
printf("*%S\n", pBuffer[i].lgrpi0_name); PrintToConsole(L"*%s\n", pBuffer[i].lgrpi0_name);
} }
NetApiBufferFree(pBuffer); NetApiBufferFree(pBuffer);
@ -129,17 +127,17 @@ DisplayLocalGroup(LPWSTR lpGroupName)
pNames[i] = pMembers[i].lgrmi3_domainandname; pNames[i] = pMembers[i].lgrmi3_domainandname;
} }
printf("Alias name %S\n", pGroupInfo->lgrpi1_name); PrintToConsole(L"Alias name %s\n", pGroupInfo->lgrpi1_name);
printf("Comment %S\n", pGroupInfo->lgrpi1_comment); PrintToConsole(L"Comment %s\n", pGroupInfo->lgrpi1_comment);
printf("\n"); PrintToConsole(L"\n");
printf("Members\n"); PrintToConsole(L"Members\n");
printf("\n"); PrintToConsole(L"\n");
printf("------------------------------------------\n"); PrintToConsole(L"------------------------------------------\n");
for (i = 0; i < dwRead; i++) for (i = 0; i < dwRead; i++)
{ {
if (pNames[i]) if (pNames[i])
printf("%S\n", pNames[i]); PrintToConsole(L"%s\n", pNames[i]);
} }
done: done:

View file

@ -57,11 +57,11 @@ EnumerateRunningServices(VOID)
&dwServiceCount, &dwServiceCount,
&dwResumeHandle)) &dwResumeHandle))
{ {
printf("The following services hav been started:\n\n"); PrintToConsole(L"The following services hav been started:\n\n");
for (i = 0; i < dwServiceCount; i++) for (i = 0; i < dwServiceCount; i++)
{ {
printf(" %S\n", lpServiceBuffer[i].lpDisplayName); PrintToConsole(L" %s\n", lpServiceBuffer[i].lpDisplayName);
} }
} }

View file

@ -37,11 +37,11 @@ EnumerateUsers(VOID)
if (Status != NERR_Success) if (Status != NERR_Success)
return Status; return Status;
printf("\nUser accounts for \\\\%S\n\n", pServer->sv100_name); PrintToConsole(L"\nUser accounts for \\\\%s\n\n", pServer->sv100_name);
NetApiBufferFree(pServer); NetApiBufferFree(pServer);
printf("------------------------------------------\n"); PrintToConsole(L"------------------------------------------\n");
Status = NetUserEnum(NULL, Status = NetUserEnum(NULL,
0, 0,
@ -64,7 +64,7 @@ EnumerateUsers(VOID)
{ {
// printf("%p\n", pBuffer[i].lgrpi0_name); // printf("%p\n", pBuffer[i].lgrpi0_name);
if (pBuffer[i].usri0_name) if (pBuffer[i].usri0_name)
printf("%S\n", pBuffer[i].usri0_name); PrintToConsole(L"%s\n", pBuffer[i].usri0_name);
} }
NetApiBufferFree(pBuffer); NetApiBufferFree(pBuffer);
@ -102,7 +102,7 @@ PrintDateTime(DWORD dwSeconds)
TimeBuffer, TimeBuffer,
80); 80);
printf("%S %S\n", DateBuffer, TimeBuffer); PrintToConsole(L"%s %s\n", DateBuffer, TimeBuffer);
} }
@ -110,6 +110,7 @@ static
NET_API_STATUS NET_API_STATUS
DisplayUser(LPWSTR lpUserName) DisplayUser(LPWSTR lpUserName)
{ {
PUSER_MODALS_INFO_0 pUserModals = NULL;
PUSER_INFO_4 pUserInfo = NULL; PUSER_INFO_4 pUserInfo = NULL;
NET_API_STATUS Status; NET_API_STATUS Status;
@ -121,39 +122,56 @@ DisplayUser(LPWSTR lpUserName)
if (Status != NERR_Success) if (Status != NERR_Success)
return Status; return Status;
printf("User name %S\n", pUserInfo->usri4_name); Status = NetUserModalsGet(NULL,
printf("Full name %S\n", pUserInfo->usri4_full_name); 0,
printf("Comment %S\n", pUserInfo->usri4_comment); (LPBYTE*)&pUserModals);
printf("User comment %S\n", pUserInfo->usri4_usr_comment); if (Status != NERR_Success)
printf("Country code %03ld ()\n", pUserInfo->usri4_country_code); goto done;
printf("Account active %S\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
printf("Account expires "); PrintToConsole(L"User name %s\n", pUserInfo->usri4_name);
PrintToConsole(L"Full name %s\n", pUserInfo->usri4_full_name);
PrintToConsole(L"Comment %s\n", pUserInfo->usri4_comment);
PrintToConsole(L"User comment %s\n", pUserInfo->usri4_usr_comment);
PrintToConsole(L"Country code %03ld ()\n", pUserInfo->usri4_country_code);
PrintToConsole(L"Account active %s\n", (pUserInfo->usri4_flags & UF_ACCOUNTDISABLE)? L"No" : ((pUserInfo->usri4_flags & UF_LOCKOUT) ? L"Locked" : L"Yes"));
PrintToConsole(L"Account expires ");
if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER) if (pUserInfo->usri4_acct_expires == TIMEQ_FOREVER)
printf("Never\n"); PrintToConsole(L"Never\n");
else else
PrintDateTime(pUserInfo->usri4_acct_expires); PrintDateTime(pUserInfo->usri4_acct_expires);
printf("\n"); PrintToConsole(L"\n");
printf("Password last set \n"); PrintToConsole(L"Password last set \n");
printf("Password expires \n");
printf("Password changeable \n"); PrintToConsole(L"Password expires ");
printf("Password required \n"); if (pUserModals->usrmod0_max_passwd_age == TIMEQ_FOREVER)
printf("User may change password \n"); PrintToConsole(L"Never\n");
printf("\n"); else
printf("Workstation allowed %S\n", pUserInfo->usri4_workstations); PrintDateTime(pUserInfo->usri4_acct_expires);
printf("Logon script %S\n", pUserInfo->usri4_script_path);
printf("User profile %S\n", pUserInfo->usri4_profile); PrintToConsole(L"Password changeable \n");
printf("Home directory %S\n", pUserInfo->usri4_home_dir); PrintToConsole(L"Password required \n");
printf("Last logon "); PrintToConsole(L"User may change password \n");
PrintToConsole(L"\n");
PrintToConsole(L"Workstation allowed %s\n", pUserInfo->usri4_workstations);
PrintToConsole(L"Logon script %s\n", pUserInfo->usri4_script_path);
PrintToConsole(L"User profile %s\n", pUserInfo->usri4_profile);
PrintToConsole(L"Home directory %s\n", pUserInfo->usri4_home_dir);
PrintToConsole(L"Last logon ");
if (pUserInfo->usri4_last_logon == 0) if (pUserInfo->usri4_last_logon == 0)
printf("Never\n"); PrintToConsole(L"Never\n");
else else
PrintDateTime(pUserInfo->usri4_last_logon); PrintDateTime(pUserInfo->usri4_last_logon);
printf("\n"); PrintToConsole(L"\n");
printf("Logon hours allowed \n"); PrintToConsole(L"Logon hours allowed \n");
printf("\n"); PrintToConsole(L"\n");
printf("Local group memberships \n"); PrintToConsole(L"Local group memberships \n");
printf("Global group memberships \n"); PrintToConsole(L"Global group memberships \n");
done:
if (pUserModals != NULL)
NetApiBufferFree(pUserModals);
if (pUserInfo != NULL) if (pUserInfo != NULL)
NetApiBufferFree(pUserInfo); NetApiBufferFree(pUserInfo);

View file

@ -51,13 +51,70 @@ PrintResourceString(
INT resID, INT resID,
...) ...)
{ {
WCHAR szMsgBuf[MAX_BUFFER_SIZE]; WCHAR szMsgBuffer[MAX_BUFFER_SIZE];
WCHAR szOutBuffer[MAX_BUFFER_SIZE];
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, resID); va_start(arg_ptr, resID);
LoadStringW(GetModuleHandle(NULL), resID, szMsgBuf, MAX_BUFFER_SIZE); LoadStringW(GetModuleHandle(NULL), resID, szMsgBuffer, MAX_BUFFER_SIZE);
vwprintf(szMsgBuf, arg_ptr); _vsnwprintf(szOutBuffer, MAX_BUFFER_SIZE, szMsgBuffer, arg_ptr);
va_end(arg_ptr); va_end(arg_ptr);
WriteToConsole(szOutBuffer);
}
VOID
PrintToConsole(
LPWSTR lpFormat,
...)
{
WCHAR szBuffer[MAX_BUFFER_SIZE];
va_list arg_ptr;
va_start(arg_ptr, lpFormat);
_vsnwprintf(szBuffer, MAX_BUFFER_SIZE, lpFormat, arg_ptr);
va_end(arg_ptr);
WriteToConsole(szBuffer);
}
VOID
WriteToConsole(
LPWSTR lpString)
{
CHAR szOemBuffer[MAX_BUFFER_SIZE * 2];
HANDLE hOutput;
DWORD dwLength;
dwLength = wcslen(lpString);
hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
if ((GetFileType(hOutput) & ~FILE_TYPE_REMOTE) == FILE_TYPE_CHAR)
{
WriteConsoleW(hOutput,
lpString,
dwLength,
&dwLength,
NULL);
}
else
{
dwLength = WideCharToMultiByte(CP_OEMCP,
0,
lpString,
dwLength,
szOemBuffer,
MAX_BUFFER_SIZE * 2,
NULL,
NULL);
WriteFile(hOutput,
szOemBuffer,
dwLength,
&dwLength,
NULL);
}
} }

View file

@ -14,6 +14,7 @@
#include <windef.h> #include <windef.h>
#include <winbase.h> #include <winbase.h>
#include <winnls.h> #include <winnls.h>
#include <wincon.h>
#include <winuser.h> #include <winuser.h>
#include <winsvc.h> #include <winsvc.h>
#include <stdio.h> #include <stdio.h>
@ -29,6 +30,15 @@ PrintResourceString(
INT resID, INT resID,
...); ...);
VOID
PrintToConsole(
LPWSTR lpFormat,
...);
VOID
WriteToConsole(
LPWSTR lpString);
VOID help(VOID); VOID help(VOID);
INT unimplemented(INT argc, WCHAR **argv); INT unimplemented(INT argc, WCHAR **argv);