如何将System.DirectoryEntry“uSNChanged”属性值更改为Int64

时间:2021-10-03 03:00:25

I'm trying to get the Int64 value of a Directory Services object's "uSNChanged" value. Unfortunately, it is always coming back as a COM object of some kind. I've tried using casting to Int64, calling Int64.Parse(), and calling Convert.ToInt64(). None of these work.

我正在尝试获取目录服务对象的“uSNChanged”值的Int64值。不幸的是,它总是以某种COM对象的形式回归。我尝试使用转换为Int64,调用Int64.Parse(),并调用Convert.ToInt64()。这些都不起作用。

For a given DirectoryEntry object, this code will display the properties:

对于给定的DirectoryEntry对象,此代码将显示以下属性:

    private static void DisplaySelectedProperties(DirectoryEntry objADObject)
    {
        try
        {
            string[] properties = new string[] {
                "displayName",
                "whenCreated",
                "whenChanged",
                "uSNCreated",
                "uSNChanged",
            };

            Console.WriteLine(String.Format("Displaying selected properties of {0}", objADObject.Path));
            foreach (string strAttrName in properties)
            {
                foreach (var objAttrValue in objADObject.Properties[strAttrName])
                {
                    string strAttrValue = objAttrValue.ToString();
                    Console.WriteLine(String.Format("   {0, -22} : {1}", strAttrName, strAttrValue));
                }
            }
            Console.WriteLine();
        }
        catch (Exception ex)
        {
            throw new ApplicationException(string.Format("Fatal error accessing: {0} - {1}", objADObject.Path, ex.Message), ex);
        }
    }

This is the output:

这是输出:

Displaying selected properties of LDAP://server/o=org/cn=obj
   displayName            : Display Name
   whenCreated            : 7/8/2009 7:29:02 PM
   whenChanged            : 7/8/2009 10:42:23 PM
   uSNCreated             : System.__ComObject
   uSNChanged             : System.__ComObject

How do I convert that System.__ComObject into a Int64?

如何将该System .__ ComObject转换为Int64?


Solution Used:

This is the solution I used based on marc_s's solution below:

这是我根据marc_s的解决方案使用的解决方案:

    public static Int64 ConvertADSLargeIntegerToInt64(object adsLargeInteger)
    {
         var highPart = (Int32)adsLargeInteger.GetType().InvokeMember("HighPart", System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         var lowPart  = (Int32)adsLargeInteger.GetType().InvokeMember("LowPart",  System.Reflection.BindingFlags.GetProperty, null, adsLargeInteger, null);
         return highPart * ((Int64)UInt32.MaxValue + 1) + lowPart;
    }

2 个解决方案

#1


I'm using this snippet of code in my ADSI Browser BeaverTail which is written in C#:

我在我的ADSI浏览器BeaverTail中使用这段代码,它是用C#编写的:

Int64 iLargeInt = 0;

IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

As far as I can tell, this should work just fine.

据我所知,这应该工作得很好。

Marc

#2


It looks like it's an IADsLargeInteger type, so a little interop magic will be required to extract the values. This Thread contains a sample VB implementation -- and mentions problems similar to your own -- however I'm nowhere near able to verify the usefulness of it right now. Hope this helps.

看起来它是一个IADsLargeInteger类型,因此需要一点互操作魔法来提取值。这个Thread包含一个示例VB实现 - 并提到类似于您自己的问题 - 但是我现在无法验证它的有用性。希望这可以帮助。

#1


I'm using this snippet of code in my ADSI Browser BeaverTail which is written in C#:

我在我的ADSI浏览器BeaverTail中使用这段代码,它是用C#编写的:

Int64 iLargeInt = 0;

IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;

As far as I can tell, this should work just fine.

据我所知,这应该工作得很好。

Marc

#2


It looks like it's an IADsLargeInteger type, so a little interop magic will be required to extract the values. This Thread contains a sample VB implementation -- and mentions problems similar to your own -- however I'm nowhere near able to verify the usefulness of it right now. Hope this helps.

看起来它是一个IADsLargeInteger类型,因此需要一点互操作魔法来提取值。这个Thread包含一个示例VB实现 - 并提到类似于您自己的问题 - 但是我现在无法验证它的有用性。希望这可以帮助。