p/invoke of crypt32.dll - I am having a strange problem

  • Thread starter Thread starter Tom
  • Start date Start date
T

Tom

I am developing on a custom CE 5.0 board, and I am trying to open the
system certificate store using CertOpenSystemStore() from the
Crypt32.dll library. The problem is that I am getting a "Can't find
an Entry Point 'CertOpenSystemStore' in a PInvoke DLL 'crypt32.dll'."
The strange thing is that I am using other cert functions from
Crypt32.dll such as PFXImportCertStore() and PFXIsPFXBlob(). I know
that I needed to add the PKCS #12 component to the platform in order
to use the PFX functionality. Do I need to add something else to get
the rest of the functionality of Crypt32.dll?

Here are some snippets of the code:

[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
[MarshalAs(UnmanagedType.LPWStr)] String szPassword);


//Open the JCID stores MY , CA and Root
IntPtr hMyStore = IntPtr.Zero;
IntPtr hCAStore = IntPtr.Zero;
IntPtr hRootStore = IntPtr.Zero;

try
{
hMyStore = CertOpenSystemStore(IntPtr.Zero, "MY");
hCAStore = CertOpenSystemStore(IntPtr.Zero, "CA");
hRootStore = CertOpenSystemStore(IntPtr.Zero,
"ROOT");
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}



It will catch here...


Anyone know the answer?

Thanks,
Tom
 
It's because it's not an actual exported API but at platform-defined macro
(SetEvent and PulseEvent are similar in this regard). If you look in the
SDK at wincrypt.h you'll see this at line 13440:

#define CertOpenSystemStoreW(hProv,szSubsystemProtocol) \
CertOpenStore( \
CERT_STORE_PROV_SYSTEM_W, \
0, \
(hProv), \
CERT_STORE_NO_CRYPT_RELEASE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER,
\
(const void *) (szSubsystemProtocol) \
)

So you need to actually P/Invoke CertOpenStore with the same parameters.
 
Thanks Chris...

Once again you have saved my bacon!

Tom



It's because it's not an actual exported API but at platform-defined macro
(SetEvent and PulseEvent are similar in this regard). If you look in the
SDK at wincrypt.h you'll see this at line 13440:

#define CertOpenSystemStoreW(hProv,szSubsystemProtocol) \
CertOpenStore( \
CERT_STORE_PROV_SYSTEM_W, \
0, \
(hProv), \
CERT_STORE_NO_CRYPT_RELEASE_FLAG|CERT_SYSTEM_STORE_CURRENT_USER,
\
(const void *) (szSubsystemProtocol) \
)

So you need to actually P/Invoke CertOpenStore with the same parameters.

--
Chris Tacke - Embedded MVP
OpenNETCF Consulting
Managed Code in the Embedded Worldwww.opennetcf.com
--


I am developing on a custom CE 5.0 board, and I am trying to open the
system certificate store using CertOpenSystemStore() from the
Crypt32.dll library. The problem is that I am getting a "Can't find
an Entry Point 'CertOpenSystemStore' in a PInvoke DLL 'crypt32.dll'."
The strange thing is that I am using other cert functions from
Crypt32.dll such as PFXImportCertStore() and PFXIsPFXBlob(). I know
that I needed to add the PKCS #12 component to the platform in order
to use the PFX functionality. Do I need to add something else to get
the rest of the functionality of Crypt32.dll?
Here are some snippets of the code:
[DllImport("crypt32.dll", SetLastError = true)]
public static extern IntPtr CertOpenSystemStore(
IntPtr hCryptProv,
[MarshalAs(UnmanagedType.LPWStr)] String szPassword);
//Open the JCID stores MY , CA and Root
IntPtr hMyStore = IntPtr.Zero;
IntPtr hCAStore = IntPtr.Zero;
IntPtr hRootStore = IntPtr.Zero;
try
{
hMyStore = CertOpenSystemStore(IntPtr.Zero, "MY");
hCAStore = CertOpenSystemStore(IntPtr.Zero, "CA");
hRootStore = CertOpenSystemStore(IntPtr.Zero,
"ROOT");
}
catch (Exception e)
{
System.Diagnostics.Debug.WriteLine(e.ToString());
}
It will catch here...
Anyone know the answer?
Thanks,
Tom
 
"Can't resolve it" isn't useful. I answered why it didn't work and the OP
took that info and defined the call and it worked. What part isn't working
for you?
 
Hi,

I am doing some WinCE 5 or 6 programming about CrypoAPI. My problem is
when I call PFXIsPFXBlob() with some input, it returns a false result.
The same input gets a true result on desktop Windows.

I just noticed that Tom said "I needed to add the PKCS #12 component to
the platform in order to use the PFX functionality". Can you tell me the
details to do that?

Many thanks. This problem has puzzled me for quite some time.

Harry
 
Back
Top