C#想要调用Dephi接口方法,有两种解决办法
一、将Dephi程序编译成一个COM组件,然后在C#里引用COM组件,
二、非托管调用Dephi的DLL文件,代码如下:
Code
Dephi接口方法:
function RSADecrypt(aPKey, aDKey, aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer; stdcall;
function RSAEncrypt(aPKey, aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer; stdcall;
function AESDecrypt(aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer;
Dephi接口方法:
function RSADecrypt(aPKey, aDKey, aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer; stdcall;
function RSAEncrypt(aPKey, aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer; stdcall;
function AESDecrypt(aData : PChar; aBuffer : pointer; aBufferSize: DWORD): integer;
Code
//c#调用Dephi
public static class DrawChartFromDll
{
//调用非托管Dll,
[DllImport("rsaadpter.dll", EntryPoint = "RSAEncrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// RSA加密
public static extern int RSAEncrypt(string aPKey, string aData,IntPtr aBuffer, int aBufferSize);
[DllImport("rsaadpter.dll", EntryPoint = "RSADecrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// RSA解密
public static extern int RSADecrypt(string aPKey, string aDKey, string aData,IntPtr aBuffer, int aBufferSize);
[DllImport("rsaadpter.dll", EntryPoint = "AESDecrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// 普通AES解密
public static extern int AESDecrypt(string aData,IntPtr aBuffer, int aBufferSize);
}
//c#调用Dephi
public static class DrawChartFromDll
{
//调用非托管Dll,
[DllImport("rsaadpter.dll", EntryPoint = "RSAEncrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// RSA加密
public static extern int RSAEncrypt(string aPKey, string aData,IntPtr aBuffer, int aBufferSize);
[DllImport("rsaadpter.dll", EntryPoint = "RSADecrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// RSA解密
public static extern int RSADecrypt(string aPKey, string aDKey, string aData,IntPtr aBuffer, int aBufferSize);
[DllImport("rsaadpter.dll", EntryPoint = "AESDecrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
/// 普通AES解密
public static extern int AESDecrypt(string aData,IntPtr aBuffer, int aBufferSize);
}
Code
//利用ASP.NET指针,实现Dephi接口方法
string publicKey = dt.Rows[0]["RSAPKey"].ToString(); //本地公钥
string privateKey = dt.Rows[0]["RSADKey"].ToString(); //本地私钥
IntPtr ptr = Marshal.AllocHGlobal(2000); //非托管定义指针,指定长度2000
int resu = DrawChartFromDll.RSADecrypt(publicKey, privateKey, data, ptr, 2000); //Dephi接口方法,RSA解密私钥
data = ToStringFromIntPtr(ptr, resu); //获取RSA解密后字符串
/// <summary>
/// 从IntPtr指针,获取字符串
/// </summary>
/// <param name="ptr">指针</param>
/// <param name="result">获取字符串字节数</param>
/// <returns></returns>
private string ToStringFromIntPtr(IntPtr ptr,int result)
{
if (result <= 0)
{
Marshal.FreeHGlobal(ptr); //Marshal释放指针分配的内存
return "0";
}
byte[] b = new byte[result];
Marshal.Copy(ptr, b, 0, result); //
Marshal.FreeHGlobal(ptr); //Marshal释放指针分配的内存
return System.Text.Encoding.Default.GetString(b, 0, result); //获取解密后的私钥字符串
}
//利用ASP.NET指针,实现Dephi接口方法
string publicKey = dt.Rows[0]["RSAPKey"].ToString(); //本地公钥
string privateKey = dt.Rows[0]["RSADKey"].ToString(); //本地私钥
IntPtr ptr = Marshal.AllocHGlobal(2000); //非托管定义指针,指定长度2000
int resu = DrawChartFromDll.RSADecrypt(publicKey, privateKey, data, ptr, 2000); //Dephi接口方法,RSA解密私钥
data = ToStringFromIntPtr(ptr, resu); //获取RSA解密后字符串
/// <summary>
/// 从IntPtr指针,获取字符串
/// </summary>
/// <param name="ptr">指针</param>
/// <param name="result">获取字符串字节数</param>
/// <returns></returns>
private string ToStringFromIntPtr(IntPtr ptr,int result)
{
if (result <= 0)
{
Marshal.FreeHGlobal(ptr); //Marshal释放指针分配的内存
return "0";
}
byte[] b = new byte[result];
Marshal.Copy(ptr, b, 0, result); //
Marshal.FreeHGlobal(ptr); //Marshal释放指针分配的内存
return System.Text.Encoding.Default.GetString(b, 0, result); //获取解密后的私钥字符串
}
,这个送给你!