Comment changer System.DirectoryEntry « uSNChanged » valeur d'attribut à un Int64
-
11-09-2019 - |
Question
Je suis en train d'obtenir la valeur Int64 de la valeur « uSNChanged » d'un objet Directory Services. Malheureusement, il revient toujours comme un objet COM de quelque sorte. Je l'ai essayé d'utiliser la coulée à Int64, appelant Int64.Parse (), et appelant Convert.ToInt64 (). Aucune de ces travaux.
Pour un objet DirectoryEntry donné, ce code affiche les propriétés:
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);
}
}
Ceci est la sortie:
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
Comment puis-je convertir ce système .__ ComObject en Int64?
Solution utilisée:
Ceci est la solution que j'utilisée en fonction de la solution de marc_s ci-dessous:
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;
}
La solution
J'utilise ce bout de code dans mon navigateur ADSI BeaverTail qui est écrit en C #:
Int64 iLargeInt = 0;
IADsLargeInteger int64Val = (IADsLargeInteger)oPropValue.LargeInteger;
iLargeInt = int64Val.HighPart * 4294967296 + int64Val.LowPart;
Pour autant que je peux dire, cela devrait fonctionner très bien.
Marc
Autres conseils
On dirait qu'il est IADsLargeInteger le type, donc un peu de magie Interop sera nécessaire pour extraire les valeurs. Cette discussion comporte un échantillon VB mise en œuvre - et mentionne des problèmes similaires à votre propre - mais je suis loin d'être en mesure de vérifier l'utilité de celui-ci en ce moment. J'espère que cela t'aides.