2013-07-14 87 views
4

我有一個編譯錯誤使用C#C#不承認SafeTokenHandle

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Windows.Forms; 
using System.Security.Principal; 
using System.Security.Permissions; 
using System.Runtime.ConstrainedExecution; 
using System.Runtime.InteropServices; 
using Microsoft.Win32.SafeHandles; 

[DllImport("advapi32.dll", SetLastError = true,CharSet = CharSet.Unicode)] 
public static extern bool ***LogonUser***(string lpszUsername, string lpszDomain, string lpszPassword, 
int dwLogonType, int dwLogonProvider, out ***SafeTokenHandle*** phToken); 

在以*號(LogonUser的和SafeTokenHandle)字。由於未知類型,我的C#編譯器無法編譯。我用visual studio 2012,windows 64,framework 4.0開發。

請幫忙。

+0

謝謝你編輯我的措辭。我的英語不好。 –

回答

0

這是因爲這些結構沒有在您的項目中定義。

從我知道你NEET的方法是:

[DllImport("advapi32.dll", SetLastError=true)] 
public static extern bool LogonUser(
    string lpszUsername, 
    string lpszDomain, 
    string lpszPassword, 
    int dwLogonType, 
    int dwLogonProvider, 
    out IntPtr phToken 
); 

here是在功能上

+0

可以使用此解決方案和您的解釋。 –

+0

@ user1045254謝謝,如果它對你有幫助,請批准我的答案 –

8

SafeTokenHandle的解釋是不是.NET Framework的一部分。我假設你的代碼有點相關this article,所以你錯過了定義:

public sealed class SafeTokenHandle : SafeHandleZeroOrMinusOneIsInvalid 
{ 
    private SafeTokenHandle() 
     : base(true) 
    { 
    } 

    [DllImport("kernel32.dll")] 
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.Success)] 
    [SuppressUnmanagedCodeSecurity] 
    [return: MarshalAs(UnmanagedType.Bool)] 
    private static extern bool CloseHandle(IntPtr handle); 

    protected override bool ReleaseHandle() 
    { 
     return CloseHandle(handle); 
    } 
} 
+0

謝謝你的幫助。但Visual Studio未知[SupperssUnmanagedCodeSecurity]。 –

+0

你需要添加'使用System.Security;'來擁有。一般你可以做ctrl +。解決這些問題(或使用右鍵單擊菜單),甚至可以在Google中搜索類型名稱以找到它的名稱空間和程序集。 – argaz

+0

我知道這是舊的文章,但什麼是真正發生在SafeTokenHandle類以上? –