-1

Possible Duplicate:
Delphi: Prompt for UAC elevation when needed

My application written in Delphi 7 for Windows 7 requires administrator privileges for some functionality. How can I elevate it to Administrator from source code?

I check the user rights with this code:

function IsUserAdmin : boolean;
const CAdminSia : TSidIdentifierAuthority = (value: (0, 0, 0, 0, 0, 5));
var sid : PSid;
    ctm : function (token: dword; sid: pointer; var isMember: bool) : bool; stdcall;
    b1  : bool;
begin
  result := false;
  ctm := GetProcAddress(LoadLibrary('advapi32.dll'), 'CheckTokenMembership');
  if (@ctm <> nil) and AllocateAndInitializeSid(CAdminSia, 2, $20, $220, 0, 0, 0, 0, 0, 0, sid) then
  begin
    result := ctm(0, sid, b1) and b1;
    FreeSid(sid);
  end;
end;

If the application started as administrator, then it returns True; if not, then False. Now if I have False as the result I want to automatically elevate the program to Administrator.

I tried with manifest elevate to administrator, but if I start the application, then I see a UAC prompt and if I answer "No", then application will not run at all.

Any chance for help?

I need administrator rights for raw access to the physical drive.

EDIT:

I also tried to disable UAC only for this application (ParamStr(0)) also from code (after pressing "Disable UAC for this application" button).

Community
  • 1
  • 1
wcale
  • 51
  • 9
  • So only users who have administrative rights can use your application? Its apps like this I work with my user base to avoid like the plague. An additional note on UAC, if a user doesn't have the ability to elevate via a UAC prompt the prompt must present them the ability to elevate with alternate username and password. This is not a senerio you've accounted for in your question. – edusysadmin Aug 07 '11 at 23:57
  • > "So only users who have administrative rights can use your application?" No, only some dangerous functions i will inform users about this – wcale Aug 08 '11 at 00:43
  • 3
    @wcale - If your program could just circumvent UAC and make itself run in the administrator scope, UAC would be pretty pointless no? – techie007 Aug 08 '11 at 01:23
  • @techie007 - yes, but you must enter password in this application (cryptography app) or key, next enable hidden drives... if application restart, then user must enter all this again. I can disable UAC checking for entire system, but i don't want to do it. – wcale Aug 08 '11 at 02:06

2 Answers2

1

If you want it to have administrator rights, then you have to go through UAC. You can't elevate to administrator without it showing the UAC prompt, unless UAC is disabled. Obviously, you have to choose YES on the UAC prompt for it to be given administrator privileges.

Eli
  • 636
  • 2
  • 7
  • 17
  • This application start and work with standard rights, no administrator. After press "Show/edit physical drives" Button, application check if its run as administrator. If not, then change to administrator with UAC warning. How to elewate to administrator from code ? UAC may show warning. – wcale Aug 08 '11 at 00:27
  • This application of yours, what does it do? It sounds like an interesting application, surely it isn't just a standard business application. Is it some kind of disk utility application? – Marthinus Aug 08 '11 at 05:26
  • @Marthinus It's cryptography application – wcale Aug 08 '11 at 13:53
1

Processes receive their token at startup and then cannot change them. Thus if you want an app that appears to elevate for some subset of its functionality, that functionality must involve a new process. What you cannot do is elevate an existing process.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490