Click here to Skip to main content
15,910,009 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: How do I set the default dir for saving in an MFC app? Pin
Bret Faller17-Aug-01 13:32
Bret Faller17-Aug-01 13:32 
GeneralRe: How do I set the default dir for saving in an MFC app? Pin
Jim Howard17-Aug-01 13:44
Jim Howard17-Aug-01 13:44 
QuestionIs current user an Administrator? Pin
Cathy17-Aug-01 11:40
Cathy17-Aug-01 11:40 
AnswerRe: Is current user an Administrator? Pin
Boris Russel17-Aug-01 12:16
Boris Russel17-Aug-01 12:16 
GeneralRe: Is current user an Administrator? Pin
Cathy21-Aug-01 5:53
Cathy21-Aug-01 5:53 
AnswerRe: Is current user an Administrator? Pin
Tim Deveaux17-Aug-01 14:48
Tim Deveaux17-Aug-01 14:48 
GeneralRe: Is current user an Administrator? Pin
Cathy21-Aug-01 5:51
Cathy21-Aug-01 5:51 
GeneralRe: Is current user an Administrator? Pin
Tim Deveaux24-Aug-01 10:46
Tim Deveaux24-Aug-01 10:46 
Hi
OK - I didn't realize that SetPriviledge wasn't an API call - looks like you should work for Bill Smile | :)

I've messed it up a bit more, and humbly submit this orangey splotch of test level code, hastly hacked into an unsuspecting CMainFrame class:

BOOL CMainFrame::SetPriviledge(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege)
{
    TOKEN_PRIVILEGES tp;
    LUID luid;
     
    if ( !LookupPrivilegeValue( 
        NULL,           // lookup privilege on local system
        lpszPrivilege,  // privilege to lookup 
        &luid ) )
    {                   // receives LUID of privilege
        printf("LookupPrivilegeValue error: %u\n", GetLastError() ); 
        return FALSE; 
    }
     
    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    if (bEnablePrivilege)
        tp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    else
        tp.Privileges[0].Attributes = 0;
     
    // Enable the privilege or disable all privileges.
    AdjustTokenPrivileges(
        hToken, 
        FALSE, 
        &tp, 
        sizeof(TOKEN_PRIVILEGES), 
        (PTOKEN_PRIVILEGES) NULL, 
        (PDWORD) NULL);
 
    // Call GetLastError to determine whether the function succeeded.
    int err = GetLastError();
    if(0 != err)
    { 
        LPVOID lpMsgBuf;
        FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM | 
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            err,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL 
            );
        char buf[256];
        sprintf(buf,"SetPriviledge failed:\n\n%s",lpMsgBuf);
        
        // Display the string.  (optional)
        MessageBox( buf, "Error", MB_OK | MB_ICONINFORMATION );
        
        // Free the buffer.
        LocalFree( lpMsgBuf );
 
        return FALSE;
    }
    
    return TRUE;
}
 
/*
We could test/set these priviledges in WINNT.H from VC6
 
#define SE_CREATE_TOKEN_NAME              TEXT("SeCreateTokenPrivilege")
#define SE_ASSIGNPRIMARYTOKEN_NAME        TEXT("SeAssignPrimaryTokenPrivilege")
#define SE_LOCK_MEMORY_NAME               TEXT("SeLockMemoryPrivilege")
#define SE_INCREASE_QUOTA_NAME            TEXT("SeIncreaseQuotaPrivilege")
#define SE_UNSOLICITED_INPUT_NAME         TEXT("SeUnsolicitedInputPrivilege")
#define SE_MACHINE_ACCOUNT_NAME           TEXT("SeMachineAccountPrivilege")
#define SE_TCB_NAME                       TEXT("SeTcbPrivilege")
#define SE_SECURITY_NAME                  TEXT("SeSecurityPrivilege")
#define SE_TAKE_OWNERSHIP_NAME            TEXT("SeTakeOwnershipPrivilege")
#define SE_LOAD_DRIVER_NAME               TEXT("SeLoadDriverPrivilege")
#define SE_SYSTEM_PROFILE_NAME            TEXT("SeSystemProfilePrivilege")
#define SE_SYSTEMTIME_NAME                TEXT("SeSystemtimePrivilege")
#define SE_PROF_SINGLE_PROCESS_NAME       TEXT("SeProfileSingleProcessPrivilege")
#define SE_INC_BASE_PRIORITY_NAME         TEXT("SeIncreaseBasePriorityPrivilege")
#define SE_CREATE_PAGEFILE_NAME           TEXT("SeCreatePagefilePrivilege")
#define SE_CREATE_PERMANENT_NAME          TEXT("SeCreatePermanentPrivilege")
#define SE_BACKUP_NAME                    TEXT("SeBackupPrivilege")
#define SE_RESTORE_NAME                   TEXT("SeRestorePrivilege")
#define SE_SHUTDOWN_NAME                  TEXT("SeShutdownPrivilege")
#define SE_DEBUG_NAME                     TEXT("SeDebugPrivilege")
#define SE_AUDIT_NAME                     TEXT("SeAuditPrivilege")
#define SE_SYSTEM_ENVIRONMENT_NAME        TEXT("SeSystemEnvironmentPrivilege")
#define SE_CHANGE_NOTIFY_NAME             TEXT("SeChangeNotifyPrivilege")
#define SE_REMOTE_SHUTDOWN_NAME           TEXT("SeRemoteShutdownPrivilege")
 
  VS7 (WINNT.H) defines four more - SE_UNDOCK_NAME, SE_SYNC_AGENT_NAME, 
  SE_ENABLE_DELEGATION_NAME and SE_MANAGE_VOLUME_NAME
 
*/
void CMainFrame::OnAdminLookup() 
{
 
    HANDLE hToken; 
    if (!OpenProcessToken( GetCurrentProcess(),
        TOKEN_ADJUST_PRIVILEGES, &hToken) )
    {
        char buf[80];
        sprintf(buf,"OpenProcessToken failed: %u\n", GetLastError() ); 
        MessageBox(buf);
    }  
    else { 
        // Try to enable the SE_TAKE_OWNERSHIP_NAME privilege
        // would indicate admin status if it succeeds...
        if(!SetPriviledge(hToken,SE_TAKE_OWNERSHIP_NAME,TRUE)) {
            // user is not admin...
        }   
    }
}


With the FormatMessage thingy you should get a 1300 ERROR_NOT_ALL_ASSIGNED with the "Not all priviledges referenced are assigned to the caller." message displayed, for whatever that's worth.

Works on Win 2000 as well. Nice work.
GeneralRe: Is current user an Administrator? Pin
Cathy24-Aug-01 11:25
Cathy24-Aug-01 11:25 
GeneralRe: Is current user an Administrator? Pin
Jon Sagara24-Aug-01 11:28
Jon Sagara24-Aug-01 11:28 
GeneralRe: Is current user an Administrator? Pin
Cathy24-Aug-01 11:54
Cathy24-Aug-01 11:54 
GeneralRe: Is current user an Administrator? Pin
Tim Deveaux24-Aug-01 11:34
Tim Deveaux24-Aug-01 11:34 
AnswerRe: Is current user an Administrator? Pin
Jon Sagara24-Aug-01 10:55
Jon Sagara24-Aug-01 10:55 
GeneralRe: Is current user an Administrator? Pin
Cathy24-Aug-01 11:26
Cathy24-Aug-01 11:26 
GeneralHOWTO: Determine if port is available Pin
Matt Weagle17-Aug-01 11:05
Matt Weagle17-Aug-01 11:05 
GeneralRe: HOWTO: Determine if port is available Pin
Tim Deveaux17-Aug-01 15:18
Tim Deveaux17-Aug-01 15:18 
GeneralBrain lock time Pin
17-Aug-01 9:46
suss17-Aug-01 9:46 
GeneralRe: Brain lock time Pin
Boris Russel17-Aug-01 12:22
Boris Russel17-Aug-01 12:22 
GeneralRe: Brain lock time Pin
Boris Russel17-Aug-01 12:25
Boris Russel17-Aug-01 12:25 
GeneralProgress updates from callback function Pin
Jake Palmer17-Aug-01 9:44
Jake Palmer17-Aug-01 9:44 
GeneralRe: Progress updates from callback function Pin
Ben Burnett17-Aug-01 17:28
Ben Burnett17-Aug-01 17:28 
GeneralNew C++ Website! Pin
17-Aug-01 9:39
suss17-Aug-01 9:39 
GeneralCEdit Pin
17-Aug-01 9:30
suss17-Aug-01 9:30 
GeneralRe: CEdit Pin
Jake Palmer17-Aug-01 9:40
Jake Palmer17-Aug-01 9:40 
GeneralRe: CEdit Pin
The_Server18-Aug-01 12:09
The_Server18-Aug-01 12:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.