When calling an unmanaged function in C #, many functions need to use the address of the object defined in C #. Therefore, the GCHandle class is used.
Usage:
DWORD ReadCert (
UNT_DEV_HANDLE hDev,
BYTE * pbCert,
DWORD * pdwCertLen,
DWORD dwFlag );
/// Return Type: DWORD-> unsigned int
/// HDev: UNT_DEV_HANDLE-> void *
/// PbCert: BYTE *
/// PdwCertLen: DWORD *
/// DwFlag: DWORD-> unsigned int
[System. Runtime. InteropServices. DllImportAttribute ("GAKeyPub", EntryPoint = "ReadCert")]
Public static extern int ReadCert (System. IntPtr hDev, [Out] IntPtr pbCert, ref int pdwCertLen, int dwFlag );
Byte [] certBytes = new byte [certLen];
GCHandle hcertBytes = GCHandle. Alloc (certBytes, GCHandleType. Pinned );
IntPtr pcert = hcertBytes. AddrOfPinnedObject ();
Result = GAKeyPubInvoke. ReadCert (devHandle, pcert, ref certLen, 1 );
In this way, C # can obtain data (not all unmanaged functions are applicable)