您的位置:首页 > 编程语言 > ASP

asp.net调用vc dll

2008-09-24 09:35 288 查看
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 怎么办呢,注意红色部分
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: