Click here to Skip to main content
15,878,871 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hey guys i am trying from past 2 days the following code works really very well but when its output is being written in text file the output gets display in form of numbers instead of strings. what must i do
i tried several int to string conversion methods but was unsuccessfull.
Below is my code
C++
#include "stdafx.h"
#include <windows.h>
#include <tlhelp32.h>
#include <iostream>


#include <string>
#include <sstream>
#define _WIN32_DCOM

#include <stdio.h>
#include <comdef.h>
//  Include the task header file.
#include <taskschd.h>
# pragma comment(lib, "taskschd.lib")
# pragma comment(lib, "comsupp.lib")

#include <initguid.h>
#include <ole2.h>
#include <mstask.h>
#include <msterr.h>
#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>
#pragma comment(lib, "WtsApi32.lib")





#include <iostream>
#include <fstream>



using namespace std;


int main() {

    ofstream outputFile;

    outputFile.open("autoreboot.txt");




    //---------------------------------------active users--------------------------------------------------------
    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);

            printf("---------------------------------------------------------------------");
            _tprintf(_T("\n Active users are : %s\n"), szUserName);
            outputFile  << "\n Active users are : %s\n" << szUserName;
            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());
    }
    output.close();

    return 0;
}
Posted
Updated 19-May-15 21:42pm
v3
Comments
Richard MacCutchan 20-May-15 3:44am    
the output gets display in form of numbers instead of strings.
What output, where, what numbers? Please explain exactly what results you are getting and why they are incorrect.

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!

// Test.cpp : Defines the entry point for the console application.
//

#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");
 
    //---------------------------------------active users--------------------------------------------------------
    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("---------------------------------------------------------------------");
            //_tprintf(_T("\n Active users are : %s\n"), szUserName);
            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;
}
 
Share this answer
 
v2
Comments
Member 11460314 20-May-15 6:08am    
thanks asif it really works well and also got to know my mistake
I don't see any numbers printed to the output file in your code.

There should be also a compiler error in the line
C++
output.close();


To write variables to streams just pass them using the << operator (see http://www.cplusplus.com/reference/ostream/ostream/operator%3C%3C/[^]). Don't use the printf format sequences. Alternatively use the C file functions (fopen, fprintf, fclose) instead of streams to use identical formatting for screen and file output.
 
Share this answer
 
Comments
Member 11460314 20-May-15 3:58am    
hey output that comes into text file is
Active users are : %s
004D8B80
(which is wrong output)
the actual output should be "qaadmin"
and yes it is outputFile.close();
sorry for misprint
Jochen Arndt 20-May-15 4:03am    
As I wrote: Don't use the printf format sequences.
An read the link. It explains how to use output streams:
outputFile << std::endl << " Active users are : " << szUserName << std::endl;
Member 11460314 20-May-15 4:00am    
i just need that a code which will help me in converting int value to actual output that need to be displayed in textfile.
Jochen Arndt 20-May-15 4:04am    
int val = 42;
outputFile << " val is " << val << std::endl;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900