function RunTaskAsInteractiveUser(cmdLine, Param, dir: String): Boolean;
const
wdc = 'wdc.dll';
type
TWdcRunTaskAsInteractiveUser = function(pwszCmdLine, pwszPath: PWideChar;
dwDummy: DWORD): HResult; stdcall;
var
WdcRunTaskAsInteractiveUser: TWdcRunTaskAsInteractiveUser;
fullname: string;
sei: SHELLEXECUTEINFO;
e: Integer;
hwdc: Cardinal;
begin
Result := False;
SetLength(fullname, Length(cmdLine));
CopyMemory(PChar(fullname), PChar(cmdLine), ByteLength(cmdLine));
//如果Windows版本>=6
if Win32MajorVersion >= 6 then
begin
hwdc := GetModuleHandle(wdc);
if hwdc = 0 then
hwdc := LoadLibrary(wdc);
@WdcRunTaskAsInteractiveUser := GetProcAddress(hwdc, 'WdcRunTaskAsInteractiveUser');
//如果能找到WdcRunTaskAsInteractiveUser那么就应该是Windows7
if Assigned(WdcRunTaskAsInteractiveUser) then
begin
if Length(Param) > 0 then
fullname := format('"%s" %s', [fullname, Param]);
// fullname + ' ' + Param;
// 最后一个参数39是逆向出来的.不知道含义.TaskMgr给的就是固定的39
// 如果给0的话,EXE等是可以启动的,但是文件夹,MP3等启动不了
e := WdcRunTaskAsInteractiveUser(PChar(fullname), PChar(dir), 39);
Result := e = S_OK;
end;
end;
//如果没执行成功,通常就是没有WdcRunTaskAsInteractiveUser,可能系统是Vista或者XP
if not Result then
begin
//
ZeroMemory(@sei, sizeof(sei));
sei.cbSize := sizeof(SHELLEXECUTEINFO);
sei.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_FLAG_NO_UI;
sei.lpFile := PChar(fullname);
sei.lpVerb := 'Open';//如果这里给'runas'可以以超级用户权限启动
sei.nShow := SW_SHOW;
if Length(Param) > 0 then
sei.lpParameters := PChar(Param)
else
sei.lpParameters := nil;
sei.lpDirectory := PChar(dir);
ShellExecuteEx(@sei);
if sei.hProcess <> 0 then
CloseHandle(sei.hProcess);