How to Fetch data in C/AL from DotNet ManagementObjectCollection object?

hsnfritzhsnfritz Member Posts: 3

Hi guys

I am working in NAV-2013 R2 and using sample code to Change DefaultPrinter in C/AL using .net objects from http://www.punky.be/2017/01/31/change-default-printer-from-cal/

I have made some changes. the data types are

i4goe8vl7ivf.png

Here is the code sample

ManagementObjectSearcher := ManagementObjectSearcher.ManagementObjectSearcher('select * from win32_printer');
ManagementObjectCollection := ManagementObjectSearcher.Get();
ObjectArray := ObjectArray.CreateInstance(GETDOTNETTYPE(Object),1);

Count := ManagementObjectCollection.Count;

varArray := varArray.CreateInstance(ManagementObjectCollection.GetType(), Count);

//***
where varArray is DotNet array of type => System.Array.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
***//

ManagementObjectCollection.CopyTo(varArray, 0);

//*** On calling this COPYTO function I get error
I get an error message that call is ambiguous between following methods i.e.
=> CopyTo(ManagementBaseObject[], int)
and
=>CopyTo(System.Array, int)
***//

FOR i := 0 TO (Count - 1) DO
BEGIN
ManagementObject := varArray.GetValue(i);
i := i + 1;
IF FORMAT(ManagementObject.GetPropertyValue('Name')) = PrinterName THEN BEGIN
ManagementObject.InvokeMethod('SetDefaultPrinter', ObjectArray);
EXIT;
END;
END;

Can you tell me how to reterive the data from ManagementObjectCollection and fix this error ?

Best Answer

  • hsnfritzhsnfritz Member Posts: 3
    Answer ✓
    I fixed it by using enumerators
    WITH ManagementObjectCollection DO BEGIN
      Enumerator := ManagementObjectCollection.GetEnumerator();
      WHILE(Enumerator.MoveNext()) DO
      BEGIN
        ManagementObject := Enumerator.Current; 
        PrinterNameTmp := FORMAT(ManagementObject.GetPropertyValue('Name'));
        MESSAGE(PrinterNameTmp);
        IF PrinterNameTmp = PrinterName THEN BEGIN
          ManagementObject.InvokeMethod('SetDefaultPrinter', ObjectArray);
          EXIT;
        END;  
      END;
    END;
    

Answers

  • okioki Member Posts: 46
    instead of:
    varArray := varArray.CreateInstance(ManagementObjectCollection.GetType(), Count);

    try:
    varArray := varArray.CreateInstance(ManagementObject.GetType(), Count);

    hope it helps.

    Oli

  • hsnfritzhsnfritz Member Posts: 3
    I already tried it but it didn't worked .. I got same error
  • hsnfritzhsnfritz Member Posts: 3
    Answer ✓
    I fixed it by using enumerators
    WITH ManagementObjectCollection DO BEGIN
      Enumerator := ManagementObjectCollection.GetEnumerator();
      WHILE(Enumerator.MoveNext()) DO
      BEGIN
        ManagementObject := Enumerator.Current; 
        PrinterNameTmp := FORMAT(ManagementObject.GetPropertyValue('Name'));
        MESSAGE(PrinterNameTmp);
        IF PrinterNameTmp = PrinterName THEN BEGIN
          ManagementObject.InvokeMethod('SetDefaultPrinter', ObjectArray);
          EXIT;
        END;  
      END;
    END;
    
Sign In or Register to comment.