在C#中调用非托管函数的时,有很多函数需要使用在C#中定义对象的地址;因此要使用到GCHandle类。

使用片断:

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);

这样C#就可以取到数据了(并不是所有的非托管函数都适用)

相关文章:

  • 2021-12-18
  • 2021-08-08
  • 2021-08-02
  • 2021-07-31
猜你喜欢
  • 2021-05-17
  • 2021-10-18
  • 2021-07-29
  • 2022-12-23
  • 2021-12-31
  • 2021-07-12
  • 2021-09-10
相关资源
相似解决方案