Hello,

I wonder whether anybody already managed to run an elevated instance
of a COM object as mentioned in msdn article below, paragraph
"Shield Implementation and APIs"?
http://msdn.microsoft.com/windowsvista/default.aspx?pull=/library/en-us/dnlong/html/AccProtVista.asp

CoCreateInstanceAsAdmin() is described here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/com/html/1595ebb8-65af-4609-b3e7-a21209e64391.asp

I get it working only when I'm logged in as Admin and the 'consent prompt'
pops up. As a limited User the Elevation UI (login prompt) is displayed,
login succeeds, however my method ShowMessage() isn't shown/executed, why??
Since I'm not familar with COM I may miss something simple, any help is
appreciated.

Arno Garrels


///////////////////////////////////////

uses
    Windows, Sysutils, ActiveX;

type
    PBindOpts3 = ^TBindOpts3;
    {$EXTERNALSYM tagBIND_OPTS3}
    tagBIND_OPTS3 = record
        cbStruct: DWORD;
        grfFlags: DWORD;
        grfMode: DWORD;
        dwTickCountDeadline: DWORD;
        dwTrackFlags: DWORD;
        dwClassContext: DWORD;
        locale: LCID;
        pServerInfo: Pointer; //DUMMY!! //COSERVERINFO * ;
        hwnd: HWND;
    end;
    TBindOpts3 = tagBIND_OPTS3;
    {$EXTERNALSYM BIND_OPTS3}
    BIND_OPTS3 = TBindOpts3;

function CoCreateInstanceAsAdmin(WndHandle: HWND; clsid: TCLSID;
                                 iid: TIID; out ppv): HRESULT;

implementation

function NewCoGetObject(pazName: PWideChar; pBindOptions: PBindOpts3;
         const iid: TIID; out ppv): HResult; stdcall; external 'ole32.dll'
         name 'CoGetObject';

function CoCreateInstanceAsAdmin(WndHandle: HWND; clsid: TCLSID;
    iid: TIID; out ppv): HRESULT;
var
    Bo      : TBindOpts3;
    Moniker : PWideChar;
begin
    Moniker := PWideChar(WideString('Elevation:Administrator!new:' +
                                    GuidToString(clsid)));
    FillChar(Bo, SizeOf(Bo), #0);
    Bo.hwnd           := WndHandle;
    Bo.cbStruct       := SizeOf(Bo);
    Bo.dwClassContext := CLSCTX_LOCAL_SERVER;
    Result            := NewCoGetObject(Moniker, @Bo, iid, ppv);
end;
////////////////////////////////////////////////


procedure TForm1.Button1Click(Sender: TObject);
var
    MyComObj : IDisplaySomething;
begin
    OleCheck(CoCreateInstanceAsAdmin(Self.Handle,
                                     Class_DisplaySomething,
                                     IDisplaySomething,
                                     MyComObj));
    MyComObj.Display('OK!');
end;
_______________________________________________
Delphi mailing list -> [email protected]
http://www.elists.org/mailman/listinfo/delphi

Reply via email to