CreateProcessAsUser redirect the output of command execution to file but output file contains chinese characters are written as “?”

Sriram Muralidharan 21 Reputation points
2021-01-06T14:14:17.22+00:00

I have used CreateProcessAsUser and redirected command execution result to file, but in output file Chinese characters are written as "?".

Command: net user

Here is the code snippet

SetConsoleOutputCP( CP_UTF8 );

PROCESS_INFORMATION pinfo;
STARTUPINFO info;

DWORD errorCode = ERROR_SUCCESS;
LPSTR process = NULL;
HANDLE processHandle = NULL;
HANDLE logHandle = NULL;
LPWSTR processNameW = NULL;
LPTSTR currentDirectory = NULL;
LPVOID lpvEnv = NULL;

DWORD CreationFlags = 0;
int ret = 0;

memset(&pinfo,0,sizeof(pinfo));
memset(&info,0,sizeof(info));
info.cb = sizeof(STARTUPINFO);
DWORD enableLogging = 1;
DWORD timeout = 60;

CreationFlags = CREATE_DEFAULT_ERROR_MODE | CREATE_NO_WINDOW |CREATE_UNICODE_ENVIRONMENT;

if(enableLogging)
{
info.dwFlags = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;

logHandle = CreateFileA ("C:\\temp\\Output.log",GENERIC_WRITE | GENERIC_READ,NULL,&sa,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (logHandle != INVALID_HANDLE_VALUE)
{
    printf("inside log handle\n");
    SetFilePointer (logHandle, 0L, NULL, FILE_END);
    info.hStdError = logHandle;
    info.hStdOutput = logHandle;
}

}
else
{
info.dwFlags = STARTF_USESHOWWINDOW;
}
info.wShowWindow = SW_SHOWNORMAL;

ret = CreateProcessAsUser(NULL,NULL,"cmd.exe /C net user",NULL, NULL,enableLogging, CreationFlags,NULL,NULL,&info,&pinfo);
if(!ret)
{
errorCode = GetLastError();
printf("Failed to Create the Process %d\n",errorCode);
}
else
{
if (timeout == 0)
{
errorCode = ERROR_SUCCESS;
}
else
{
ret = WaitForSingleObject(pinfo.hProcess,timeout*1000*60);
if(ret != WAIT_OBJECT_0)
{
errorCode = WAIT_TIMEOUT;
if(TerminateProcess (pinfo.hProcess, 0))
{
printf("The process has been successfully terminated ..! \n");
}
else
{
printf("The process termination is failed with error code : %d \n",GetLastError());
}
}
}
}
if(enableLogging)
CloseHandle(logHandle);

Here is the output of the net user command execution through win32 application
??????b??

?????z Administrator DefaultAccount

Guest localadmin localuser

Sriram WDAGUtilityAccount

?R?O???????A???o??@?Φh????~?C

How to deal with problem ? Please provide the solution. What am i missing here ?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,649 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,755 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.