4

I've written a dll that runs an Excel add-in (some years ago).

I use this code to retrieve the domain name and that works fine in Windows XP, but it fails in Windows 7.
Only if I run as administrator does it work.
However I don't want to run as administrator because this code is part of an Excel add-in dll and Excel cannot find the user's files if running as admin.

MyReg:= TRegistry.Create;

MyReg.RootKey:= HKEY_LOCAL_MACHINE;
MyReg.OpenKey(RegKeyWin7,false);
NetworkID2:= lowercase(trim(MyReg.ReadString(RegValWin7)));
MyReg.CloseKey;

FreeAndNil(MyReg);

FNetworkOK:= (NetworkID2 = OKRes4);
//Temp check to pinpoint the problem.
if FNetWorkOK = false then ShowMessage('Error wrong domain: '+NetworkID2)
else ShowMessage('all ok');

How do I retrieve the domain name in Windows 7 using Delphi under normal privileges?

Johan
  • 74,508
  • 24
  • 191
  • 319

1 Answers1

7

Use NetWkstaGetInfo in the Win32 API, requesting info via level value = 100.

Return information about the workstation environment, including platform-specific information, the name of the domain and the local computer, and information concerning the operating system. The bufptr parameter points to a WKSTA_INFO_100 structure.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140
  • Thanks, Checked it out, I need to be a authenticated user to access, so I think normal users have the privileges run this. Kind of hate to have to use these horrible Win_api functions, was hoping that was a Delphi wrapper function for that. – Johan Jun 20 '11 at 14:04
  • Yes, Win32 does it all but can be ugly. – Steve Townsend Jun 20 '11 at 14:05
  • @Johan: there are many Delphi examples for NetWkstaGetInfo: http://www.google.com/search?q=NetWkstaGetInfo+delphi, for instance this one http://www.delphi-central.com/tutorials/delphi_user_list.aspx – Jeroen Wiert Pluimers Jun 20 '11 at 14:06
  • 1
    @Jeroen, Yea, I found this one: http://www.delphi-central.com/tutorials/delphi_user_list.aspx He! that's the same one you found, anyway good one because it works on the devel-machine. Lets see if it works in production. – Johan Jun 20 '11 at 14:15
  • @Johan - glad to hear it, all the best – Steve Townsend Jun 20 '11 at 14:46