2011-08-01 88 views
4

通過一些傳統的模擬邏輯去的時候,我發現了以下異常:無法找到DLL「ADVAPI32.DLL」假冒例外名爲「LogonUser的」切入點

無法找到名爲「切入點的LogonUser 'in DLL'advapi32.dll'

我知道這個錯誤意味着我的應用程序無法在advapi32.dll中找到LogonUser方法。

的代碼看起來是這樣的:

[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true, ExactSpelling = true)] 
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String  lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 



if(LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref _tokenHandle)) 
{ 
//do stuff... 
} 

任何人都有類似的錯誤 - 如何解決它或它爲什麼發生什麼建議嗎?除了使用advapi32.dll(它的.net 3.5解決方案,但有很多遺留類),還有更好的方法嗎?

回答

5

也許有事情做與「ExactSpelling =真正的」

這似乎工作:

public enum LogonType : int 
{ 
    Interactive = 2, 
    Network = 3, 
    Batch = 4, 
    Service = 5, 
    Unlock = 7, 
    NetworkCleartText = 8, 
    NewCredentials = 9, 
} 

public enum LogonProvider : int 
{ 
    Default = 0, 
} 

public class Impersonation : IDisposable 
{ 
    #region Dll Imports 

    [DllImport("kernel32.dll")] 
    private static extern Boolean CloseHandle(IntPtr hObject); 

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool LogonUser(string username, string domain, 
              string password, LogonType logonType, 
              LogonProvider logonProvider, 
              out IntPtr userToken); 

    [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool DuplicateToken(IntPtr token, int impersonationLevel, 
     ref IntPtr duplication); 

      [DllImport("advapi32.dll", SetLastError = true)] 
    static extern bool ImpersonateLoggedOnUser(IntPtr userToken); 
    #endregion 

    #region Private members 

    private bool _disposed; 

    private WindowsImpersonationContext _impersonationContext; 

    #endregion 

    #region Constructors 

    public Impersonation(String username, String domain, String password) 
    { 
     IntPtr userToken = IntPtr.Zero; 
     IntPtr userTokenDuplication = IntPtr.Zero; 

     // Logon with user and get token. 
     bool loggedOn = LogonUser(username, domain, password, 
      LogonType.Interactive, LogonProvider.Default, 
      out userToken); 

     if (loggedOn) 
     { 
      try 
      { 
       // Create a duplication of the usertoken, this is a solution 
       // for the known bug that is published under KB article Q319615. 
       if (DuplicateToken(userToken, 2, ref userTokenDuplication)) 
       { 
        // Create windows identity from the token and impersonate the user. 
        WindowsIdentity identity = new WindowsIdentity(userTokenDuplication); 
        _impersonationContext = identity.Impersonate(); 
       } 
       else 
       { 
        // Token duplication failed! 
        // Use the default ctor overload 
        // that will use Mashal.GetLastWin32Error(); 
        // to create the exceptions details. 
        throw new Exception("Could not copy token"); 
       } 
      } 
      finally 
      { 
       // Close usertoken handle duplication when created. 
       if (!userTokenDuplication.Equals(IntPtr.Zero)) 
       { 
        // Closes the handle of the user. 
        CloseHandle(userTokenDuplication); 
        userTokenDuplication = IntPtr.Zero; 
       } 

       // Close usertoken handle when created. 
       if (!userToken.Equals(IntPtr.Zero)) 
       { 
        // Closes the handle of the user. 
        CloseHandle(userToken); 
        userToken = IntPtr.Zero; 
       } 
      } 
     } 
     else 
     {    
      throw new Exception("Login failed"); 
     } 
    } 

    ~Impersonation() 
    { 
     Dispose(false); 
    } 
    #endregion 

    #region Public methods 

    public void Revert() 
    { 
     if (_impersonationContext != null) 
     { 
      // Revert to previous user. 
      _impersonationContext.Undo(); 
      _impersonationContext = null; 
     } 
    } 
    #endregion 

    #region IDisposable implementation. 

    public void Dispose() 
    { 
     Dispose(true); 
     GC.SuppressFinalize(this); 
    } 

    protected virtual void Dispose(bool disposing) 
    { 
     if (!_disposed) 
     { 
      Revert(); 

      _disposed = true; 
     } 
    } 
    #endregion 
} 
+0

看起來可能是原因,因爲這是我的榜樣和之間的唯一區別一個在pinvoke.net - 虐待測試,只要我有機會 – woggles

1

您是否嘗試過使用pinvoke.net上提供的LogonUser簽名版本?

相關問題