🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #5349

Re: Function to enumerate domains?

#5349
From: Oren Halvani
Date: Sun, 04 Sep 2005 22:11
92 lines
3764 bytes
"TerryC" <terrycDONT_SPAM_PLEASE@sweetwaterhsa.SPAM_DIEDIE.com> wrote
in message news:4318b2cc$1@newsgroups.borland.com...
>
> What is the Win32 API function to enumerate the available
> domains?  I have looked at a bunch of Net... functions, many
> want you to supply the relevant domain, but none to give you the
> list of domains.  I'd like to be able to fill a ComboBox for the
> interface.
>
> Thanks,
> Terry



Hi Terry,

have a look on the following function...NOTE i cannot test it on my machine, because it's
a standalone workstation..so i hope it will run on yours...



Oren

/************************************************************************/

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

TFormular *Formular;

__fastcall TFormular::TFormular(TComponent* Owner): TForm(Owner) { }

#define NUM_ELEMENTS(x) (sizeof((x)) / sizeof((x)[0]))

int go(void)
{
    SID_NAME_USE snu;
    TOKEN_USER* ptu;
    HANDLE hProcessToken;
    DWORD dwLen = 0, cbAccountName, cbDomainName;
    TCHAR szAccountName[256], szDomainName[256], szDomainAccount[512];

    if(OpenProcessToken(GetCurrentProcess(), TOKEN_ALL_ACCESS, &hProcessToken))
    {
        GetTokenInformation(hProcessToken, TokenUser, NULL, 0, &dwLen);

        if((dwLen) && (NULL != (ptu = (TOKEN_USER*)HeapAlloc(GetProcessHeap(), 0, dwLen))))
        {
            if(GetTokenInformation(hProcessToken, TokenUser, (PVOID)ptu,  dwLen, &dwLen))
            {
                cbAccountName = NUM_ELEMENTS(szAccountName);
                cbDomainName = NUM_ELEMENTS(szDomainName);

                if(LookupAccountSid(NULL, ptu->User.Sid, szAccountName,
                               &cbAccountName, szDomainName,
                               &cbDomainName, &snu))
                {
                    _tcscpy(szDomainAccount, szDomainName);
                    if(_tcslen(szDomainAccount))
                    {
                        _tcscat(szDomainAccount, TEXT("\\"));
                    }
                    _tcscat(szDomainAccount, szAccountName);
                    _tprintf(TEXT("%s\n"), szDomainAccount);
                }
                else
                {
                    _tprintf(TEXT("LookupAccountSid failed: %u\n"), GetLastError());
                }
            }
            else
            {
                _tprintf(TEXT("GetTokenInformation failed: %u\n"), GetLastError());
            }
            HeapFree(GetProcessHeap(), 0, (PVOID)ptu);
        }
    CloseHandle(hProcessToken);
    }
    else
    {
    _tprintf(TEXT("OpenProcessToken failed: %u\n"), GetLastError());
    }
    return(0);
}

void __fastcall TFormular::Button1Click(TObject *Sender)
{
   String str = IntToStr(go());

   ShowMessage(str);
} 
Technical Details
Message-ID: <431b62be@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!proxad.net!gatel-ffm!gatel-ffm!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail
References: <4318b2cc$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,60e85a5f942051e5
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "Oren Halvani" <HaHaHa@HaHaHa.com>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4318b2cc$1@newsgroups.borland.com>
Subject: Re: Function to enumerate domains?
Date: Mon, 5 Sep 2005 00:11:00 +0200
Lines: 94
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2096
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2096
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 217.132.147.156
Message-ID: <431b62be@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1125868222 217.132.147.156 (4 Sep 2005 14:10:22 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!proxad.net!gatel-ffm!gatel-ffm!news-out.newsgroups.com!propagator2-sterling.newsfeeds.com!in.nntp.be!caladan.landsraad.net!newsgroups.borland.com!not-for-mail