Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I find the Windows Workgroup name?

991 views
Skip to first unread message

Ace

unread,
Aug 9, 2005, 10:24:37 PM8/9/05
to
Question: How do I find the computer's workgroup name? (programmatically,
that is; please don't tell me to open the Control Panel!! On one Delphi
website that was the only solution given!!!!)

I have spent several hours on the internet trying to answer this question
alone with no success.


Angus Robertson - Magenta Systems Ltd

unread,
Aug 10, 2005, 5:04:00 AM8/10/05
to
In article <42f96563$1...@newsgroups.borland.com>, askaqu...@rogers.vom
(Ace) wrote:

> Question: How do I find the computer's workgroup name?

The workgroup is returned by the NetServerGetInfo API if you request
the domain name, and the PC is not a member of a domain.

It's easiest to use Colin Wilson's NT LAN Manager components from:

http://www.wilsonc.demon.co.uk/delphi_6.htm

Angus

Peter Below (TeamB)

unread,
Aug 10, 2005, 5:22:10 AM8/10/05
to
In article <42f96563$1...@newsgroups.borland.com>, Ace wrote:
> Question: How do I find the computer's workgroup name? (programmatically,
> that is; please don't tell me to open the Control Panel!! On one Delphi
> website that was the only solution given!!!!)
>
The WMI object Win32_ComputerSystem has a Workgroup property that looks
promising. The only problem is that WMI is not available per default on
older platforms.

A MSDN search also turned up a function in the network management API:
NetGetJoinInformation. Like many others of this group of API functions it is
not available on Win9x platforms (although the LAN manager API there may
have a similar function). The JwaLMJoin unit of the JEDI API translation
contains the required declarations.

Then there is the registry. This is notoriously platform-dependent as well,
so you need to check on all platforms you need to support if the key names
are the same there. There used to be a website dedicated to the secrets of
the Windows registry, www.regedit.com or .org i think, may be worth a
trawl...

On Win2K, for example, i see a key

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer

which contains a value "Last Domain", which contains the workgroup name
together with some numeric values that probably distinguish domain logins
from workgroup membership. (The value is "0,2,WORKGROUP" on my home PC).

--
Peter Below (TeamB)
Use the newsgroup archives :
http://www.mers.com/searchsite.html
http://www.tamaracka.com/search.htm
http://groups.google.com
http://www.prolix.be


Ace

unread,
Aug 10, 2005, 9:39:00 PM8/10/05
to
I'm going to save that link as one of my favourite Delphi links. Great
looking stuff!

Ace

unread,
Aug 10, 2005, 9:39:40 PM8/10/05
to
Having your interesting and intelligent comments and terms, Peter, helped me
to do a better web search. I agree that your idea of using the registry
isn't going to work on a variety of machines, so I don't like that idea very
much. Let me show you the code that I found on the web (and adapted very
slightly) for my own use. It seems to work. On my home computer it gets my
workgroup name. I'm hoping that it gets my domain name at the office. I hope
I pasted all the relevant code into this snippet. Frankly, I didn't even try
very hard to understand it. It just works, and sometimes that's part of the
beauty of coding. :-)

type
WKSTA_INFO_100 = Record
wki100_platform_id : DWORD;
wki100_computername : LPWSTR;
wki100_langroup : LPWSTR;
wki100_ver_major : DWORD;
wki100_ver_minor : DWORD;
End;

LPWKSTA_INFO_100 = ^WKSTA_INFO_100;

function GetNetParam(AParam : integer) : string;
Var
PBuf : LPWKSTA_INFO_100;
Res : LongInt;
begin
result := '';
Res := NetWkstaGetInfo (Nil, 100, @PBuf);
If Res = NERR_Success Then
begin
case AParam of
0: Result := string(PBuf^.wki100_computername);
1: Result := string(PBuf^.wki100_langroup);
end;
end;
end;

// Following function returns the name of the local computer:
function GetTheComputerName : string;
begin
Result := GetNetParam(0);
end;

//Following function returns the name of the domain to which the computer
belongs:
function GetTheDomainName : string;
begin
Result := GetNetParam(1);
end;


Ace

unread,
Aug 10, 2005, 10:00:56 PM8/10/05
to
I'm worried about this statement in the source code. It possibly means an
unprivileged user may not be allowed to determine his own domain name. That
would be a shame. Sigh. One step forward, one step back. I might have to
reread your original message for more ideas.

//You must be a member of the Administrators local group to successfully
//execute NetWkstaSetInfo on a remote server or on a computer that has
//local security enabled.


Paul Nicholls

unread,
Aug 11, 2005, 12:06:24 AM8/11/05
to
I Just tried the GetDomainName function and it got my domain name just fine
at my work :)

cheers,
Paul.
<SNIP>


David

unread,
Aug 11, 2005, 6:41:30 AM8/11/05
to
how about using the NetGetJoinInformation method?
My hacked attempt at: http://mystuff.clarke.co.nz/delphistuff.asp


"Ace" <askaqu...@rogers.vom> wrote in message
news:42fa...@newsgroups.borland.com...

Robert Jenkins

unread,
Aug 18, 2005, 4:23:46 PM8/18/05
to
Here is a function that will return user/domain name that I found on the
internet and don't believe it requires special permission to work.


Function GetCurrentUserAndDomain(Var strUser : String; Var strDomain :
String) : Boolean;

Type
PTOKEN_USER = ^TOKEN_USER;
_TOKEN_USER = record
User : TSidAndAttributes;
end;
TOKEN_USER = _TOKEN_USER;

Var hToken : THandle;
Var cbBuf : Cardinal;
var ptiUser : PTOKEN_USER;
Var snu : SID_NAME_USE;
Var chUser : Cardinal;
Var chDomain : Cardinal;
Var szUser : pChar;
Var szDomain : pChar;
Begin
Result := False;

If NOT OpenThreadToken(GetCurrentThread,TOKEN_QUERY,true,hToken) Then
Begin
If (GetLastError <> ERROR_NO_TOKEN) Then Exit;
If NOT (OpenProcessToken(GetCurrentProcess,TOKEN_QUERY,hToken)) Then
Exit;
End;

If NOT (GetTokenInformation(hToken, TokenUser, nil, 0, cbBuf)) Then
If (GetLastError <> ERROR_INSUFFICIENT_BUFFER) Then
Begin
CloseHandle(hToken);
exit;
end;

If (cbBuf = 0) Then Exit;

chDomain := 50;
chUser := 50;

GetMem(ptiUser,cbBuf);
GetMem(szDomain,chDomain);
GetMem(szUser,chUser);

If GetTokenInformation(hToken,TokenUser,ptiUser,cbBuf,cbBuf) Then
Begin
If
(LookupAccountSid(nil,ptiUser.User.Sid,szUser,chUser,szDomain,chDomain,snu))
Then
Begin
Result := True;
strUser := StrPas(szUser);
strDomain := StrPas(szDomain);
End;
End;
CloseHandle(hToken);
FreeMem(ptiUser);
FreeMem(szDomain);
FreeMem(szUser);
End;

"David" <spam...@localhost.org> wrote in message
news:42fb2b5d$1...@newsgroups.borland.com...

Ace

unread,
Aug 20, 2005, 5:32:58 PM8/20/05
to
You're *awesome*, dude. I'll be keeping this code handy.
0 new messages