asp.net調用vc dll

public class DllInvoke
{
    [DllImport("kernel32.dll")]
    private extern static IntPtr LoadLibrary(String path);

    [DllImport("kernel32.dll")]
    private extern static IntPtr GetProcAddress(IntPtr lib,String funcName);

    [DllImport("kernel32.dll")]
    private extern static bool FreeLibrary(IntPtr lib);

    private IntPtr hLib;

    public DllInvoke(String DLLPath1,String DLLPath2)
    {
        LoadLibrary(DLLPath2);
        hLib = LoadLibrary(DLLPath1);
    }

    ~DllInvoke()
    {
        FreeLibrary(hLib);
    }

    //將要執行的函數轉換爲委託
    public Delegate Invoke(String APIName,Type t)
    {
        IntPtr api = GetProcAddress(hLib,APIName);
        return (Delegate)Marshal.GetDelegateForFunctionPointer(api,t);
    }
}

 

//使用

public class DecryptCommon
{
    public delegate bool Decrypt(StringBuilder encryptKey, StringBuilder outKey);

    public DecryptCommon()
    {
        //
        // TODO: 在此處添加構造函數邏輯
        //
    }

    public static bool getDecrypt(StringBuilder encryptKey, out StringBuilder outKey)
    {
        DllInvoke dll = new DllInvoke(System.Web.HttpContext.Current.Server.MapPath(@"~/Bin/Decrypt.dll"), System.Web.HttpContext.Current.Server.MapPath(@"~/Bin/DES3.DLL"));
        Decrypt decrypt = (Decrypt)dll.Invoke("Decrypt", typeof(Decrypt));
        outKey =new StringBuilder(20);
        return decrypt(encryptKey, outKey);
    }

    //解密
    public static string getMiMa(string encryptKey)
    {
        StringBuilder sbEncryptKey = new StringBuilder();
        sbEncryptKey.Append(encryptKey);

        StringBuilder value = null;

        getDecrypt(sbEncryptKey, out value);
        return value.ToString();
    }

    //解密
    public static bool getMiMaBool(string encryptKey)
    {
        StringBuilder sbEncryptKey = new StringBuilder();
        sbEncryptKey.Append(encryptKey);

        StringBuilder value = null;


        return getDecrypt(sbEncryptKey, out value);
    }
}

 

 

從網上找到了上面的代碼,要是一個dll調用另外一個dll 怎麼辦呢,注意紅色部分

 

發佈了124 篇原創文章 · 獲贊 7 · 訪問量 25萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章