Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / December 2004

Tip: Looking for answers? Try searching our database.

Unable to successfully return data from dllimport function (C#)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
seths - 31 Dec 2004 15:31 GMT
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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.