LoginSignup
4
4

More than 5 years have passed since last update.

PC上の全てのユーザー名を取得するプログラム

Last updated at Posted at 2015-12-09

@7of9 さんと@yumetodo さんの協力のもと、日本語ユーザーも出るようになりました。
netapi32.libのリンカー指定が必要なので注意してください。

main.cpp
#include <Windows.h>
#include <iostream>
#include <LM.h>
int main() {
    PVOID pv;
    DWORD n, i = 0, err;
    std::wcout.imbue(std::locale(""));
    do
    {
        switch (err = NetQueryDisplayInformation(0, 1, i, MAXDWORD, MAX_PREFERRED_LENGTH, &n, &pv))
        {
        case 0:
        case ERROR_MORE_DATA:
            if (n)
            {
                PNET_DISPLAY_USER p = (PNET_DISPLAY_USER)pv;

                do
                {
                    i = p->usri1_next_index;

                    //                  DbgPrint("<%S> <%S>\n", p->usri1_name, p->usri1_comment);
                    std::wcout << p->usri1_name << std::endl;

#if 0   // for C++ Builder XE4
                    OutputDebugString(p->usri1_name);
#endif

                } while (p++, --n);
            }
            NetApiBufferFree(pv);
            break;
        }
    } while (err == ERROR_MORE_DATA);
#if defined(_WIN32) && !(defined(_MSC_VER) && !defined(__clang__))
    std::system("pause");
#endif
    return 0;
}

初代コード(Guest読み取りでエラー)

main.cpp
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <iostream>
#include <LM.h>
#pragma comment(lib, "netapi32.lib")

int main() {
    PNET_DISPLAY_GROUP buf;
    wchar_t szServer[256] = TEXT("");
    DWORD dwRec, i = 0, res;
    do {
        res = NetQueryDisplayInformation(szServer, 1, 0, 10, 1024, &dwRec, (PVOID*)&buf);
        if (res == ERROR_SUCCESS || res == ERROR_MORE_DATA) {
            PNET_DISPLAY_GROUP p = buf;
            for (; dwRec > 0; dwRec--) {
                std::wcout << p->grpi3_name << std::endl;
                i = p->grpi3_next_index;
                p++;
            }
            NetApiBufferFree(buf);
        }
        else std::cout << res << std::endl;
    } while (res == ERROR_MORE_DATA);
    return 0;
}

2代コード(日本語ユーザー名の読み取りができない)

main.cpp
#ifndef UNICODE
#define UNICODE
#endif
#include <Windows.h>
#include <iostream>
#include <LM.h>
#pragma comment(lib, "netapi32.lib")
int main() {
    PVOID pv;
    DWORD n, i = 0, err;
    do
    {
        switch (err = NetQueryDisplayInformation(0, 1, i, MAXDWORD, MAX_PREFERRED_LENGTH, &n, &pv))
        {
        case 0:
        case ERROR_MORE_DATA:
            if (n) {
                PNET_DISPLAY_USER p = (PNET_DISPLAY_USER)pv;

                do{
                    i = p->usri1_next_index;
//                  DbgPrint("<%S> <%S>\n", p->usri1_name, p->usri1_comment);
                    std::wcout << p->usri1_name << std::endl;

#if 0   // for C++ Builder XE4
                    OutputDebugString(p->usri1_name);
#endif

                } while (p++, --n);
            }
            NetApiBufferFree(pv);
            break;
        }
    } while (err == ERROR_MORE_DATA);
    return 0;
}
4
4
10

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4