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下。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace ConsoleApplication1
{
         class Program
        {
                 static void Main( string[] args)
                {
                        StringBuilder sb = new StringBuilder();
                        StringBuilder sb1 = new StringBuilder();
                        StringBuilder sb2 = new StringBuilder();
                        sb.Append( "000000");
                        String ss = "";
                        test.Encrypt(sb, sb1);
                        test.Decrypt(sb1, sb2);
                        Console.ReadLine();
                }
        }
         class test
        {
                [DllImport( "CryptoAPI.dll", EntryPoint = "Encrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
                 public static extern short Encrypt(StringBuilder str, StringBuilder str1); //实现DLL加密
                [DllImport( "CryptoAPI.dll", EntryPoint = "Decrypt", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
                 public static extern short Decrypt(StringBuilder str1, StringBuilder str2); //实现DLL解密
        }
}
只要你的函数返回类型和参数跟C++的对应上即可。LPTSTR对应C#的StringBuilder。Char **对应C#的ref string,具体的用到了再去查。还有C++的编码一定要和C#的编一致.接下来我再介绍下非托管DLL的动态调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace ConsoleApplication1
{
         public class DLLWrapper
        {
                 ///<summary>    
                 ///API LoadLibrary    
                 ///</summary>    
                [DllImport( "Kernel32")]
                 public static extern int LoadLibrary(String funcname);

                 ///<summary>    
                 ///API GetProcAddress    
                 ///</summary>    
                [DllImport( "Kernel32")]
                 public static extern int GetProcAddress( int handle, String funcname);
                 ///<summary>    
                 ///API FreeLibrary    
                 ///</summary>    
                [DllImport( "Kernel32")]
                 public static extern int FreeLibrary( int handle);
                 ///<summary>    
                 ///通过非托管函数名转换为对应的委托
                 ///</summary>    
                 ///<param name="dllModule">通过LoadLibrary获得的DLL句柄</param>    
                 ///<param name="functionName">非托管函数名</param>    
                 ///<param name="t">对应的委托类型</param>    
                 ///<returns>委托实例,可强制转换为适当的委托类型</returns>    
                 public static Delegate GetFunctionAddress( int dllModule, string functionName, Type t)
                {
                         int address = GetProcAddress(dllModule, functionName);
                         if (address == 0)
                                 return null;
                         else
                                 return Marshal.GetDelegateForFunctionPointer( new IntPtr(address), t);
                }

                 ///<summary>    
                 ///将表示函数地址的IntPtr实例转换成对应的委托                  ///</summary>    
                 public static Delegate GetDelegateFromIntPtr(IntPtr address, Type t)
                {
                         if (address == IntPtr.Zero)
                                 return null;
                         else
                                 return Marshal.GetDelegateForFunctionPointer(address, t);
                }

                 ///<summary>    
                 ///将表示函数地址的int转换成对应的委托
                 ///</summary>    
                 public static Delegate GetDelegateFromIntPtr( int address, Type t)
                {
                         if (address == 0)
                                 return null;
                         else
                                 return Marshal.GetDelegateForFunctionPointer( new IntPtr(address), t);
                }

                 delegate short Encrypt(StringBuilder decrypt, StringBuilder encrypt);

                 public string ReadDLL()
                {
                         int hModule = DLLWrapper.LoadLibrary( "CryptoAPI.dll");
                         if (hModule == 0)
                                 return "0";
                        Encrypt encrypt = (Encrypt)DLLWrapper.GetFunctionAddress(hModule, "Encrypt", typeof(Encrypt));
                         if (encrypt == null)
                        {
                                DLLWrapper.FreeLibrary(hModule);
                                 return "0";
                        }
                        StringBuilder ss = new StringBuilder(100);
                        StringBuilder sb = new StringBuilder(100);
                        sb.Append( "aaa");
                        encrypt(sb, ss);
                        DLLWrapper.FreeLibrary(hModule);
                         return ss.ToString();
                }
        }
}

你可能感兴趣的:(C#,职场,dll,休闲)