Home > Back-end >  PowerShell Add-Type Simple C# Structures -- One works and one doessn't
PowerShell Add-Type Simple C# Structures -- One works and one doessn't

Time:09-17

I've had some success with p-Invoking some API functions, but now something seemingly simple has me stumped.

This code works:

Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

public class WPAPI1 {

    [StructLayout(LayoutKind.Sequential)]
    public struct POINT {
        public long x;
        public long y;

        public POINT( long x, long y)
        {
            this.x = x;
            this.y = y;
        }
    };
}
'@

Output and test:

PS > Add-Type -TypeDefinition @'
>> using System;
>> using System.IO;
>> using System.Text;
>> using System.Runtime.InteropServices;
>>
>> public class WPAPI1 {
>>
>>     [StructLayout(LayoutKind.Sequential)]
>>     public struct POINT {
>>         public long x;
>>         public long y;
>>
>>         public POINT( long x, long y)
>>         {
>>             this.x = x;
>>             this.y = y;
>>         }
>>     };
>> }
>> '@
>>
WARNING: The generated type defines no public methods or properties.
PS > $p = [WPAPI1 POINT]::new(5,10)
PS > $p

x  y
-  -
5 10

But this code won't compile:

Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

public class WPAPI2 {

    StructLayout(LayoutKind.Sequential)]
    public struct RECT {
        public long left;
        public long top;
        public long right;
        public long bottom;

        public RECT(long left, long top, long right, long botton)
        {
            this.left   = left;
            this.top    = top;
            this.right  = right;
            this.bottom = bottom;
        }
    };
}
'@

Throwing the following errors:

Add-Type : c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : Method must have a
return type
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(9) :     public struct RECT {
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
   mpilerError) [Add-Type], Exception
      FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
   mmand

Add-Type : c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : Identifier expected
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(9) :     public struct RECT {
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
   mpilerError) [Add-Type], Exception
      FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
   mmand

Add-Type : c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : ; expected
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(7) :
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(8) : >>>
StructLayout(LayoutKind.Sequential)]
c:\Users\keith\AppData\Local\Temp\3v2a5y04.0.cs(9) :     public struct RECT {
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
   mpilerError) [Add-Type], Exception
      FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
   mmand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
      FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeComm
   and

PS >

Focusing on the first error: Method must have a return type, my internet searches aren't helping. Consturctors aren't supposed to need a return type, and I've triple-checked case and spelling of RECT.

I'm stumped. Please advise...

Edit:

Fixed the missing [ (thought my error had changed but it was getting late). After that, code produces different error:

Add-Type : c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : Warning as Error:
Assignment made to same variable; did you mean to assign something else?
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(16) :         {
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : >>>             this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(18) :         }
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
   mpilerError) [Add-Type], Exception
      FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
   mmand

Add-Type : c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : Use of possibly
unassigned field 'bottom'
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(16) :         {
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(17) : >>>             this.left = left
; this.top = top ; this.right = right ; this.bottom = bottom;
c:\Users\keith\AppData\Local\Temp\saoucata.0.cs(18) :         }
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (Microsoft.Power...peCompilerError:AddTypeCo
   mpilerError) [Add-Type], Exception
      FullyQualifiedErrorId : SOURCE_CODE_ERROR,Microsoft.PowerShell.Commands.AddTypeCo
   mmand

Add-Type : Cannot add type. Compilation errors occurred.
At line:1 char:1
  Add-Type -TypeDefinition @'
  ~~~~~~~~~~~~~~~~~~~~~~~~~~~
      CategoryInfo          : InvalidData: (:) [Add-Type], InvalidOperationException
      FullyQualifiedErrorId : COMPILER_ERRORS,Microsoft.PowerShell.Commands.AddTypeComm
   and

I appreciate the help. Visually impaired here -- have to zoom in a lot -- can make one lose the "big picture", so to speak! :D

CodePudding user response:

Add-Type -TypeDefinition @'
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

public class WPAPI2 {

    [StructLayout(LayoutKind.Sequential)]
    public struct RECT {
        public long left;
        public long top;
        public long right;
        public long bottom;

        public RECT(long left, long top, long right, long bottom)
        {
            this.left   = left;
            this.top    = top;
            this.right  = right;
            this.bottom = bottom;
        }
    };
}
'@

Note: "[" before StructuredLayout and "botton" vs "bottom" in the RECT signature.

  • Related