0

Possible Duplicates:
Delphi: Prompt for UAC elevation when needed
Delphi Administrator rights D7 W7

I'm using Delphi 7 on Windows 7. I need administrator privileges for some function in my application, which needs RAW access to the physical drive. How do I elevate to Administrator from source code? I.E. I chceck rights with:

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 is started as administrator, then return True if not then False. Now if I have False as the result, I want to automatically elevate to Administrator.

I tried manifest elevation to administrator, but if I start the application, then I see the UAC prompt. If i answer NO, then application will not run at all.

EDIT: Or 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
  • @Conor Boyd - that topic has more than 2 years. Many things has changed so far. And i ask differently. – wcale Aug 08 '11 at 02:35
  • The answer I linked to is still 100% relevant to elevation on Windows 7. The age of it is irrelevant. Your question was "how do I elevate in source"; I linked to an answer to that question. – Conor Boyd Aug 08 '11 at 03:19
  • Whether or not a user is logged in as Admin is a completely separate issue to whether or not a process is running elevated. – Conor Boyd Aug 08 '11 at 03:19
  • Ok, if it's not possible, then what about second part of this querstion? – wcale Aug 08 '11 at 03:28
  • If I understand correctly, your second question is "is it possible to disable UAC for a single application using Delphi". I don't know the answer to that (although my belief is that you can't), and I suggest you would be better to ask this as a separate question. – Conor Boyd Aug 08 '11 at 03:39
  • 1
    You started out asking how to elevate your process, which is a duplicate question. That's not the same as disabling UAC. If you want to disable UAC, then please ask that in a separate, independent question, not as an afterthought in this duplicate question. – Rob Kennedy Aug 08 '11 at 03:56
  • I wonder what may be the mental model behind the idea to disable UAC for one application only (and if possible by the application itself). "*But I just need it*" does not count as an argument, because you don't. Is there something wrong with the UAC prompt that induces some kind of german Angst when you see it? – JensG Apr 18 '15 at 22:15

1 Answers1

3

You cannot just "Elevate" for your application mid-execution. What you would have to do is have another application or COM DLL that you call from your application, and you specify the required privileges when doing so.

Nathanial Woolls
  • 5,231
  • 24
  • 32
  • What if i dump application memory to a new address and execute from new address as administrator? – wcale Aug 08 '11 at 02:38
  • 1
    @wcale: Nathanial's answer is correct, and the duplicate I mentioned above goes into it in more detail. – Conor Boyd Aug 08 '11 at 03:17
  • @Conor Boyd What about this part? it's duplicated also? >"EDIT: Or disable UAC only for this application (ParamStr(0)) also from code (after pressing "Disable UAC for this application" button." – wcale Aug 08 '11 at 03:25
  • I've never encountered a way to disable UAC for a single application. You don't want to do that. As Nathanial suggested, the best way is to nicely provide an elevation prompt to the user at the point of execution in your program at which you require elevated privileges. The way that worked nicely for me was to have a separate COM object, request the OS create that COM object in an elevated context when I needed it, and then to hold that COM object alive for as long as I required elevated privileges. Works nicely, doesn't annoy the user, and is the recommended approach to UAC. – Conor Boyd Aug 08 '11 at 03:35
  • 1
    @wcale You cannot disable UAC for single application, and you cannot bypass UAC confirmation. If you could, then the whole point of UAC would be void. – Alexey Ivanov Aug 08 '11 at 05:37