How to work with the MSAdminBase COM object in .NET?
This object is implemented in %windir%\system32\ADMWPROX.DLL - see HKEY_CLASSES_ROOT\CLSID\{70B51430-B6CA-11D0-B9B9-00A0C922E750}
It is perfectly usable from C code, but I wish to replace it with a .NET code and what a bummer - ADMWPROX.DLL does not appear as a valid type library to tlbimp:
z:\Work>tlbimp c:\Windows\system32\admwprox.dll
Microsoft (R) .NET Framework开发者_开发知识库 Type Library to Assembly Converter 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
TlbImp : error TI1002 : The input file 'c:\Windows\system32\admwprox.dll' is not a valid type library.
z:\Work>
And of course, it is missing from the list of COM objects when adding a COM reference in Visual Studio.
PS: I am trying to set the IIS://localhost/W3SVC/1!SslCertHash
property and this article explains that the only way to do it is through the MSAdminBase
object, rather than MSAdminObject
(due to a bug in the schema definition). The latter is conveniently wrapped by the .NET System.DirectoryServices.DirectoryEntry
, but the former is not - hence is my pain.
OK, here is how I did it:
- Converted the IMSAdminBase_W interface definition to IDL. The interface is declared in iadmw.h from the Microsoft SDK. It contains all the IDL attributes inside comments, so the conversion is easy. Of course, the IDL needs to declare the coclass and the library as well.
- Then I compiled the IDL file with midl.
- Next, I ran tlbimp on the resulting tlb file.
- Finally, the produced interop dll had to be fixed, specifically TlbImp attributed the structures with the StructLayout.Pack = 4, which will fail us if the .NET runs as 64 bits - see Access Violation inside inetinfo.exe when passing byte array through IMSAdminBase from a .NET app compiled to AnyCPU. I used Reflector.NET to decompile it. In fact, I then just included the decompiled interop source as part of my project.
That's it.
I did not create the interop source code as the first and only step, because the IMSAdminBase_W interface is quite large, so I preferred to convert it to IDL, which was easier.
精彩评论