Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Not Recognizing SafeTokenHandle

I have a compilation error with C#

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);

In the word at the * sign (LogonUser and SafeTokenHandle). My C# compiler can not compile because of unknown type. I develop with visual studio 2012, windows 64, framework 4.0.

Please Help.

like image 919
Siwakorn.Apichit Avatar asked Dec 26 '22 00:12

Siwakorn.Apichit


1 Answers

SafeTokenHandle is not part of the .Net framework. I'm assuming your code is somewhat related to this article, So you're missing the definition:

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);
    }
}
like image 55
argaz Avatar answered Dec 29 '22 11:12

argaz