Type a page name and press Enter. You'll jump to the page if it exists, or you can create it if it doesn't.
To create a page in a module other than crypt32, prefix the name with the module name and a period.
CertOpenSystemStore (crypt32)
.
Summary
This function is a simplified function used to open the most common system certificate store. To open certificate stores with more complex requirements, such as file-based or memory-based stores, use the
CertOpenStore function.
C# Signature:
[DllImport("crypt32.dll", CharSet=CharSet.Auto, SetLastError=true)]
static extern IntPtr CertOpenSystemStore(IntPtr hCryptProv, string storename);
VB .NET Signature:
<DllImport("Crypt32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
Private Shared Function CertOpenSystemStore( _
ByVal hCryptProv As IntPtr, _
ByVal storename As String) As IntPtr
End Function
User-Defined Types:
None.
Notes:
Try MSDN: http://msdn.microsoft.com/en-us/library/ms867087.aspx
The sample code below seems to be taken from this.
None.
Tips & Tricks:
Please add some!
Sample Code:
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel;
using System;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.ComponentModel;
public class WinCapi
{
public class WinCapi {
/*
HCERTSTORE WINAPI CertOpenSystemStore(HCRYPTPROV hprov, LPTCSTR szSubsystemProtocol);
BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags);
/*
HCERTSTORE WINAPI CertOpenSystemStore(HCRYPTPROV hprov, LPTCSTR szSubsystemProtocol);
BOOL WINAPI CertCloseStore(HCERTSTORE hCertStore, DWORD dwFlags);
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingType,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContext);
PCCERT_CONTEXT WINAPI CertFindCertificateInStore(
HCERTSTORE hCertStore,
DWORD dwCertEncodingType,
DWORD dwFindFlags,
DWORD dwFindType,
const void* pvFindPara,
PCCERT_CONTEXT pPrevCertContext);
BOOL WINAPI CertFreeCertificateContext(
PCCERT_CONTEXT pCertContext
); */
[DllImport("crypt32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename);
BOOL WINAPI CertFreeCertificateContext(
PCCERT_CONTEXT pCertContext
);
*/
[DllImport("crypt32.dll", SetLastError = true)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags);
[DllImport("crypt32.dll", CharSet=CharSet.Auto, SetLastError=true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
string storename) ;
[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
[In, MarshalAs(UnmanagedType.LPWStr)]String pszFindString,
IntPtr pPrevCertCntxt);
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertCloseStore(
IntPtr hCertStore,
uint dwFlags) ;
[DllImport("crypt32.dll", SetLastError = true)]
public static extern bool CertFreeCertificateContext(
IntPtr hCertStore);
}
[DllImport("crypt32.dll", SetLastError=true)]
public static extern IntPtr CertFindCertificateInStore(
IntPtr hCertStore,
uint dwCertEncodingType,
uint dwFindFlags,
uint dwFindType,
[In, MarshalAs(UnmanagedType.LPWStr)]String pszFindString,
IntPtr pPrevCertCntxt) ;
public class SimpleCert
{
const string MY = "MY";
const string OTHERS = "AddressBook";
const uint PKCS_7_ASN_ENCODING = 0x00010000;
const uint X509_ASN_ENCODING = 0x00000001;
const uint CERT_FIND_SUBJECT_STR = 0x00080007;
[DllImport("crypt32.dll", SetLastError=true)]
public static extern bool CertFreeCertificateContext(
IntPtr hCertStore) ;
}
static uint MY_ENCODING_TYPE = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING;
public class SimpleCert {
const string MY = "MY";
const string OTHERS = "AddressBook";
const uint PKCS_7_ASN_ENCODING = 0x00010000;
const uint X509_ASN_ENCODING = 0x00000001;
const uint CERT_FIND_SUBJECT_STR = 0x00080007;
static string lpszCertSubject = "searched name";
static uint MY_ENCODING_TYPE = PKCS_7_ASN_ENCODING | X509_ASN_ENCODING ;
public static void Main()
{
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
static string lpszCertSubject = "searched name" ;
hSysStore = WinCapi.CertOpenSystemStore(IntPtr.Zero, MY);
Console.WriteLine("Store Handle:\t0x{0:X}", hSysStore.ToInt32());
public static void Main(){
IntPtr hSysStore = IntPtr.Zero;
IntPtr hCertCntxt = IntPtr.Zero;
if (hSysStore != IntPtr.Zero)
{
hCertCntxt = WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_SUBJECT_STR,
lpszCertSubject,
IntPtr.Zero);
hSysStore = WinCapi.CertOpenSystemStore(IntPtr.Zero, MY) ;
Console.WriteLine("Store Handle:\t0x{0:X}", hSysStore.ToInt32());
if (hCertCntxt != IntPtr.Zero)
{ //use certcontext from managed code
Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt32());
X509Certificate foundcert = new X509Certificate(hCertCntxt);
Console.WriteLine("\nFound certificate with SubjectName string \"{0}\"", lpszCertSubject);
Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
Console.WriteLine("HashString:\t{0}", foundcert.GetCertHashString());
}
else
Console.WriteLine("Could not find SubjectName containing string \"{0}\"", lpszCertSubject);
}
//------- Clean Up -----------
if (hCertCntxt != IntPtr.Zero)
WinCapi.CertFreeCertificateContext(hCertCntxt);
if (hSysStore != IntPtr.Zero)
WinCapi.CertCloseStore(hSysStore, 0);
}
}
Alternative Managed API:
TODO
if(hSysStore != IntPtr.Zero)
{
hCertCntxt=WinCapi.CertFindCertificateInStore(
hSysStore,
MY_ENCODING_TYPE,
0,
CERT_FIND_SUBJECT_STR,
lpszCertSubject ,
IntPtr.Zero) ;
if(hCertCntxt != IntPtr.Zero){ //use certcontext from managed code
Console.WriteLine("CertContext:\t0x{0:X}", hCertCntxt.ToInt32()) ;
X509Certificate foundcert = new X509Certificate(hCertCntxt);
Console.WriteLine("\nFound certificate with SubjectName string \"{0}\"",lpszCertSubject);
Console.WriteLine("SubjectName:\t{0}", foundcert.GetName());
Console.WriteLine("Serial No:\t{0}", foundcert.GetSerialNumberString());
Console.WriteLine("HashString:\t{0}" , foundcert.GetCertHashString());
}
else
Console.WriteLine("Could not find SubjectName containing string \"{0}\"", lpszCertSubject);
}
//------- Clean Up ---------
if(hCertCntxt != IntPtr.Zero)
WinCapi.CertFreeCertificateContext(hCertCntxt);
if(hSysStore != IntPtr.Zero)
WinCapi.CertCloseStore(hSysStore, 0) ;
}
}
Alternative Managed API:
TODO
Documentation
Opens a certificate store using a specified store provider type.
3/16/2007 7:39:35 AM - -134.134.136.1
Please edit this page!
Do you have...
helpful tips or sample code to share for using this API in managed code?
corrections to the existing content?
variations of the signature you want to share?
additional languages you want to include?
Select "Edit This Page" on the right hand toolbar and edit it! Or add new pages containing supporting types needed for this API (structures, delegates, and more).