Click to See Complete Forum and Search --> : Which API can retrieve workgroup name?


jsongrts
September 28th, 2006, 07:11 PM
Which API can I use to retrieve the workgroup name? I knew to call GetComputerName/GetComputerNameEx to access the computer name

wildfrog
September 28th, 2006, 07:41 PM
You can do something like this:

#include <windows.h>
#include <Lm.h>

// need to link with Netapi32.lib
#pragma comment(lib, "Netapi32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
// the buffer is allocated by the system
LPWSTR lpNameBuffer;

NET_API_STATUS nas;
NETSETUP_JOIN_STATUS BufferType;

// get info
nas = NetGetJoinInformation(NULL, &lpNameBuffer, &BufferType);

if (nas != NERR_Success)
{
// op failed :(
return 0;
}

switch (BufferType)
{
case NetSetupUnknownStatus:
wprintf(L"Unknown network status!\n");
break;

case NetSetupUnjoined:
wprintf(L"Not joined to any workgroup or domain.\n");
break;

case NetSetupWorkgroupName:
wprintf(L"I'm joined to the workgroup '%s'.\n", lpNameBuffer);
break;

case NetSetupDomainName:
wprintf(L"I'm joined to the domain '%s'.\n", lpNameBuffer);
break;
}

// clean up
NetApiBufferFree(lpNameBuffer);

return 0;
}

- petter

Mouse_103
November 22nd, 2006, 10:16 PM
what compiler do you use?
I had to correct some errors.


You can do something like this:

#include <windows.h>
#include <Lm.h>

// need to link with Netapi32.lib
#pragma comment(lib, "Netapi32.lib")

int _tmain(int argc, _TCHAR* argv[])
{
// the buffer is allocated by the system
LPWSTR lpNameBuffer;

NET_API_STATUS nas;
NETSETUP_JOIN_STATUS BufferType;

// get info
nas = NetGetJoinInformation(NULL, &lpNameBuffer, &BufferType);

if (nas != NERR_Success)
{
// op failed :(
return 0;
}

switch (BufferType)
{
case NetSetupUnknownStatus:
wprintf(L"Unknown network status!\n");
break;

case NetSetupUnjoined:
wprintf(L"Not joined to any workgroup or domain.\n");
break;

case NetSetupWorkgroupName:
wprintf(L"I'm joined to the workgroup '%s'.\n", lpNameBuffer);
break;

case NetSetupDomainName:
wprintf(L"I'm joined to the domain '%s'.\n", lpNameBuffer);
break;
}

// clean up
NetApiBufferFree(lpNameBuffer);

return 0;
}

- petter

wildfrog
November 23rd, 2006, 12:49 AM
what compiler do you use?
I had to correct some errors.
I probably used VC++ 2005. What errors did you correct?

- petter

NoHero
November 23rd, 2006, 11:59 AM
Using NetGetJoinInformation requires Windows XP or Windows 2000 Professional. There you need to set the proper macros to get them:

_WIN32_WINNT>=0x0500
WINVER>=0x0500

wildfrog
November 24th, 2006, 02:48 PM
Using NetGetJoinInformation requires Windows XP or Windows 2000 Professional.Ah, thanks for clarifying that one. :thumb:

- petter