Stanislav
Stanislav

Reputation: 33

Is it possible to use Certificate Enrollment API within C# UWP project

My C# UWP project is x64 and it is part of packaging project. I add to my project links CertEnroll.dll and certcli.dll from Windows\SysWow64. Then I add code to create objects from CERTENROLLLib namespace.

using System;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using CERTCLILib;
using CERTENROLLLib;
...
CCspInformation cspInformation = new CCspInformationClass();
cspInformation.InitializeFromName(CngProvider.MicrosoftSoftwareKeyStorageProvider.Provider);

And compiler return error CS1752 “Interop type ‘CCspInformationClass’ cannot be embedded use the applicable interface instead”. After searching how fix this trouble, I found solution: set in project file EmbedInteropTypes attribute to false:

<COMReference Include="CERTENROLLLib">
    <Guid>{728AB348-217D-11DA-B2A4-000E7BBB2B09}</Guid>
    <VersionMajor>1</VersionMajor>
    <VersionMinor>0</VersionMinor>
    <Lcid>0</Lcid>
    <WrapperTool>tlbimp</WrapperTool>
    <Isolated>False</Isolated>
    <EmbedInteropTypes>False</EmbedInteropTypes>
</COMReference>

In this case project was built successfully, but debugging throws exception: Could not load file or assembly 'Interop.CERTENROLLLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified. Then I try remove CertEnroll.dll from my project references and add Interop.CERTENROLLLib.dll. I also set in project file EmbedInteropTypes attribute to false:

<ItemGroup>
   <Reference Include="Interop.CERTENROLLLib">
      <HintPath>.\Interop.CERTENROLLLib.dll</HintPath>
      <EmbedInteropTypes>False</EmbedInteropTypes>
   </Reference>
</ItemGroup>

In this case debugger throws exception at the point of creating instance of CERTENROLLLib.CCspInformationClass:

COMException: Creating an instance of the COM component with CLSID {884E2007-217D-11DA-B2A4-000E7BBB2B09} using CoCreateInstanceFromApp failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)). Please make sure your COM object is in the allowed list of CoCreateInstanceFromApp.

CERTENROLLLib types is not present in the registry. So, I try to register CertEnroll.dll and Interop.CERTENROLLLib.dll using

My project properties:

My project properties

Build configuration:

Build configuration

My project references:

My project references

Upvotes: 0

Views: 530

Answers (0)

Related Questions