PInvoke - Structures

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mythran

    #1

    PInvoke - Structures

    [StructLayout(La youtKind.Sequen tial)]
    struct MyStruct
    {
    public int Number;
    }

    [StructLayout(La youtKind.Sequen tial)]
    struct MyStruct
    {
    private int mNumber;
    public int Number
    {
    get {
    return mNumber;
    }
    set {
    mNumber = value;
    }
    }
    }

    For PInvoke calls, are the two structures above interchangeable ? If a
    structure is defined as the first MyStruct, can I use an instance of the 2nd
    MyStruct?

    Thanks,
    Mythran

  • Michael C

    #2
    Re: PInvoke - Structures

    "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
    news:eJAseb$NGH A.2828@TK2MSFTN GP12.phx.gbl...[color=blue]
    > For PInvoke calls, are the two structures above interchangeable ? If a
    > structure is defined as the first MyStruct, can I use an instance of the
    > 2nd MyStruct?[/color]

    I dunno, give it a try and see. You'll have your answer in a minute or so.

    Michael


    Comment

    • Mythran

      #3
      Re: PInvoke - Structures


      "Michael C" <nospam@nospam. com> wrote in message
      news:egDCAi$NGH A.1216@TK2MSFTN GP14.phx.gbl...[color=blue]
      > "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
      > news:eJAseb$NGH A.2828@TK2MSFTN GP12.phx.gbl...[color=green]
      >> For PInvoke calls, are the two structures above interchangeable ? If a
      >> structure is defined as the first MyStruct, can I use an instance of the
      >> 2nd MyStruct?[/color]
      >
      > I dunno, give it a try and see. You'll have your answer in a minute or so.
      >
      > Michael
      >[/color]

      I did try and the results are...confusing :)

      I created a struct for RECT and used properties instead of members. It
      worked fine. When I put Console.WriteLi ne inside the getter for Left, the
      returned value for the function I used returned an empty
      structure...rep eatedly.

      <shrug>

      Mythran

      Comment

      • Michael C

        #4
        Re: PInvoke - Structures

        "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
        news:eQgMNOBOGH A.3284@TK2MSFTN GP14.phx.gbl...[color=blue]
        > I did try and the results are...confusing :)
        >
        > I created a struct for RECT and used properties instead of members. It
        > worked fine. When I put Console.WriteLi ne inside the getter for Left, the
        > returned value for the function I used returned an empty
        > structure...rep eatedly.[/color]

        I don't quite get what you mean here?

        [color=blue]
        >
        > <shrug>
        >
        > Mythran
        >[/color]


        Comment

        • Mythran

          #5
          Re: PInvoke - Structures


          "Michael C" <nospam@nospam. com> wrote in message
          news:eiBmKoPOGH A.2816@TK2MSFTN GP15.phx.gbl...[color=blue]
          > "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
          > news:eQgMNOBOGH A.3284@TK2MSFTN GP14.phx.gbl...[color=green]
          >> I did try and the results are...confusing :)
          >>
          >> I created a struct for RECT and used properties instead of members. It
          >> worked fine. When I put Console.WriteLi ne inside the getter for Left,
          >> the returned value for the function I used returned an empty
          >> structure...rep eatedly.[/color]
          >
          > I don't quite get what you mean here?
          >
          >[color=green]
          >>
          >> <shrug>
          >>
          >> Mythran
          >>[/color]
          >
          >[/color]

          I don't remember which api call I made, but it returns a code that you can
          use to identify what the contents of RECT is. I set it up using the normal
          RECT layout, and it worked fine (the return code was the code that said it
          had data in it). When I changed to properties, it also worked fine. Once I
          put in a Console.WriteLi ne inside the get {} part of the Left property, the
          return code was the code the meant there was no data in the RECT structure
          (the RECT was empty).

          Mythran

          Comment

          • Mythran

            #6
            Re: PInvoke - Structures

            Ok, I tried this (no code included, just letting ya know what I tried).

            In my Left property for my struct that contains public properties instead of
            public members, I put a Console.WriteLi ne("HERE"); after I set the private
            member, mLeft. When I pass this structure to the API function, the
            structure is returned (out parameter) with data in it. The
            Console.WriteLi ne() call is never called though (no "HERE" on console
            window). The Left value is set though. It could be that it just places the
            values in memory at the locations of the first 4 members, I'd have to give
            it a try and test it out. But it's not that important :)

            Mythran

            Comment

            • Michael C

              #7
              Re: PInvoke - Structures

              "Mythran" <kip_potter@hot mail.comREMOVET RAIL> wrote in message
              news:edm9yJZOGH A.2916@tk2msftn gp13.phx.gbl...[color=blue]
              > Ok, I tried this (no code included, just letting ya know what I tried).
              >
              > In my Left property for my struct that contains public properties instead
              > of public members, I put a Console.WriteLi ne("HERE"); after I set the
              > private member, mLeft. When I pass this structure to the API function,
              > the structure is returned (out parameter) with data in it. The
              > Console.WriteLi ne() call is never called though (no "HERE" on console
              > window). The Left value is set though. It could be that it just places
              > the values in memory at the locations of the first 4 members, I'd have to
              > give it a try and test it out. But it's not that important :)[/color]

              That makes sense, it must be picking up the private variables still and
              marshalling those.

              Michael


              Comment

              Working...