asp.net调用vc dll

本文展示了如何在ASP.NET中通过DllImport调用来加载和使用两个DLL(Decrypt.dll和DES3.DLL)。代码创建了一个DllInvoke类,用于加载DLL并执行指定函数,然后在DecryptCommon类中定义委托并调用解密函数。提供了两种解密方法:一种返回解密后的字符串,另一种返回解密结果的布尔值。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 怎么办呢,注意红色部分

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值