using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
namespace ConsoleApp3
{
class Program
{
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern bool OpenBDK01();
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern bool CloseBDK01();
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern int GetMyPinCount();
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern IntPtr GetDevSN();
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern char LoginForBDK01(Byte[] lpPassword, int iPWLen);
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern char EncryptForSM4(byte[] lpEData, byte[] lpDData, int iDLen);
[DllImport(@"C:\Users\cj\source\repos\ConsoleApp3\ConsoleApp3\BDK01ForCryptolDll.dll")]
public static extern char DecyptForSM4(byte[] lpDData, byte[] lpEData, int iDLen);
static void Main(string[] args)
{
bool Device_status = OpenBDK01();
string devsn = Marshal.PtrToStringAnsi(GetDevSN());
while (true)
{
Console.WriteLine("the device SN is {0}", devsn);
Console.WriteLine("the device status is {0}", Device_status);
}
}
}
}
一辕录、IntPtr 與 string互轉
string str = "aa";
IntPtr init = Marshal.StringToHGlobalAnsi(str);
string ss= Marshal.PtrToStringAnsi(init);
//最后釋放掉
Marshal.FreeHGlobal(init);
二植袍、char*與string互轉
string a = "11";
char* aChar = (char*)System.Runtime.InteropServices.Marshal.StringToHGlobalAnsi(a).ToPointer();
string result = Marshal.PtrToStringAnsi((IntPtr)aChar);
三畦攘、char* 與 IntPtr互轉
可以直接強制類型轉換
IntPtr init = (IntPtr)aChar;
char* aChar = (char*)init;