Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. pInvoke Struct

pInvoke Struct

Scheduled Pinned Locked Moved C#
csharpquestion
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Saksida Bojan
    wrote on last edited by
    #1

    I seen a struct on a pInvoke.Net

    [StructLayout(LayoutKind.Sequential)]
    public struct PROCESSENTRY32
    {
    public uint dwSize;
    public uint cntUsage;
    public uint th32ProcessID;
    public IntPtr th32DefaultHeapID;
    public uint th32ModuleID;
    public uint cntThreads;
    public uint th32ParentProcessID;
    public int pcPriClassBase;
    public uint dwFlags;
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
    };

    PROCESSENTRY32 pe = new PROCESSENTRY32();
    pe.dwSize = (uint)Marshal.SizeOf(pe);

    I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags

    realJSOPR D L 3 Replies Last reply
    0
    • S Saksida Bojan

      I seen a struct on a pInvoke.Net

      [StructLayout(LayoutKind.Sequential)]
      public struct PROCESSENTRY32
      {
      public uint dwSize;
      public uint cntUsage;
      public uint th32ProcessID;
      public IntPtr th32DefaultHeapID;
      public uint th32ModuleID;
      public uint cntThreads;
      public uint th32ParentProcessID;
      public int pcPriClassBase;
      public uint dwFlags;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
      };

      PROCESSENTRY32 pe = new PROCESSENTRY32();
      pe.dwSize = (uint)Marshal.SizeOf(pe);

      I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags

      realJSOPR Offline
      realJSOPR Offline
      realJSOP
      wrote on last edited by
      #2

      Inside what constructor? The only thing you have to worry about is establishing the size of the struct so that the WinAPI knows how big the object is. The only concern about initializing it is what scope the resulting object has.

      .45 ACP - because shooting twice is just silly
      -----
      "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
      -----
      "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

      S 1 Reply Last reply
      0
      • realJSOPR realJSOP

        Inside what constructor? The only thing you have to worry about is establishing the size of the struct so that the WinAPI knows how big the object is. The only concern about initializing it is what scope the resulting object has.

        .45 ACP - because shooting twice is just silly
        -----
        "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
        -----
        "The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001

        S Offline
        S Offline
        Saksida Bojan
        wrote on last edited by
        #3

        i see, so it doesn't have struct problems like c# i know for size iz has to be:

        public struct Size
        {
        public Size(int x, int y)
        {
        X=x;
        Y=y;
        }
        public int X;
        public int Y;
        }

        And it shoudln't be used as:

        Size size = new Size(1,1);
        size.X = 5;
        size.Y = 10;

        1 Reply Last reply
        0
        • S Saksida Bojan

          I seen a struct on a pInvoke.Net

          [StructLayout(LayoutKind.Sequential)]
          public struct PROCESSENTRY32
          {
          public uint dwSize;
          public uint cntUsage;
          public uint th32ProcessID;
          public IntPtr th32DefaultHeapID;
          public uint th32ModuleID;
          public uint cntThreads;
          public uint th32ParentProcessID;
          public int pcPriClassBase;
          public uint dwFlags;
          [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
          };

          PROCESSENTRY32 pe = new PROCESSENTRY32();
          pe.dwSize = (uint)Marshal.SizeOf(pe);

          I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags

          D Offline
          D Offline
          DaveyM69
          wrote on last edited by
          #4

          Saksida Bojan wrote:

          strut should be intalized inside constructior

          Correct, however every struct has a default parameterless constructor that cannot be overridden. If you are creating your own constructor (with parameters) then you would need to need to initialize every field inside that constructor. Nothing to do with your question, but *sometimes* when PInvoking you can interchange int/uint. The size of the struct you have declared will be 296 bytes on a 32 bit system and 300 bytes on a 64 bit system. This is well within the range of an int so you can safely declaredwSize as an int (still 4 bytes) and get rid of the cast to uint when calculating the size.

          Dave
          Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
          BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

          1 Reply Last reply
          0
          • S Saksida Bojan

            I seen a struct on a pInvoke.Net

            [StructLayout(LayoutKind.Sequential)]
            public struct PROCESSENTRY32
            {
            public uint dwSize;
            public uint cntUsage;
            public uint th32ProcessID;
            public IntPtr th32DefaultHeapID;
            public uint th32ModuleID;
            public uint cntThreads;
            public uint th32ParentProcessID;
            public int pcPriClassBase;
            public uint dwFlags;
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst=260)] public string szExeFile;
            };

            PROCESSENTRY32 pe = new PROCESSENTRY32();
            pe.dwSize = (uint)Marshal.SizeOf(pe);

            I know i have read somewhere here that strut should be intalized inside constructior or it will couse problems. Does that applay to WinAPI too? Edit: Forgot code tags

            L Offline
            L Offline
            Lost User
            wrote on last edited by
            #5

            If you don't explicitly initialize the fields in a struct (such as when using the default ctor) all fields will be initialized to their default value (0, 0.0, null, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:

            11.1.2 Default constructors

            All value types implicitly declare a public parameterless instance constructor called the default constructor.
            The default constructor returns a zero-initialized instance known as the default value for the value type:

            Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?

            modified on Saturday, September 18, 2010 12:46 PM

            D S 2 Replies Last reply
            0
            • L Lost User

              If you don't explicitly initialize the fields in a struct (such as when using the default ctor) all fields will be initialized to their default value (0, 0.0, null, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:

              11.1.2 Default constructors

              All value types implicitly declare a public parameterless instance constructor called the default constructor.
              The default constructor returns a zero-initialized instance known as the default value for the value type:

              Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?

              modified on Saturday, September 18, 2010 12:46 PM

              D Offline
              D Offline
              DaveyM69
              wrote on last edited by
              #6

              harold aptroot wrote:

              lang="none"

              Try lang="text"

              Dave
              Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
              BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

              L 1 Reply Last reply
              0
              • D DaveyM69

                harold aptroot wrote:

                lang="none"

                Try lang="text"

                Dave
                Binging is like googling, it just feels dirtier. Please take your VB.NET out of our nice case sensitive forum. Astonish us. Be exceptional. (Pete O'Hanlon)
                BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                Thanks, that did the trick. "none" used to work though, I always used that.

                1 Reply Last reply
                0
                • L Lost User

                  If you don't explicitly initialize the fields in a struct (such as when using the default ctor) all fields will be initialized to their default value (0, 0.0, null, etc) That's not really a "problem", and it's well-defined behaviour. Even if you have a huge struct like this. The semantics of the default ctor are described in §11.1.2 of ECMA-334:

                  11.1.2 Default constructors

                  All value types implicitly declare a public parameterless instance constructor called the default constructor.
                  The default constructor returns a zero-initialized instance known as the default value for the value type:

                  Followed by a table of types and their default values. What kind of problems do you expect? edit: pre with lang="none" appears to be broken..?

                  modified on Saturday, September 18, 2010 12:46 PM

                  S Offline
                  S Offline
                  Saksida Bojan
                  wrote on last edited by
                  #8

                  harold aptroot wrote:

                  What kind of problems do you expect?

                  I do not know if i will have a problem. I don't remeber where i read, maybe a dsicusion Class versus Struct. that if you change struct member value, you actualy intilizing new instane of a struct. Does that happen in WinAPI or shoudn't i be worried about that? Maybe it is my paranoia.

                  L 1 Reply Last reply
                  0
                  • S Saksida Bojan

                    harold aptroot wrote:

                    What kind of problems do you expect?

                    I do not know if i will have a problem. I don't remeber where i read, maybe a dsicusion Class versus Struct. that if you change struct member value, you actualy intilizing new instane of a struct. Does that happen in WinAPI or shoudn't i be worried about that? Maybe it is my paranoia.

                    L Offline
                    L Offline
                    Lost User
                    wrote on last edited by
                    #9

                    Saksida Bojan wrote:

                    that if you change struct member value, you actualy intilizing new instane of a struct

                    That is not what happens. Something related does happen though - structs are often copied because their semantics require it, and then changing a field in the struct only causes the copy to change. That is also why it sometimes isn't even allowed.

                    S 1 Reply Last reply
                    0
                    • L Lost User

                      Saksida Bojan wrote:

                      that if you change struct member value, you actualy intilizing new instane of a struct

                      That is not what happens. Something related does happen though - structs are often copied because their semantics require it, and then changing a field in the struct only causes the copy to change. That is also why it sometimes isn't even allowed.

                      S Offline
                      S Offline
                      Saksida Bojan
                      wrote on last edited by
                      #10

                      thank you for answer

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups