I am having difficulty figuring out which datatypes to use for return values
in a particular DLL import function (C#).
I am trying to use the CryptGetKeyParam function from the ADVAPI32.dll API
call to return certificate information. I am able to successfully call
CryptAquireContext, and CryptGetUserKey to setup this call.
The CryptGetKeyParam function returns data in different forms depending on
which flags are used on input. When you call CryptGetKeyParam with the
KP_CERTIFICATE flag, the API should return a buffer containing the
DER-encoded x.509 cert (this is what I am unable to do).
I am able to successfully call CryptGetKeyParam with the KP_KEYLEN flag to
return the length key length (I return this as a uint).
Any help is appreciated :)
This is what I currently have (when I run this code the app crashes with a
memory error):
//import api functions
[DllImport("Advapi32.dll", CharSet=CharSet.Auto,SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
internal extern static bool CryptAcquireContext(
ref IntPtr phProv,
string pszContainer,
string pszProvider,
uint dwProvType,
uint dwFlags);
[DllImport("ADVAPI32.DLL", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern Boolean CryptGetUserKey(
IntPtr hProv,
uint dwKeySpec,
ref IntPtr hKey);
[DllImport("ADVAPI32.DLL", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern Boolean CryptGetKeyParam(
IntPtr hKey,
uint dwParam,
byte[] pbData,
ref uint pdwDataLen,
uint dwFlags);
// as defined in MSDN
// BOOL CRYPTFUNC CryptGetKeyParam(
// HCRYPTKEY hKey,
// DWORD dwParam,
// BYTE* pbData,
// DWORD* pdwDataLen,
// DWORD dwFlags
// );
internal static int GetError()
{
return Marshal.GetLastWin32Error();
}
public const uint AT_KEYEXCHANGE = 1;
public const uint PROV_RSA_FULL = 1;
public const uint KP_CERTIFICATE = 26;
public const int CRYPT_SILENT = 64;
//function
public string GetCert(string container, string provider)
{
IntPtr hProv = IntPtr.Zero;
IntPtr hKey = IntPtr.Zero;
uint cbCert = uint.MaxValue;
if(!CryptAcquireContext(ref hProv, container, provider, PROV_RSA_FULL,
CRYPT_SILENT))
{
//unable to establish context to CSP
return "Error establishing context to CSP. Error= " + GetError();
}
if(!CryptGetUserKey(hProv, AT_KEYEXCHANGE, ref hKey))
{
//unable to get user key
return "Error getting user key. Error= " + GetError();
}
if(!CryptGetKeyParam(hKey, KP_CERTIFICATE, pbCert, ref cbCert, 0))
{
//call failed
return "Error getting key length. Error= " + GetError();
}
return "success";
}
Jeff Gaines - 31 Dec 2004 16:46 GMT
> I am having difficulty figuring out which datatypes to use for return
> values in a particular DLL import function (C#).
[quoted text clipped - 14 lines]
>
> Any help is appreciated :)
I would try:
[DllImport("ADVAPI32.DLL", CharSet=CharSet.Auto, SetLastError=true)]
internal static extern Boolean CryptGetKeyParam(
IntPtr hKey,
uint dwParam,
ref byte[] pbData, ********
ref uint pdwDataLen,
uint dwFlags);
e.g. effectively pass a pointer to the call.
You must initiate pbData before you use it so it is large enough for
the returned data.

Signature
Jeff Gaines
Posted with XanaNews 1.17.1.2 http://www.wilsonc.demon.co.uk/delphi.htm