1

I am attempting to create a program which writes the current username in text form (example John) to file on windows. I tried it through GetUserNameEx(NameDisplay, name, &size); but the output value is

002CF514

I tried this:

#ifndef _UNICODE
#define _UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define SECURITY_WIN32
#include <Security.h>

#include <iostream>
#include <Lmcons.h>
#include <fstream>

#pragma comment(lib, "Secur32.lib")

using namespace std;

int main(void)
{
    TCHAR name[UNLEN + 1];
    DWORD size = UNLEN + 1;

    GetUserNameEx(NameDisplay, name, &size);

    ofstream File;
    File.open("NAME.TXT", ios::app);
    File << name;
    File.close();

    return 0;
}
Marek
  • 147
  • 1
  • 13

1 Answers1

2

Since NameDisplay is a wide string, you have to use wofstream instead of ofstream. Also do not use TCHAR it is awfully deprecated thing. Use wchar_t instead. So correct version should be:

#ifndef _UNICODE
#define _UNICODE
#define UNICODE
#endif

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

#define SECURITY_WIN32
#include <Security.h>

#include <iostream>
#include <Lmcons.h>
#include <fstream>

#pragma comment(lib, "Secur32.lib")

using namespace std;

int main(void)
{
    wchar_t name[UNLEN + 1];
    DWORD size = UNLEN + 1;

    GetUserNameEx(NameDisplay, name, &size);

    std::locale::global(std::locale("Russian_Russia"));
    wofstream File;
    File.open("NAME.TXT", ios::app);
    File << name;
    File.close();

    return 0;
}

Update: Apparently Visual Studio always uses ANSI encoding to write a streams, so you have to imbue a locale to the fstream. Updated version of the code prints my user name in the Cyrillic locale correctly. You would have to change locale name for your country/language. See this answer for the additional info.

Community
  • 1
  • 1
Ari0nhh
  • 5,720
  • 3
  • 28
  • 33
  • *Also do not use TCHAR it is awfully deprecated thing.* [Really?](http://stackoverflow.com/q/234365/464709) Since when? – Frédéric Hamidi Sep 13 '16 at 10:09
  • @FrédéricHamidi Since Windows 98 era actually. – Ari0nhh Sep 13 '16 at 10:10
  • Sorry, that's complete nonsense. `TCHAR` has never been deprecated, and I've personally been using it far later than Windows 98 (and I'm not alone). The question I linked to in my previous comment also (partially) disagrees with you. – Frédéric Hamidi Sep 13 '16 at 10:27
  • Why was this solution downvoted? It solves the problem. The OP's question explicitly defines UNICODE. – Richard Hodges Sep 13 '16 at 10:44
  • @Marek Updated my answer. – Ari0nhh Sep 13 '16 at 11:25
  • @Ari0nhh with this code I again get output 0022FB1C but I need him in text form example: John – Marek Sep 13 '16 at 12:04
  • You need `pubsetbuf` for this to work. Also use Unicode filename instead of ANSI. Use this instead: `wofstream File; wchar_t buf[128]; File.rdbuf()->pubsetbuf(buf, 128); File.open(L"NAME.TXT", ios::app);` – Barmak Shemirani Sep 13 '16 at 19:17
  • @FrédéricHamidi: if you look at the voting on that question, I think you'll see a clear majority against using continuing to use TCHAR. There's just no point any more, and it is dangerous, because it (typically) means that you don't get an error from the compiler if you accidentally try to build for ANSI, the program just doesn't work properly. YMMV. – Harry Johnston Sep 13 '16 at 23:10