C# 非托管DLL调用

这是C++的代码
CRYPTOAPI_API short CALLBACK Encrypt(LPTSTR sOrigin,LPTSTR sEncrypt) 

  int    i; 
  char *sSource,*sConvert; 

  if (sOrigin != NULL) { 
    sSource = new char[strlen(sOrigin)+21]; 
    sConvert = new char[strlen(sOrigin)+21]; 
    strcpy(sSource,sOrigin); 

    Confuse(sSource,sConvert); 
    strcpy(sSource,sConvert); 
    Explode(sSource,sConvert); 
    for (i=0;i<10;i++) { 
      Shuffle(sConvert); 
    } 
    strcpy(sEncrypt,sConvert); 
    delete sConvert; 
    delete sSource; 
  } 
  else { 
    strcpy(sEncrypt,""); 
  } 
  return 1; 

现在我要在C#中调用CryptoAPI.dll中的Encrypt函数,首先我把dll拷贝到bin下。
C# 非托管DLL调用using System; 
C# 非托管DLL调用using System.Collections.Generic; 
C# 非托管DLL调用using System.Linq; 
C# 非托管DLL调用using System.Text; 
C# 非托管DLL调用using System.Runtime.InteropServices; 
C# 非托管DLL调用namespace ConsoleApplication1 
C# 非托管DLL调用
C# 非托管DLL调用        class Program 
C# 非托管DLL调用        { 
C# 非托管DLL调用                static void Main(string[] args) 
C# 非托管DLL调用                { 
C# 非托管DLL调用                        StringBuilder sb = new StringBuilder(); 
C# 非托管DLL调用                        StringBuilder sb1 = new StringBuilder(); 
C# 非托管DLL调用                        StringBuilder sb2 = new StringBuilder(); 
C# 非托管DLL调用                        sb.Append("000000"); 
C# 非托管DLL调用                        String ss = ""; 
C# 非托管DLL调用                        test.Encrypt(sb, sb1); 
C# 非托管DLL调用                        test.Decrypt(sb1, sb2); 
C# 非托管DLL调用                        Console.ReadLine(); 
C# 非托管DLL调用                } 
C# 非托管DLL调用        } 
C# 非托管DLL调用        class test 
C# 非托管DLL调用        { 
C# 非托管DLL调用                [DllImport("CryptoAPI.dll", EntryPoint = "Encrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 
C# 非托管DLL调用                public static extern short Encrypt(StringBuilder str, StringBuilder str1);//实现DLL加密 
C# 非托管DLL调用                [DllImport("CryptoAPI.dll", EntryPoint = "Decrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)] 
C# 非托管DLL调用                public static extern short Decrypt(StringBuilder str1, StringBuilder str2);//实现DLL解密 
C# 非托管DLL调用        } 
C# 非托管DLL调用
只要你的函数返回类型和参数跟C++的对应上即可。LPTSTR对应C#的StringBuilder。Char **对应C#的ref string,具体的用到了再去查。还有C++的编码一定要和C#的编一致.接下来我再介绍下非托管DLL的动态调用
C# 非托管DLL调用using System; 
C# 非托管DLL调用using System.Collections.Generic; 
C# 非托管DLL调用using System.Linq; 
C# 非托管DLL调用using System.Text; 
C# 非托管DLL调用using System.Runtime.InteropServices; 
C# 非托管DLL调用 
C# 非托管DLL调用namespace ConsoleApplication1 
C# 非托管DLL调用
C# 非托管DLL调用        public class DLLWrapper 
C# 非托管DLL调用        { 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///API LoadLibrary    
C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                [DllImport("Kernel32")] 
C# 非托管DLL调用                public static extern int LoadLibrary(String funcname); 
C# 非托管DLL调用 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///API GetProcAddress    
C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                [DllImport("Kernel32")] 
C# 非托管DLL调用                public static extern int GetProcAddress(int handle, String funcname); 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///API FreeLibrary    
C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                [DllImport("Kernel32")] 
C# 非托管DLL调用                public static extern int FreeLibrary(int handle); 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///通过非托管函数名转换为对应的委托
C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                ///<param name="dllModule">通过LoadLibrary获得的DLL句柄</param>    
C# 非托管DLL调用                ///<param name="functionName">非托管函数名</param>    
C# 非托管DLL调用                ///<param name="t">对应的委托类型</param>    
C# 非托管DLL调用                ///<returns>委托实例,可强制转换为适当的委托类型</returns>    
C# 非托管DLL调用                public static Delegate GetFunctionAddress(int dllModule, string functionName, Type t) 
C# 非托管DLL调用                { 
C# 非托管DLL调用                        int address = GetProcAddress(dllModule, functionName); 
C# 非托管DLL调用                        if (address == 0) 
C# 非托管DLL调用                                return null
C# 非托管DLL调用                        else 
C# 非托管DLL调用                                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t); 
C# 非托管DLL调用                } 
C# 非托管DLL调用 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///将表示函数地址的IntPtr实例转换成对应的委托C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t) 
C# 非托管DLL调用                { 
C# 非托管DLL调用                        if (address == IntPtr.Zero) 
C# 非托管DLL调用                                return null
C# 非托管DLL调用                        else 
C# 非托管DLL调用                                return Marshal.GetDelegateForFunctionPointer(address, t); 
C# 非托管DLL调用                } 
C# 非托管DLL调用 
C# 非托管DLL调用                ///<summary>    
C# 非托管DLL调用                ///将表示函数地址的int转换成对应的委托
C# 非托管DLL调用                ///</summary>    
C# 非托管DLL调用                public static Delegate GetDelegateFromIntPtr(int address, Type t) 
C# 非托管DLL调用                { 
C# 非托管DLL调用                        if (address == 0) 
C# 非托管DLL调用                                return null
C# 非托管DLL调用                        else 
C# 非托管DLL调用                                return Marshal.GetDelegateForFunctionPointer(new IntPtr(address), t); 
C# 非托管DLL调用                } 
C# 非托管DLL调用 
C# 非托管DLL调用                delegate short Encrypt(StringBuilder decrypt, StringBuilder encrypt); 
C# 非托管DLL调用 
C# 非托管DLL调用                public string ReadDLL() 
C# 非托管DLL调用                { 
C# 非托管DLL调用                        int hModule = DLLWrapper.LoadLibrary("CryptoAPI.dll"); 
C# 非托管DLL调用                        if (hModule == 0) 
C# 非托管DLL调用                                return "0"
C# 非托管DLL调用                        Encrypt encrypt = (Encrypt)DLLWrapper.GetFunctionAddress(hModule, "Encrypt"typeof(Encrypt)); 
C# 非托管DLL调用                        if (encrypt == null
C# 非托管DLL调用                        { 
C# 非托管DLL调用                                DLLWrapper.FreeLibrary(hModule); 
C# 非托管DLL调用                                return "0"
C# 非托管DLL调用                        } 
C# 非托管DLL调用                        StringBuilder ss = new StringBuilder(100); 
C# 非托管DLL调用                        StringBuilder sb = new StringBuilder(100); 
C# 非托管DLL调用                        sb.Append("aaa"); 
C# 非托管DLL调用                        encrypt(sb, ss); 
C# 非托管DLL调用                        DLLWrapper.FreeLibrary(hModule); 
C# 非托管DLL调用                        return ss.ToString(); 
C# 非托管DLL调用                } 
C# 非托管DLL调用        } 
C# 非托管DLL调用
上一篇:SpringBoot集成Swagger2中不同环境开关配置


下一篇:为ASP.NET封装的SQL数据库访问类