{"id":950,"date":"2009-06-22T20:10:43","date_gmt":"2009-06-22T18:10:43","guid":{"rendered":"https:\/\/alax.info\/blog\/?p=950"},"modified":"2009-06-22T20:10:43","modified_gmt":"2009-06-22T18:10:43","slug":"regsetkeysecurity-cregkeysetkeysecurity-and-csecuritydesc","status":"publish","type":"post","link":"https:\/\/alax.info\/blog\/950","title":{"rendered":"RegSetKeySecurity, CRegKey::SetKeySecurity and CSecurityDesc"},"content":{"rendered":"<p>One thing is worth special mentioning in connection with previous post on <a href=\"https:\/\/alax.info\/blog\/944\">DirectShow Filter Graph Spy<\/a> on <a href=\"http:\/\/www.microsoft.com\/windows\/windows-vista\/default.aspx\">Microsoft Vista<\/a> system: ATL&#8217;s CSecurityDesc class caused to waste some time.<\/p>\n<pre style=\"background: #ffffff none repeat scroll 0% 0%; color: #000000;\">CRegKey Key<span style=\"color: #800080;\">;<\/span>\r\nATLENSURE_SUCCEEDED<span style=\"color: #808030;\">(<\/span>HRESULT_FROM_WIN32<span style=\"color: #808030;\">(<\/span>Key<span style=\"color: #808030;\">.<\/span>Open<span style=\"color: #808030;\">(<\/span>HKEY_CLASSES_ROOT<span style=\"color: #808030;\">,<\/span> pszKeyName<span style=\"color: #808030;\">,<\/span> READ_CONTROL <span style=\"color: #808030;\">|<\/span> WRITE_OWNER<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nCSecurityDesc AdministratorsOwnerSecurityDescriptor<span style=\"color: #800080;\">;<\/span>\r\nAdministratorsOwnerSecurityDescriptor<span style=\"color: #808030;\">.<\/span>SetOwner<span style=\"color: #808030;\">(<\/span>Sids<span style=\"color: #800080;\">::<\/span>Admins<span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nATLENSURE_SUCCEEDED<span style=\"color: #808030;\">(<\/span>HRESULT_FROM_WIN32<span style=\"color: #808030;\">(<\/span>Key<span style=\"color: #808030;\">.<\/span>SetKeySecurity<span style=\"color: #808030;\">(<\/span>OWNER_SECURITY_INFORMATION<span style=\"color: #808030;\">,<\/span> <span style=\"color: #808030;\">&amp;<\/span>AdministratorsOwnerSecurityDescriptor<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/pre>\n<p>The code compiles fine, but on runtime it gives error <em>87<\/em> (<em>ERROR_INVALID_PARAMETER<\/em>, <em>E_INVALIDARG<\/em>) in the last line, returned from <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa379314(VS.85).aspx\">RegSetKeySecurity<\/a> API call. My first guess was that ATL&#8217;s <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/k0c0e5w6.aspx\">CSecurityDesc<\/a> class for some reason prepared wrong descriptor which resulted in rejecting it as an argument. From the first glance it looks (not sure) that this class deals, to some extent, with structures itself rather than using API functions, so it could be that it results in something looking differently from expected by API calls.<\/p>\n<p>Still the problem is in class itself and its cast from <em>CSecurityDesc&amp;<\/em> to required <em>SECURITY_DESCRIPTOR*<\/em> type. The class only implements operator to automatically cast to <em><span style=\"color: #ff0000;\">const<\/span> SECURITY_DESCRIPTOR*<\/em> type, so the following line would not be passed by compiler:<\/p>\n<pre style=\"background: #ffffff none repeat scroll 0% 0%; color: #000000;\">Key<span style=\"color: #808030;\">.<\/span>SetKeySecurity<span style=\"color: #808030;\">(<\/span>OWNER_SECURITY_INFORMATION<span style=\"color: #808030;\">,<\/span> AdministratorsOwnerSecurityDescriptor<span style=\"color: #808030;\">)<\/span><\/pre>\n<p>However <em><span style=\"color: #ff0000;\">&amp;<\/span>AdministratorsOwnerSecurityDescriptor<\/em> is another level of indirection and hence <em>SECURITY_DESCRIPTOR*<\/em><span style=\"color: #ff0000;\">*<\/span> type, which is passed by compiler, but results in indeed invalid argument.<\/p>\n<p>So in order to correctly convert <em>CSecurityDesc&amp;<\/em> to <em>SECURITY_DESCRIPTOR*<\/em> it can be done this way:<\/p>\n<pre style=\"background: #ffffff none repeat scroll 0% 0%; color: #000000;\">CRegKey Key<span style=\"color: #800080;\">;<\/span>\r\nATLENSURE_SUCCEEDED<span style=\"color: #808030;\">(<\/span>HRESULT_FROM_WIN32<span style=\"color: #808030;\">(<\/span>Key<span style=\"color: #808030;\">.<\/span>Open<span style=\"color: #808030;\">(<\/span>HKEY_CLASSES_ROOT<span style=\"color: #808030;\">,<\/span> pszKeyName<span style=\"color: #808030;\">,<\/span> READ_CONTROL <span style=\"color: #808030;\">|<\/span> WRITE_OWNER<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nCSecurityDesc AdministratorsOwnerSecurityDescriptor<span style=\"color: #800080;\">;<\/span>\r\nAdministratorsOwnerSecurityDescriptor<span style=\"color: #808030;\">.<\/span>SetOwner<span style=\"color: #808030;\">(<\/span>Sids<span style=\"color: #800080;\">::<\/span>Admins<span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span>\r\nATLENSURE_SUCCEEDED<span style=\"color: #808030;\">(<\/span>HRESULT_FROM_WIN32<span style=\"color: #808030;\">(<\/span>Key<span style=\"color: #808030;\">.<\/span>SetKeySecurity<span style=\"color: #808030;\">(<\/span>OWNER_SECURITY_INFORMATION<span style=\"color: #808030;\">,<\/span> const_cast<span style=\"color: #808030;\">&lt;<\/span><span style=\"color: #603000;\">SECURITY_DESCRIPTOR<\/span><span style=\"color: #808030;\">*<\/span><span style=\"color: #808030;\">&gt;<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #808030;\">(<\/span><span style=\"color: #800000; font-weight: bold;\">const<\/span> <span style=\"color: #603000;\">SECURITY_DESCRIPTOR<\/span><span style=\"color: #808030;\">*<\/span><span style=\"color: #808030;\">)<\/span> AdministratorsOwnerSecurityDescriptor<span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #808030;\">)<\/span><span style=\"color: #800080;\">;<\/span><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>One thing is worth special mentioning in connection with previous post on DirectShow Filter Graph Spy on Microsoft Vista system: ATL&#8217;s CSecurityDesc class caused to waste some time. CRegKey Key; ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(Key.Open(HKEY_CLASSES_ROOT, pszKeyName, READ_CONTROL | WRITE_OWNER))); CSecurityDesc AdministratorsOwnerSecurityDescriptor; AdministratorsOwnerSecurityDescriptor.SetOwner(Sids::Admins()); ATLENSURE_SUCCEEDED(HRESULT_FROM_WIN32(Key.SetKeySecurity(OWNER_SECURITY_INFORMATION, &amp;AdministratorsOwnerSecurityDescriptor))); The code compiles fine, but on runtime it gives error 87 (ERROR_INVALID_PARAMETER, E_INVALIDARG) in the&hellip; <\/p>\n<p><a class=\"moretag\" href=\"https:\/\/alax.info\/blog\/950\">Read the full article<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,13],"tags":[81,487,208,210,123,207,128,209,150],"class_list":["post-950","post","type-post","status-publish","format-standard","hentry","category-atl","category-source","tag-api","tag-atl","tag-cregkey","tag-csecuritydesc","tag-registry","tag-regsetkeysecurity","tag-security","tag-setkeysecurity","tag-windows"],"_links":{"self":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/950","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/comments?post=950"}],"version-history":[{"count":0,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/posts\/950\/revisions"}],"wp:attachment":[{"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/media?parent=950"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/categories?post=950"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/alax.info\/blog\/wp-json\/wp\/v2\/tags?post=950"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}