title: powershell calling exported function of a c++ dll with structure type parameter
body: <pre><code>struct foostruct
  {
      int B;
      int A;
      int D;
  };
  
  int foofunc(foostruct foo)
  {
      return foo.A;
  }
  </code></pre>
  
  <p>I have exported <code>FooFunc</code> from <code>test.dll</code> 
  Now I am trying to call this from powershell</p>
  
  <pre><code>$source =  @"
  public struct foostruct {
  public int B;
  public int A;
  public int D;
  }
  "@
  Add-Type -TypeDefinition $Source -IgnoreWarnings
  $bar = New-Object foostruct
  $bar.B = 1;
  $bar.A = 2;
  $bar.D = 3;
  
  $MethodDefinition = @'
  
  [DllImport("test.dll", CharSet = CharSet.Unicode, SetLastError = true)]
  
  public static extern int foofunc(foostruct foo);
  
  '@
  
  $dll = Add-Type -MemberDefinition $MethodDefinition -Name 'dll' -PassThru -IgnoreWarnings
  </code></pre>
  
  <p>This throws error</p>
  
  <p><code>The type or namespace name 'foostruct' could not be found (are you missing a using directive or an assembly reference?)</code></p>
  
  <p>I tried moving the definition of <code>fostruct</code> inside <code>$MethodDefinition</code> as mentioned <a href="https://stackoverflow.com/q/32218847/3475381">here</a> but then when i call it using the below i get <code>Unable to cast object of type 'System.Object[,]' to type 'System.Object[]'</code> error.</p>
  
  <pre><code>$Result = $dll::foofunc($bar)
  </code></pre>