Recent

Author Topic: IInternetSecurityManager and SetZoneMapping SIGSEGV  (Read 3640 times)

ispanec

  • New member
  • *
  • Posts: 5
IInternetSecurityManager and SetZoneMapping SIGSEGV
« on: November 19, 2015, 03:02:07 pm »
Hello everyone!

I try to add site in Trusted Zone (Internet Explorer) using IInternetSecurityManager interface and method SetZoneMapping
https://msdn.microsoft.com/en-us/library/ms537143%28v=vs.85%29.aspx

C++ example:
https://technet.microsoft.com/en-us/ms537181

My test code:
Code: Pascal  [Select][+][-]
  1. program TrustedSite;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, activex, windows
  10.   { you can add units after this };
  11.  
  12. type
  13.   IInternetSecurityManager = interface(IUnknown)['{79eac9ee-baf9-11ce-8c82-00aa004ba90b}']
  14.   function SetZoneMapping(dwZone: DWORD; lpszPattern: LPCWSTR; dwFlags: DWORD): HResult; stdcall;
  15. end;
  16.  
  17.   //function CoInternetCreateSecurityManager(sp: IServiceProvider; var ppSM: IInternetSecurityManager; dwReserved: DWORD): HRESULT;
  18.   //stdcall; external 'urlmon.dll';
  19.  
  20. const
  21.   //CLSID_InternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  22.   CLSID_InternetSecurityManager: TGUID = '{7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4}';
  23.   IID_IInternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  24.  
  25.   URLZONE_ESC_FLAG         = $100;
  26.   URLZONE_LOCAL_MACHINE    = 0;
  27.   URLZONE_INTRANET         = URLZONE_LOCAL_MACHINE + 1;
  28.   URLZONE_TRUSTED          = URLZONE_INTRANET + 1;
  29.   URLZONE_INTERNET         = URLZONE_TRUSTED + 1;
  30.   URLZONE_UNTRUSTED        = URLZONE_INTERNET + 1;
  31.   URLZONE_PREDEFINED_MIN   = 0;
  32.   URLZONE_PREDEFINED_MAX   = 999;
  33.   URLZONE_USER_MIN         = 1000;
  34.   URLZONE_USER_MAX         = 10000;
  35.  
  36.   SZM_CREATE = 0;
  37.   SZM_DELETE = 1;
  38.  
  39. var
  40.   pResult:HRESULT;
  41.   pInternetSecurityManager:IInternetSecurityManager;
  42. begin
  43.  
  44.   WriteLn('Try Create Instance');
  45.  
  46.   CoInitialize(nil);
  47.  
  48.   pResult := CoCreateInstance( CLSID_InternetSecurityManager,
  49.                                nil,
  50.                                CLSCTX_ALL, //CLSCTX_INPROC_SERVER
  51.                                IID_IInternetSecurityManager,
  52.                                pInternetSecurityManager );
  53.  
  54.   if (Failed(pResult)) then begin
  55.       WriteLn('Failed Create Instance');
  56.       ReadLn();
  57.     Exit;
  58.   end;
  59.  
  60.   WriteLn('Instance Created');
  61.  
  62.   pResult :=  pInternetSecurityManager.SetZoneMapping(URLZONE_ESC_FLAG or URLZONE_TRUSTED,
  63.                                           PWideChar(WideString('http://msdn.microsoft.com')), //StringToOleStr('http://msdn.microsoft.com'),
  64.                                           SZM_CREATE);  
  65.  
  66.   CoUninitialize();
  67.  
  68.   {if CoInternetCreateSecurityManager(nil, pInternetSecurityManager, 0) = S_OK then begin
  69.      pInternetSecurityManager.SetZoneMapping(URLZONE_ESC_FLAG or URLZONE_TRUSTED,
  70.                                              PWideChar(WideString('http://msdn.microsoft.com')),
  71.                                              SZM_CREATE);
  72.      WriteLn('End');
  73.   end;      }
  74.  
  75.   WriteLn('End');
  76.   ReadLn();
  77. end.
  78.  

pInternetSecurityManager created, but pInternetSecurityManager.SetZoneMapping generate external SIGSEGV

If i use CoInternetCreateSecurityManager there is the same error on call pInternetSecurityManager.SetZoneMapping

How to avoid this error?

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8797
  • FPC developer.
Re: IInternetSecurityManager and SetZoneMapping SIGSEGV
« Reply #1 on: November 19, 2015, 03:30:53 pm »
You specify that there is only one method in this interface, while MSDN says there are more

ChrisF

  • Hero Member
  • *****
  • Posts: 542
Re: IInternetSecurityManager and SetZoneMapping SIGSEGV
« Reply #2 on: November 19, 2015, 04:28:47 pm »
I agree with marcov.

Use the full interface definition:
Code: Pascal  [Select][+][-]
  1. type
  2.   IInternetSecurityMgrSite = interface
  3.     ['{79eac9ed-baf9-11ce-8c82-00aa004ba90b}']
  4.     function GetWindow(out hwnd: HWnd): HResult; stdcall;
  5.     function EnableModeless(fEnable: BOOL): HResult; stdcall;
  6.   end;
  7.  
  8.   IInternetSecurityManager = interface
  9.     ['{79eac9ee-baf9-11ce-8c82-00aa004ba90b}']
  10.     function SetSecuritySite(Site: IInternetSecurityMgrSite): HResult; stdcall;
  11.     function GetSecuritySite(out Site: IInternetSecurityMgrSite): HResult; stdcall;
  12.     function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD;
  13.       dwFlags: DWORD): HResult; stdcall;
  14.     function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
  15.       var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
  16.     function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
  17.       pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  18.       dwFlags, dwReserved: DWORD): HResult; stdcall;
  19.     function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
  20.       out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  21.       dwReserved: DWORD): HResult; stdcall;
  22.     function SetZoneMapping(dwZone: DWORD; lpszPattern: LPCWSTR;
  23.       dwFlags: DWORD): HResult; stdcall;
  24.     function GetZoneMappings(dwZone: DWORD; out enumString: IEnumString;
  25.       dwFlags: DWORD): HResult; stdcall;
  26.   end;

However, there is still a crash when the program ended; probably something else wrong/incomplete ...

ispanec

  • New member
  • *
  • Posts: 5
Re: IInternetSecurityManager and SetZoneMapping SIGSEGV
« Reply #3 on: November 19, 2015, 06:25:15 pm »
marcov, ChrisF, you are right, i had to specify all methods!

Quote
However, there is still a crash when the program ended; probably something else wrong/incomplete ...
Need to free resources pInternetSecurityManager := nil or use CoInternetCreateSecurityManager

It's work on Windows 7 and IE 11.
CoCreateInstance:
Code: Pascal  [Select][+][-]
  1. program TrustedSite;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, activex, windows
  10.   { you can add units after this };
  11.  
  12. type
  13.   IInternetSecurityMgrSite = interface(IUnknown)
  14.     ['{79eac9ed-baf9-11ce-8c82-00aa004ba90b}']
  15.     function GetWindow(out hwnd: HWnd): HResult; stdcall;
  16.     function EnableModeless(fEnable: BOOL): HResult; stdcall;
  17.   end;
  18.  
  19.   IInternetSecurityManager = interface(IUnknown)
  20.     ['{79eac9ee-baf9-11ce-8c82-00aa004ba90b}']
  21.     function SetSecuritySite(Site: IInternetSecurityMgrSite): HResult; stdcall;
  22.     function GetSecuritySite(out Site: IInternetSecurityMgrSite): HResult; stdcall;
  23.     function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD;
  24.       dwFlags: DWORD): HResult; stdcall;
  25.     function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
  26.       var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
  27.     function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
  28.       pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  29.       dwFlags, dwReserved: DWORD): HResult; stdcall;
  30.     function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
  31.       out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  32.       dwReserved: DWORD): HResult; stdcall;
  33.     function SetZoneMapping(dwZone: DWORD; lpszPattern: LPCWSTR;
  34.       dwFlags: DWORD): HResult; stdcall;
  35.     function GetZoneMappings(dwZone: DWORD; out enumString: IEnumString;
  36.       dwFlags: DWORD): HResult; stdcall;
  37.   end;
  38.  
  39.  
  40. const
  41.   //CLSID_InternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  42.   CLSID_InternetSecurityManager: TGUID = '{7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4}';
  43.   IID_IInternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  44.  
  45.   URLZONE_ESC_FLAG         = $100;
  46.   URLZONE_LOCAL_MACHINE    = 0;
  47.   URLZONE_INTRANET         = URLZONE_LOCAL_MACHINE + 1;
  48.   URLZONE_TRUSTED          = URLZONE_INTRANET + 1;
  49.   URLZONE_INTERNET         = URLZONE_TRUSTED + 1;
  50.   URLZONE_UNTRUSTED        = URLZONE_INTERNET + 1;
  51.   URLZONE_PREDEFINED_MIN   = 0;
  52.   URLZONE_PREDEFINED_MAX   = 999;
  53.   URLZONE_USER_MIN         = 1000;
  54.   URLZONE_USER_MAX         = 10000;
  55.  
  56.   SZM_CREATE = 0;
  57.   SZM_DELETE = 1;
  58.  
  59. var
  60.   pResult:HRESULT;
  61.   pInternetSecurityManager:IInternetSecurityManager;
  62. begin
  63.     WriteLn('Try Create Instance');
  64.  
  65.   CoInitialize(nil);
  66.  
  67.   pResult := CoCreateInstance( CLSID_InternetSecurityManager,
  68.                                nil,
  69.                                CLSCTX_ALL, //CLSCTX_INPROC_SERVER
  70.                                IID_IInternetSecurityManager,
  71.                                pInternetSecurityManager );
  72.  
  73.   if (Failed(pResult)) then begin
  74.       WriteLn('Failed Create Instance');
  75.       ReadLn();
  76.     Exit;
  77.   end;
  78.  
  79.   WriteLn('Instance Created');
  80.  
  81.   pResult :=  pInternetSecurityManager.SetZoneMapping(URLZONE_TRUSTED, //URLZONE_ESC_FLAG or
  82.                                           PWideChar(WideString('https://msdn.microsoft.com')),
  83.                                           SZM_CREATE);
  84.  
  85.  
  86.   pInternetSecurityManager := nil;
  87.   //pInternetSecurityManager._Release;
  88.   CoUninitialize();
  89.  
  90.   WriteLn('End');
  91.   ReadLn();
  92.  
  93. end.
  94.  

CoInternetCreateSecurityManager:
Code: Pascal  [Select][+][-]
  1. program TrustedSite;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}{$IFDEF UseCThreads}
  7.   cthreads,
  8.   {$ENDIF}{$ENDIF}
  9.   Classes, activex, windows
  10.   { you can add units after this };
  11.  
  12. type
  13.   IInternetSecurityMgrSite = interface(IUnknown)
  14.     ['{79eac9ed-baf9-11ce-8c82-00aa004ba90b}']
  15.     function GetWindow(out hwnd: HWnd): HResult; stdcall;
  16.     function EnableModeless(fEnable: BOOL): HResult; stdcall;
  17.   end;
  18.  
  19.   IInternetSecurityManager = interface(IUnknown)
  20.     ['{79eac9ee-baf9-11ce-8c82-00aa004ba90b}']
  21.     function SetSecuritySite(Site: IInternetSecurityMgrSite): HResult; stdcall;
  22.     function GetSecuritySite(out Site: IInternetSecurityMgrSite): HResult; stdcall;
  23.     function MapUrlToZone(pwszUrl: LPCWSTR; out dwZone: DWORD;
  24.       dwFlags: DWORD): HResult; stdcall;
  25.     function GetSecurityId(pwszUrl: LPCWSTR; pbSecurityId: Pointer;
  26.       var cbSecurityId: DWORD; dwReserved: DWORD): HResult; stdcall;
  27.     function ProcessUrlAction(pwszUrl: LPCWSTR; dwAction: DWORD;
  28.       pPolicy: Pointer; cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  29.       dwFlags, dwReserved: DWORD): HResult; stdcall;
  30.     function QueryCustomPolicy(pwszUrl: LPCWSTR; const guidKey: TGUID;
  31.       out pPolicy: Pointer; out cbPolicy: DWORD; pContext: Pointer; cbContext: DWORD;
  32.       dwReserved: DWORD): HResult; stdcall;
  33.     function SetZoneMapping(dwZone: DWORD; lpszPattern: LPCWSTR;
  34.       dwFlags: DWORD): HResult; stdcall;
  35.     function GetZoneMappings(dwZone: DWORD; out enumString: IEnumString;
  36.       dwFlags: DWORD): HResult; stdcall;
  37.   end;
  38.  
  39.   function CoInternetCreateSecurityManager(sp: IServiceProvider; var ppSM: IInternetSecurityManager; dwReserved: DWORD): HRESULT;
  40.   stdcall; external 'urlmon.dll';
  41.  
  42. const
  43.   //CLSID_InternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  44.   CLSID_InternetSecurityManager: TGUID = '{7b8a2d94-0ac9-11d1-896c-00c04fb6bfc4}';
  45.   IID_IInternetSecurityManager: TGUID = '{79eac9ee-baf9-11ce-8c82-00aa004ba90b}';
  46.  
  47.   URLZONE_ESC_FLAG         = $100;
  48.   URLZONE_LOCAL_MACHINE    = 0;
  49.   URLZONE_INTRANET         = URLZONE_LOCAL_MACHINE + 1;
  50.   URLZONE_TRUSTED          = URLZONE_INTRANET + 1;
  51.   URLZONE_INTERNET         = URLZONE_TRUSTED + 1;
  52.   URLZONE_UNTRUSTED        = URLZONE_INTERNET + 1;
  53.   URLZONE_PREDEFINED_MIN   = 0;
  54.   URLZONE_PREDEFINED_MAX   = 999;
  55.   URLZONE_USER_MIN         = 1000;
  56.   URLZONE_USER_MAX         = 10000;
  57.  
  58.   SZM_CREATE = 0;
  59.   SZM_DELETE = 1;
  60.  
  61. var
  62.   pResult:HRESULT;
  63.   pInternetSecurityManager:IInternetSecurityManager;
  64. begin
  65.  
  66.  
  67.   if CoInternetCreateSecurityManager(nil, pInternetSecurityManager, 0) = S_OK then begin
  68.      WriteLn('Internet Security Manager Created');
  69.  
  70.      pResult := pInternetSecurityManager.SetZoneMapping(URLZONE_TRUSTED,    //URLZONE_ESC_FLAG or
  71.                                              PWideChar(WideString('https://msdn.microsoft.com')),
  72.                                              SZM_CREATE);
  73.  
  74.      if (pResult = 0) then
  75.         WriteLn('Site added to trusted');
  76.  
  77.   end;
  78.  
  79.   WriteLn('End');
  80.   ReadLn();
  81. end.          
  82.  

URLZONE_ESC_FLAG or URLZONE_TRUSTED it does not add to trusted sites, use only URLZONE_TRUSTED


Thanks again for your help!

 

TinyPortal © 2005-2018