用Delphi2007开发的程序,由于需要写文件和访问其它一些系统级资源(如进程\内存等),所以需要使用Administrator身份来运行程序,但不想使用"右键+run as"的方法.用了.manifest文件的方法也没有作用(下面附上.manifest的内容),请教解决办法?附:.manifest文件内容
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="1.0.0.0" processorArchitecture="x86" name="test.exe.manifest" type="win32" />
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*" />
</dependentAssembly>
</dependency>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
</requestedPrivileges>
</security>
</trustInfo>
</assembly>

解决方案 »

  1.   

    要不你就直接提权
     const   
        SE_DEBUG_NAME                               :   PChar   =   'SeDebugPrivilege';   
        SE_Shutdown_NAME                               :   PChar   =   'SeShutdownPrivilege';     
      function   EnableDebugPrivilege(whichPrivilege:String;fEnable:boolean=true):boolean;   
      var   
        fOk   :   boolean;   
        hToken:   THANDLE   ;   
        tp:   TOKEN_PRIVILEGES   ;   
        dwRetLen   :   DWord;   
      begin   
        fOk   :=   false;   
            if   OpenProcessToken(GetCurrentProcess(),   TOKEN_ADJUST_PRIVILEGES   or   TOKEN_QUERY,hToken)   then   
            begin   
                  tp.PrivilegeCount   :=   1;   
                  LookupPrivilegeValue(nil,   PChar(whichPrivilege),   tp.Privileges[0].Luid);   
                  if   fEnable   then   
                      tp.Privileges[0].Attributes   :=   SE_PRIVILEGE_ENABLED   
                      else   
                      tp.Privileges[0].Attributes   :=0;   
                  dwRetLen:=0;   
                  AdjustTokenPrivileges(hToken,   false,   tp,   sizeof(tp),   nil,   dwRetLen);   
                  fOk   :=   (GetLastError()=   ERROR_SUCCESS);   
                  CloseHandle(hToken);   
            end;   
            result:=fOk;   
      end;   
      procedure   WhetherSetPrivileges(whichPrivilege:String;fEnable:boolean=true);   
      begin   
                EnableDebugPrivilege(whichPrivilege,fEnable);   
      end;   
      procedure   ExitWindows98(mode:Longint   );   
      begin   
          WhetherSetPrivileges(SE_Shutdown_NAME);   
          case(mode)   of   
              2:   
                  ExitWindowsEx(EWX_SHUTDOWN   +   EWX_POWEROFF,   0)   ;   
              1:   
                  ExitWindowsEx(EWX_REBOOT   +   EWX_FORCE,   0)   ;   
              0:   
                  ExitWindowsEx(EWX_LOGOFF,   0)   ;   
          end;   
          WhetherSetPrivileges(SE_Shutdown_NAME,false);   
      end;   
      procedure   TForm1.SpeedButton1Click(Sender:   TObject);   
      begin   
      ExitWindows98(cb1.ItemIndex   );   
      end;
    获得admin关机的代码,可以自己改一下,改成获得admin权限后再运行这个程序
      

  2.   

    两种解决方法:1.将此文件取与你的主程序同名并与执行文件放在同一目录下,程序即会调用该manifest文件。
    2.为了不依赖.manifest文件,可以在编写程序时即把这个文件放在资源文件中,首先关闭你的项目。
    制作一个如MYMF.manifest,内容同上。然后编写一个和项目同名的资源源文件,如:MYRC.rc,内容如下:
    1 24 MYMF.manifest
    注意1和24后面都有一个空格。
    如果已经有了资源源文件,则在文件增加上面这一行。
    使用brcc32 project.rc编译这个资源文件。
    注意,此时不要用Delphi打开你的项目,否则Delphi会自动用旧的资源文件覆盖你生成的资源文件。
    好了,打开你的项目,编译运行,以后就不需要再依赖.manifest文件了。
      

  3.   

    似乎只要你把自己的exe命名为setup.exe就够了。
      

  4.   

    function EnableDebugPrivilege: Boolean;
      function EnablePrivilege(hToken: Cardinal; PrivName: string;
    bEnable: Boolean): Boolean;
      var
      TP: TOKEN_PRIVILEGES;
      Dummy: Cardinal;
      begin
      TP.PrivilegeCount := 1;
      LookupPrivilegeValue(nil, pchar(PrivName),
    TP.Privileges[0].Luid);
      if bEnable then
      TP.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED
      else TP.Privileges[0].Attributes := 0;
      AdjustTokenPrivileges(hToken, False, TP, SizeOf(TP), nil,
    Dummy);
      Result := GetLastError = ERROR_SUCCESS;
      end;
    var
      hToken: Cardinal;
    begin
      OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES,
    hToken);
      result:=EnablePrivilege(hToken, 'SeDebugPrivilege', True);
      CloseHandle(hToken);
    end;
      

  5.   

    还是自己找出了问题所在:
    后来发现生成的EXE中已经包含了24号资源,只不过其中level="asInvoker",将其修改成level="requireAdministrator"好了.
    因为EXE中已经有了24号资源,所以便忽略了外部的.manifest文件.(我自己的猜测)分就派给STONE_LJG吧,毕竟他说得没错,
    其它说提升系统权限之类的算是跑题了,
    lextm的说法虽然网上也有这么说,但我尝试过没有用的.谢谢大家.