tags:

views:

373

answers:

6

Hi..

I'm in the process of porting a C++ library from Linux to Windows, and am having problems with getuid(), which is not supported in Windows.

Any ideas what I can use in its place?

+3  A: 

You can retrieves the name of the user associated with the current thread with GetUserName :

// ANSI version    
string GetWindowsUserNameA()
    {
        char buffer[UNLEN + 1] = {0};
        DWORD buffer_len;
        if (!::GetUserNameA(buffer, & buffer_len))
        {
            // error handling
        }

        return string(buffer);
    }
Samuel_xL
GetUserName() works on every version of windows, you can't trust minimal versions listed on MSDN
Anders
@Anders, you are right, I've edited, thanks.
Samuel_xL
A: 

in DotNet - Environment.UserName

Dani
The question specifies C++. Unless OP is willing to move to C++/CLI, this doesn't help.
ephemient
+1  A: 

Check out Microsoft's recommendations on porting with the Interix (also known as Services for UNIX 3.0) library. Overkill for what you want though.

Epsilon Prime
+1  A: 

Windows' closest equivalent of a UID is (probably) a SID. GetUserName followed by LookupAccountName should get you the user's SID.

Jerry Coffin
+3  A: 

The Windows equivilent is actually the user's SID. You can get this by using the "GetTokenInformation" call and querying for the TokenUser information class.

To call GetTokenInformation, you need a handle to the users token, which you can get by calling OpenProcessToken (or OpenThreadToken if you're impersonating someone).

Larry Osterman
I believe this gets you information about the SID being used to run the process, so it's essentially equivalent to `geteuid()` rather than `getuid()`.
Jerry Coffin
A: 

The right api is SHGetUID(), exported from Shell

marcus