在IIS6下使用ASP.Net[DllImport(" 调用非托管加密狗的dll,然后IIS6停止响应,
程序在IIS5下无错,执行正常,在Windows2003+VS2005下调试无错,执行正常,但是在IIS6下就出错了,症状为IIS无响应排除加密狗DLL内部接口问题,调用代码问题,并设置IIS6服务为IIS5模式,启用了父路径,启用IIS和桌面交互的服务IIS6示例调用代码:
public class DllInvoke
{
//引用系统方法来取得dll路径
[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);
[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType); private IntPtr hLib;
public DllInvoke(String DLLPath)
{
this.hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(this.hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke(String APIName, Type t)
{
IntPtr api = GetProcAddress(this.hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
--------------------
调用
DllInvoke dll = new DllInvoke(Server.MapPath(@"~/Bin/XXX.dll"));
按地址载入调用dll
XXX_GetNetState GetNetState = (XXX_GetNetState)dll.Invoke("XXX_GetNetState", typeof(XXX_GetNetState));
转换为委托方法
int status = GetNetState();
下面是传递参数例子:
程序在IIS5下无错,执行正常,在Windows2003+VS2005下调试无错,执行正常,但是在IIS6下就出错了,症状为IIS无响应排除加密狗DLL内部接口问题,调用代码问题,并设置IIS6服务为IIS5模式,启用了父路径,启用IIS和桌面交互的服务IIS6示例调用代码:
public class DllInvoke
{
//引用系统方法来取得dll路径
[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);
[DllImport("user32.dll", EntryPoint = "MessageBox")]
public static extern int MessageBox(int hWnd, String strMessage, String strCaption, uint uiType); private IntPtr hLib;
public DllInvoke(String DLLPath)
{
this.hLib = LoadLibrary(DLLPath);
}
~DllInvoke()
{
FreeLibrary(this.hLib);
}
//将要执行的函数转换为委托
public Delegate Invoke(String APIName, Type t)
{
IntPtr api = GetProcAddress(this.hLib, APIName);
return (Delegate)Marshal.GetDelegateForFunctionPointer(api, t);
}
}
--------------------
调用
DllInvoke dll = new DllInvoke(Server.MapPath(@"~/Bin/XXX.dll"));
按地址载入调用dll
XXX_GetNetState GetNetState = (XXX_GetNetState)dll.Invoke("XXX_GetNetState", typeof(XXX_GetNetState));
转换为委托方法
int status = GetNetState();
下面是传递参数例子:
再不行就调用COM互操作.
IIS本身的问题,或者是安装的问题
public class IdentityAnalogue
{
//模拟指定用户时使用的常量定义
public const int LOGON32_LOGON_INTERACTIVE = 2;
/**/
///
///
///
public const int LOGON32_PROVIDER_DEFAULT = 0;
/**/
///
///
///
WindowsImpersonationContext impersonationContext;
//win32api引用
/**/
/// mmary>
///
///
///
///
///
///
///
///
///
[DllImport("advapi32.dll")]
public static extern int LogonUserA(string lpszUserName,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);
/**/
///
///
///
///
///
///
///
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern int DuplicateToken(IntPtr hToken,
int impersonationLevel,
ref IntPtr hNewToken);
/**/
///
///
///
///
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern bool RevertToSelf();
/**/
///
///
///
///
///
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
/**/
///
///
///
public IdentityAnalogue()
{
}
//模拟指定的用户身份 /// <summary>
/// 身份模拟
/// </summary>
/// <param name="userName">用户名</param>
/// <param name="domain">域</param>
/// <param name="password">密码</param>
/// <returns></returns>
public bool ImpersonateValidUser(string userName, string domain, string password)
{
WindowsIdentity tempWindowsIdentity;
IntPtr token = IntPtr.Zero;
IntPtr tokenDuplicate = IntPtr.Zero;
if (RevertToSelf())
{
if (LogonUserA(userName, domain, password, 2, 0, ref token) != 0)
{
if (DuplicateToken(token, 2, ref tokenDuplicate) != 0)
{
tempWindowsIdentity = new WindowsIdentity(tokenDuplicate);
impersonationContext = tempWindowsIdentity.Impersonate();
if (impersonationContext != null)
{
CloseHandle(token);
CloseHandle(tokenDuplicate);
return true;
}
}
}
}
if (token != IntPtr.Zero)
CloseHandle(token);
if (tokenDuplicate != IntPtr.Zero)
CloseHandle(tokenDuplicate);
return false;
} /// <summary>
/// 取消模拟
/// </summary>
public void UndoImpersonation()
{
impersonationContext.Undo();
}
}