How to preselect Administrator when running an application using ShellExecuteEx with verb "runas"? (Windows XP)
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 开发者_JAVA百科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?
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);
You probably want the CreateProcessWithLogonW Function instead.
精彩评论