Unix to Windows Porting Dictionary for HPC

Links

Function List

getgrnam


Unix

header file: grp.h

 struct group * getgrnam (const char *groupname) 
 int getgrnam_r (const char *name, struct group *grp, char *buffer, size_t bufsize, struct group **result); 
 struct group * getgrgid (gid_t gid) 
 int getgrgid_r (gid_t gid, struct group *grp, char *buffer, size_t bufsize, struct group **result) 
 struct group * getgrent (void)

Windows

header file: Winbase.h or windows.h

BOOL WINAPI LookupAccountName(
  __in_opt   LPCTSTR lpSystemName,
  __in       LPCTSTR lpAccountName,
  __out_opt  PSID Sid,
  __inout    LPDWORD cbSid,
  __out_opt  LPTSTR ReferencedDomainName,
  __inout    LPDWORD cchReferencedDomainName,
  __out      PSID_NAME_USE peUse
);
      
BOOL WINAPI LookupAccountSid(
  __in_opt   LPCTSTR lpSystemName,
  __in       PSID lpSid,
  __out_opt  LPTSTR lpName,
  __inout    LPDWORD cchName,
  __out_opt  LPTSTR lpReferencedDomainName,
  __inout    LPDWORD cchReferencedDomainName,
  __out      PSID_NAME_USE peUse
);
      

Purpose

Return group information such as name and ID from the user database.

Discussion

Unix systems retrieve user information through a set of standard functions. The function with "_r" suffixes are for threaded programs so that the retrieved information is not corrupted by another thread. The information can be obtained by username or by user ID. The same amount of information is returned by all of the functions listed.

On Unix systems the group and user information requests are handled by separate functions.

On Windows similar information can be obtained though, naturally, with different functions. The information retreived can have different sizes and this is important to remember when porting the code. While ID's on Unix systems are typically 32-bit (16-bit on older systems) Windows's ID are larger (and may vary in size). The equivalent information returned by the single Unix function must be obtained through several Windows function calls. However, an examination of the source will usually reveil that not all of the information is needed and a single, more specific Windows function will be appropriate. An example is obtaining the username by ID to display the username; the information returned by the Unix function such as shell, password expiry, etc. is not needed.

On Windows systems user and group information requests are handled by the same functions. The Windows functions will return a Sid Type. This is important because the information database on Windows contains information for users, groups and computers. The value will allow you to know what type the account is.

Example of Use in Windows

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

BOOL rez;
char Name[MAX_NAME];
char Domain[MAX_NAME]
pSid Sid;
SID_NAME_USE SidType;
DWORD nSize, dSize, SidSize;

/* Assumes you have already obtained the SID */
nSize = sizeof(Name);
dSize = sizeof(Domain);
rez = LookupAccountSid(NULL, Sid, Name, &nSize, Domain, &dSize, &SidType);

/* Make two calls: first to get the SID size, then the full call */
strncpy(Name, "Accounting", sizeof(Name));
SidSize = 0;
rez = LookupAccountName(NULL, Name, Sid, &SidSize, Domain, &dSize, &SidType);
if (rez && SidSize > 0) {
        pSid = (PSID)malloc(Sidsize);
        rez = LookupAccountName(NULL, Name, Sid, &SidSize, Domain, &dSize, &SidType);
        if (SidType == SidTypeGroup) {
                /* something... */
        }
}
    
blog comments powered by Disqus