The problem seems to be in wide character to char conversion.
I have modified your code a bit. Try debug the code and see whether it solves your problem!
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include "Shlwapi.h"
#include "Tlhelp32.h"
#include<wtsapi32.h>
#include <powrprof.h>
#include<windows.h>
#include<winnt.h>
#include <aclapi.h>
#include <psapi.h>
#include <wtsapi32.h>
#ifndef _WIN32_WINNT // Allow use of features specific to Windows X or later.
#define _WIN32_WINNT 0x0501 // Change this to the appropriate value to target other versions of Windows.
#endif
#include <wtsapi32.h>
#include <tchar.h>
#include <string.h>
#pragma comment(lib, "WtsApi32.lib")
#include <iostream>
#include <fstream>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
ofstream outputFile;
outputFile.open("autoreboot.txt");
LPTSTR szUserName = NULL;
LPTSTR szDomainName = NULL;
DWORD dwLen = 0;
BOOL bStatus = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION,
WTSDomainName,
&szDomainName,
&dwLen);
if (bStatus)
{
bStatus = WTSQuerySessionInformation(WTS_CURRENT_SERVER_HANDLE,
WTS_CURRENT_SESSION,
WTSUserName,
&szUserName,
&dwLen);
if (bStatus)
{
DWORD cbUpn = _tcslen(szUserName) + 1 + _tcslen(szDomainName);
LPTSTR szUpn = (LPTSTR)LocalAlloc(0, (cbUpn + 1) * sizeof(TCHAR));
_tcscpy(szUpn, szUserName);
_tcscat(szUpn, _T("@"));
_tcscat(szUpn, szDomainName);
std::wstring tmpString( szUpn );
std::string str( tmpString.begin(), tmpString.end() );
printf("---------------------------------------------------------------------");
outputFile << "\n Active users are : " ;
outputFile << str.c_str();
outputFile << std::endl;
printf("---------------------------------------------------------------------");
LocalFree(szUpn);
WTSFreeMemory(szUserName);
}
else
{
_tprintf(_T("WTSQuerySessionInformation on WTSUserName failed with error 0x%.8X\n"), GetLastError());
}
WTSFreeMemory(szDomainName);
}
else
{
_tprintf(_T("WTSQuerySessionInformation on WTSDomainName failed with error 0x%.8X\n"), GetLastError());
}
outputFile.close();
return 0;
}