none
How to write to a 0 length array in C# which has been passed in by a PowerShell CmdLet RRS feed

  • Question

  • Hello,

    [string[]]$MyInArray = @( "System_UUID", "System_BIOS_VERSION" )

    [string[]]$MyOutArray = @()

    MyCmdLet -InputArray $MyInArray -OutputArray $MyOutArray


    Write-Host $MyOutArray.ToString()

    I create a $MyInArray in PS fill it with data and send it across to a C# based DLL. Along with that I pass in a null string Array called $MyOutArray. The question is how do I dynamically allocate memory to a string. Thanks a lot in advance for any help/hint to resolve this


    Sunday, January 28, 2018 3:01 AM

Answers

  • Finally I solved it. So here is the trick.

    $DataInList = New-Object System.Collections.Generic.List[string];

    $DataOutList = New-Object System.Collections.Generic.List[string];

    This goes as a parameter to the CmdLet from the PS

    In the DLL in the Cmdlet the return type has to be QueueObjectForWriting

    In the driver the return type for the method should be List<string>

    Then when it executed 

    $DataOutList = GetInfoCmdLet <with all its options> 

    $DataOutList will have the value

    Thanks to Sam for the help, the case is closed

    Tuesday, January 30, 2018 2:06 AM

All replies

  • I am so confused. First, is MyOutArray the same thing as OutArray? To the computer those are totally different and even for us humans we have to be careful.

    I am not sure what the requirements are; do you say that PS allocates an array with no items and you must use that array as output? That sounds impossible to me. I don't know PS so I might not be able to help but is there any way you can get PS to use an array that you create in C#? If not then it seems impossible. In C# we cannot increase the size of an array; we must allocate a new array. If PS arrays are like that then it seems impossible to do what you are trying to do. In .Net there are many collections (such as Lists)  that would solve the problem very easily. Can you use a .Net collection?



    Sam Hobbs
    SimpleSamples.Info

    Sunday, January 28, 2018 5:28 AM
  • Thanks Sam, and very sorry for getting back late. Was out of the internet world over the weekend. Yes I have tried the List too but the problem is the List gets filled in the DLL but it will be local memory and back in the PS it becomes out of scope. So I have to do what we do in C like a malloc().

    char * abcd;

    abcd = (char *)malloc(1234);

    I have to do something like that in the DLL which is written in C#. That is my new question. 

    When I submitted this earlier I had not thought of this idea your answer helped me. Let me try that and get back ASAP. 

    best regards

    ananda

    Monday, January 29, 2018 2:32 PM
  • Another possibility might be to make two functions, the first one to get the number of items and the other to get the items. Then PowerShell can allocate the array for the correct size.


    Sam Hobbs
    SimpleSamples.Info

    Monday, January 29, 2018 8:18 PM
  • I spent the whole day trying all possible solutions and none worked. However I have a very clean statement of my issue. I shall edit my first posting so as not to confuse the future viewers
    Monday, January 29, 2018 11:32 PM
  • Finally I solved it. So here is the trick.

    $DataInList = New-Object System.Collections.Generic.List[string];

    $DataOutList = New-Object System.Collections.Generic.List[string];

    This goes as a parameter to the CmdLet from the PS

    In the DLL in the Cmdlet the return type has to be QueueObjectForWriting

    In the driver the return type for the method should be List<string>

    Then when it executed 

    $DataOutList = GetInfoCmdLet <with all its options> 

    $DataOutList will have the value

    Thanks to Sam for the help, the case is closed

    Tuesday, January 30, 2018 2:06 AM