Click here to Skip to main content
15,886,110 members
Articles / Web Development / IIS

SharpBITS.NET - A Wrapper for the BITS API

Rate me:
Please Sign up or sign in to vote.
4.83/5 (22 votes)
7 Oct 2010CPOL4 min read 184.3K   4.3K   137  
A BITS wrapper library for .NET 2.0.
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace SharpBits.Base
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1008:EnumsShouldHaveZeroValue"), Flags()]
    public enum RpcAuthnLevel
    {
        Default = 0,
        None = 1,
        Connect = 2,
        Call = 3,
        Pkt = 4,
        PktIntegrity = 5,
        PktPrivacy = 6
    }

    public enum RpcImpLevel
    {
        Default = 0,
        Anonymous = 1,
        Identify = 2,
        Impersonate = 3,
        Delegate = 4
    }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase"), System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1027:MarkEnumsWithFlags")]
    public enum EoAuthnCap
    {
        None = 0x00,
        MutualAuth = 0x01,
        StaticCloaking = 0x20,
        DynamicCloaking = 0x40,
        AnyAuthority = 0x80,
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "Member")]
        MakeFullSIC = 0x100,
        Default = 0x800,
        SecureRefs = 0x02,
        AccessControl = 0x04,
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", MessageId = "Member")]
        AppID = 0x08,
        Dynamic = 0x10,
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "Member")]
        RequireFullSIC = 0x200,
        AutoImpersonate = 0x400,
        NoCustomMarshal = 0x2000,
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1705:LongAcronymsShouldBePascalCased", MessageId = "Member")]
        DisableAAA = 0x1000
    }

    static class UnsafeNativeMethods
    {
        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAsAttribute(UnmanagedType.Bool)]
        internal static extern bool ConvertStringSidToSidW(string stringSID, ref IntPtr SID);

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        [return: MarshalAsAttribute(UnmanagedType.Bool)]
        internal static extern bool LookupAccountSidW(string systemName, IntPtr SID,
            StringBuilder name, ref long cbName, StringBuilder domainName, ref long cbDomainName, ref int psUse);

        [System.Runtime.InteropServices.DllImport("ole32.dll")]
        public static extern int CoInitializeSecurity(IntPtr pVoid, int cAuthSvc, IntPtr asAuthSvc, IntPtr pReserved1, RpcAuthnLevel level,
            RpcImpLevel impers, IntPtr pAuthList, EoAuthnCap dwCapabilities, IntPtr pReserved3);
    }

    internal static class Utils
    {
        internal static string GetName(string SID)
        {
            const int size = 255;
            long cbUserName = size;
            long cbDomainName = size;
            IntPtr ptrSID = new IntPtr(0);
            int psUse = 0;
            StringBuilder userName = new StringBuilder(size);
            StringBuilder domainName = new StringBuilder(size);
            if (UnsafeNativeMethods.ConvertStringSidToSidW(SID, ref ptrSID))
            {
                if (UnsafeNativeMethods.LookupAccountSidW(string.Empty, ptrSID, userName, ref cbUserName, domainName, ref cbDomainName, ref psUse))
                {
                    return string.Concat(domainName.ToString(), "\\", userName.ToString());
                }
            }
            return string.Empty;
        }

        internal static FILETIME DateTime2FileTime(DateTime dateTime)
        {
            long fileTime = 0;
            if (dateTime != DateTime.MinValue)      //Checking for MinValue
                fileTime = dateTime.ToFileTimeUtc();    
            FILETIME resultingFileTime = new FILETIME();
            resultingFileTime.dwLowDateTime = (uint)(fileTime & 0xFFFFFFFF);
            resultingFileTime.dwHighDateTime = (uint)(fileTime >> 32);
            return resultingFileTime; 
        }

        internal static DateTime FileTime2DateTime(FILETIME fileTime)
        {
            if (fileTime.dwHighDateTime == 0 && fileTime.dwLowDateTime == 0)    //Checking for MinValue
                return DateTime.MinValue;

            long dateTime = (((long)fileTime.dwHighDateTime) << 32) + fileTime.dwLowDateTime;
            return DateTime.FromFileTimeUtc(dateTime);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Svalbard and Jan Mayen Svalbard and Jan Mayen
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions