Delphi建立ActiveX控制元件,實現安全介面及無介面程式碼

Max Woods發表於2014-10-09

Delphi建立OCX控制元件非常的方便,但IE呼叫時彈出的安全認證非常麻煩,有時OCX也不需要介面,IE呼叫時需要隱藏,非常不方便。在DELPHI中建立OCX實現安全介面和建立事件中修改部分程式碼

 

實現安全介面

繼承:IObjectSafety

過載方法:

 function GetInterfaceSafetyOptions(const IID: TIID; pdwSupportedOptions,        //安全介面
      pdwEnabledOptions: PDWORD): HResult; stdcall;
function SetInterfaceSafetyOptions(const IID: TIID; dwOptionSetMask,
      dwEnabledOptions: DWORD): HResult; stdcall;

 


function TZhddMsg.GetInterfaceSafetyOptions(const IID: TIID;
  pdwSupportedOptions, pdwEnabledOptions: PDWORD): HResult;
var
  Unk: IUnknown;
begin
  if (pdwSupportedOptions = nil) or (pdwEnabledOptions = nil) then
  begin
    Result := E_POINTER;
    Exit;
  end;
  Result := QueryInterface(IID, Unk);
  if Result = S_OK then
  begin
    pdwSupportedOptions^ := INTERFACESAFE_FOR_UNTRUSTED_CALLER or
                                             INTERFACESAFE_FOR_UNTRUSTED_DATA;
    pdwEnabledOptions^ :=
      INTERFACESAFE_FOR_UNTRUSTED_CALLER or INTERFACESAFE_FOR_UNTRUSTED_DATA;
  end
  else 
  begin
    pdwSupportedOptions^ := 0;
    pdwEnabledOptions^ := 0;
  end;
end;

function TZhddMsg.SetInterfaceSafetyOptions(const IID: TIID;
  dwOptionSetMask, dwEnabledOptions: DWORD): HResult;
var
  Unk: IUnknown;
begin
  Result := QueryInterface(IID, Unk);
  if Result <> S_OK then Exit;
end;

 

無介面:

initialization
  TActiveFormFactory.Create(
    ComServer,
    TActiveFormControl,
    TZhddMsg,
    Class_ZhddMsg,
    1,
    '',
    OLEMISC_SIMPLEFRAME or OLEMISC_ACTSLIKELABEL,
    //OLEMISC_INVISIBLEATRUNTIME or OLEMISC_ACTSLIKELABEL, //隱藏主介面
    tmApartment);
end.

 

相關文章