views:

92

answers:

2

I have an application which runs another application (mine) using the WinAPI "ShellExecuteEx" with the verb "RunAs" so that the other application should start with "Administrator" credentials.

My OS is Windows XP. (Do not bother about other operating system, as I have specific code in place)

The only problem with this thing is that the "Current User" comes preselected. I want it to be the "Following User" with Administrator selected.

How to do this?

+1  A: 

There are no documented settings or anything like that to change the defaults in the runas dialog. If you are willing to use ugly hacks, I have some old code that does it:

HHOOK g_MSRunAsHook;
...

LRESULT SndDlgItemMsg(HWND hDlg,int id,UINT Msg,WPARAM wp=0,LPARAM lp=0) {return SendMessage(GetDlgItem(hDlg,id),Msg,wp,lp);}
LRESULT CALLBACK MSRunAsDlgModProc(int nCode,WPARAM wp,LPARAM lp) 
{
    CWPRETSTRUCT*pCWPS;
    if (nCode >= 0 && (pCWPS=(CWPRETSTRUCT*)lp) && WM_INITDIALOG==pCWPS->message)
    {
        TCHAR buf[30];
        GetClassName(pCWPS->hwnd,buf,ARRAYSIZE(buf));
        if (!lstrcmpi(buf,_T("#32770"))) 
        {
            GetClassName(GetDlgItem(pCWPS->hwnd,0x105),buf,ARRAYSIZE(buf));
            if (!lstrcmpi(buf,_T("SysCredential"))) //is this the correct dialog?
            {
                SndDlgItemMsg(pCWPS->hwnd,0x106,BM_SETCHECK,BST_UNCHECKED);//optional
                SndDlgItemMsg(pCWPS->hwnd,0x104,BM_CLICK);
                SndDlgItemMsg(GetDlgItem(pCWPS->hwnd,0x105),0x3EB,WM_SETTEXT,0,(LPARAM)"Admin name");
                SndDlgItemMsg(GetDlgItem(pCWPS->hwnd,0x105),0x3ED,WM_SETTEXT,0,(LPARAM)"Admin pwd");
            }
        }
    }
    return CallNextHookEx(g_MSRunAsHook,nCode,wp,lp);
}
...

g_MSRunAsHook=NULL;
if(GetOSVerMajor()==5 && GetOSVerMinor()>=1) //only XP/2003 (you must implement GetOSVer* on your own)
{
    g_MSRunAsHook=SetWindowsHookEx(WH_CALLWNDPROCRET,MSRunAsDlgModProc,0,GetCurrentThreadId());
}
ShellExecuteEx(&sei);
if (g_MSRunAsHook)UnhookWindowsHookEx(g_MSRunAsHook);
Anders
I will check this out
Xinxua
A: 

You probably want the CreateProcessWithLogonW Function instead.

Bob Riemersma
I would like the windows to prompt a dialog for the user to enter the credentials rather than my application giving it through code. :)
Xinxua
Well that makes sense. A normal program wouldn't be expected to have these things hard-coded.
Bob Riemersma