Array of pointer in C#

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

    #1

    Array of pointer in C#

    Hi,
    Could you please help me how to declare an araay of pointer in C#.
    In my program I declared an structure

    public struct SEventQ
    {
    public uint uiUserData;
    public uint uiEvent;
    public uint uiParam0;
    public uint uiParam1;
    }

    Now I want to delare an array of pointer look likes
    unsafe SEventQ [] *EvQ = new SEventQ[10];


    Thanks,

    Kathy Tran
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: Array of pointer in C#

    Kathy,

    Why do you want an array of pointers. Are you performing some sort of
    interop which requires an array of pointers to these structures to be taken,
    or do you need an array of structure?

    If you wanted an array of pointers, I believe you would do:

    SEventQ **EvQ;

    However, you couldn't say "new SEventQ*", since you can't "create" a
    pointer in that manner.

    What is it that you are trying to do?


    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m



    "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
    news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...[color=blue]
    > Hi,
    > Could you please help me how to declare an araay of pointer in C#.
    > In my program I declared an structure
    >
    > public struct SEventQ
    > {
    > public uint uiUserData;
    > public uint uiEvent;
    > public uint uiParam0;
    > public uint uiParam1;
    > }
    >
    > Now I want to delare an array of pointer look likes
    > unsafe SEventQ [] *EvQ = new SEventQ[10];
    >
    >
    > Thanks,
    >
    > Kathy Tran[/color]


    Comment

    • Kathy Tran

      #3
      Re: Array of pointer in C#

      Hi Nicholas Paldino ,
      Thanks for responding my question. I am performing some of interop which
      requires an array of pointers to these structures to be taken. In my program
      I want to go through an array of pointer, with each element in array I assign
      some values in structure and then pass each element to un unmanaged code
      function

      [DllImport("Pika API.dll")]
      unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);

      but I am very confusing and do not how to declare an array of pointer.

      Thank,
      Kathy Tran

      "Nicholas Paldino [.NET/C# MVP]" wrote:
      [color=blue]
      > Kathy,
      >
      > Why do you want an array of pointers. Are you performing some sort of
      > interop which requires an array of pointers to these structures to be taken,
      > or do you need an array of structure?
      >
      > If you wanted an array of pointers, I believe you would do:
      >
      > SEventQ **EvQ;
      >
      > However, you couldn't say "new SEventQ*", since you can't "create" a
      > pointer in that manner.
      >
      > What is it that you are trying to do?
      >
      >
      > --
      > - Nicholas Paldino [.NET/C# MVP]
      > - mvp@spam.guard. caspershouse.co m
      >
      >
      >
      > "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
      > news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...[color=green]
      > > Hi,
      > > Could you please help me how to declare an araay of pointer in C#.
      > > In my program I declared an structure
      > >
      > > public struct SEventQ
      > > {
      > > public uint uiUserData;
      > > public uint uiEvent;
      > > public uint uiParam0;
      > > public uint uiParam1;
      > > }
      > >
      > > Now I want to delare an array of pointer look likes
      > > unsafe SEventQ [] *EvQ = new SEventQ[10];
      > >
      > >
      > > Thanks,
      > >
      > > Kathy Tran[/color]
      >
      >
      >[/color]

      Comment

      • Nicholas Paldino [.NET/C# MVP]

        #4
        Re: Array of pointer in C#

        Kathy,

        What is the original declaration (in C/C++) of the method? The one that
        you have is not an array of pointers, but rather, just a pointer to the
        structure, which is the same as an array of structures (not an array of
        pointers to a structure).


        --
        - Nicholas Paldino [.NET/C# MVP]
        - mvp@spam.guard. caspershouse.co m

        "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
        news:E9E0B4C6-1DAB-446C-BA5D-55DAB4EFF3CB@mi crosoft.com...[color=blue]
        > Hi Nicholas Paldino ,
        > Thanks for responding my question. I am performing some of interop which
        > requires an array of pointers to these structures to be taken. In my
        > program
        > I want to go through an array of pointer, with each element in array I
        > assign
        > some values in structure and then pass each element to un unmanaged code
        > function
        >
        > [DllImport("Pika API.dll")]
        > unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);
        >
        > but I am very confusing and do not how to declare an array of pointer.
        >
        > Thank,
        > Kathy Tran
        >
        > "Nicholas Paldino [.NET/C# MVP]" wrote:
        >[color=green]
        >> Kathy,
        >>
        >> Why do you want an array of pointers. Are you performing some sort
        >> of
        >> interop which requires an array of pointers to these structures to be
        >> taken,
        >> or do you need an array of structure?
        >>
        >> If you wanted an array of pointers, I believe you would do:
        >>
        >> SEventQ **EvQ;
        >>
        >> However, you couldn't say "new SEventQ*", since you can't "create" a
        >> pointer in that manner.
        >>
        >> What is it that you are trying to do?
        >>
        >>
        >> --
        >> - Nicholas Paldino [.NET/C# MVP]
        >> - mvp@spam.guard. caspershouse.co m
        >>
        >>
        >>
        >> "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
        >> news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...[color=darkred]
        >> > Hi,
        >> > Could you please help me how to declare an araay of pointer in C#.
        >> > In my program I declared an structure
        >> >
        >> > public struct SEventQ
        >> > {
        >> > public uint uiUserData;
        >> > public uint uiEvent;
        >> > public uint uiParam0;
        >> > public uint uiParam1;
        >> > }
        >> >
        >> > Now I want to delare an array of pointer look likes
        >> > unsafe SEventQ [] *EvQ = new SEventQ[10];
        >> >
        >> >
        >> > Thanks,
        >> >
        >> > Kathy Tran[/color]
        >>
        >>
        >>[/color][/color]


        Comment

        • Jonathan Woodbury

          #5
          Re: Array of pointer in C#

          This MSDN article has examples for this kind of thing:

          http://msdn.microsoft.com/msdnmag/issues/02/08/CQA/

          I hope one of the examples helps.

          - Jonathan W.


          "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
          news:E9E0B4C6-1DAB-446C-BA5D-55DAB4EFF3CB@mi crosoft.com...[color=blue]
          > Hi Nicholas Paldino ,
          > Thanks for responding my question. I am performing some of interop which
          > requires an array of pointers to these structures to be taken. In my
          > program
          > I want to go through an array of pointer, with each element in array I
          > assign
          > some values in structure and then pass each element to un unmanaged code
          > function
          >
          > [DllImport("Pika API.dll")]
          > unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);
          >
          > but I am very confusing and do not how to declare an array of pointer.
          >
          > Thank,
          > Kathy Tran
          >
          > "Nicholas Paldino [.NET/C# MVP]" wrote:
          >[color=green]
          >> Kathy,
          >>
          >> Why do you want an array of pointers. Are you performing some sort
          >> of
          >> interop which requires an array of pointers to these structures to be
          >> taken,
          >> or do you need an array of structure?
          >>
          >> If you wanted an array of pointers, I believe you would do:
          >>
          >> SEventQ **EvQ;
          >>
          >> However, you couldn't say "new SEventQ*", since you can't "create" a
          >> pointer in that manner.
          >>
          >> What is it that you are trying to do?
          >>
          >>
          >> --
          >> - Nicholas Paldino [.NET/C# MVP]
          >> - mvp@spam.guard. caspershouse.co m
          >>
          >>
          >>
          >> "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
          >> news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...[color=darkred]
          >> > Hi,
          >> > Could you please help me how to declare an araay of pointer in C#.
          >> > In my program I declared an structure
          >> >
          >> > public struct SEventQ
          >> > {
          >> > public uint uiUserData;
          >> > public uint uiEvent;
          >> > public uint uiParam0;
          >> > public uint uiParam1;
          >> > }
          >> >
          >> > Now I want to delare an array of pointer look likes
          >> > unsafe SEventQ [] *EvQ = new SEventQ[10];
          >> >
          >> >
          >> > Thanks,
          >> >
          >> > Kathy Tran[/color]
          >>
          >>
          >>[/color][/color]


          Comment

          • Kathy Tran

            #6
            Re: Array of pointer in C#

            Hi Nicholas Paldino,
            This is the original declaration in C

            typedef struct TBufferHeader_t ag
            {
            PK_PCHAR lpData;
            PK_U32 dwBufferLength;
            PK_U32 dwBytesRecorded ;
            PK_U32 dwUser;
            PK_U32 dwFlags;
            PK_U32 dwTimeStamp;
            struct TBufferHeader_t ag *lpNext;
            PK_U32 reserved;
            } TBufferHeader, *PBufferHeader;

            Syntax
            PK_STATUS PK_AUDIO_InputA ddBuffer
            (
            IN TResourceHandle hPort, // TResourceHandle is an int
            IN PBufferHeader pBuffer
            );

            My program will call the above function

            Kathy Tran
            "Nicholas Paldino [.NET/C# MVP]" wrote:
            [color=blue]
            > Kathy,
            >
            > What is the original declaration (in C/C++) of the method? The one that
            > you have is not an array of pointers, but rather, just a pointer to the
            > structure, which is the same as an array of structures (not an array of
            > pointers to a structure).
            >
            >
            > --
            > - Nicholas Paldino [.NET/C# MVP]
            > - mvp@spam.guard. caspershouse.co m
            >
            > "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
            > news:E9E0B4C6-1DAB-446C-BA5D-55DAB4EFF3CB@mi crosoft.com...[color=green]
            > > Hi Nicholas Paldino ,
            > > Thanks for responding my question. I am performing some of interop which
            > > requires an array of pointers to these structures to be taken. In my
            > > program
            > > I want to go through an array of pointer, with each element in array I
            > > assign
            > > some values in structure and then pass each element to un unmanaged code
            > > function
            > >
            > > [DllImport("Pika API.dll")]
            > > unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);
            > >
            > > but I am very confusing and do not how to declare an array of pointer.
            > >
            > > Thank,
            > > Kathy Tran
            > >
            > > "Nicholas Paldino [.NET/C# MVP]" wrote:
            > >[color=darkred]
            > >> Kathy,
            > >>
            > >> Why do you want an array of pointers. Are you performing some sort
            > >> of
            > >> interop which requires an array of pointers to these structures to be
            > >> taken,
            > >> or do you need an array of structure?
            > >>
            > >> If you wanted an array of pointers, I believe you would do:
            > >>
            > >> SEventQ **EvQ;
            > >>
            > >> However, you couldn't say "new SEventQ*", since you can't "create" a
            > >> pointer in that manner.
            > >>
            > >> What is it that you are trying to do?
            > >>
            > >>
            > >> --
            > >> - Nicholas Paldino [.NET/C# MVP]
            > >> - mvp@spam.guard. caspershouse.co m
            > >>
            > >>
            > >>
            > >> "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
            > >> news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...
            > >> > Hi,
            > >> > Could you please help me how to declare an araay of pointer in C#.
            > >> > In my program I declared an structure
            > >> >
            > >> > public struct SEventQ
            > >> > {
            > >> > public uint uiUserData;
            > >> > public uint uiEvent;
            > >> > public uint uiParam0;
            > >> > public uint uiParam1;
            > >> > }
            > >> >
            > >> > Now I want to delare an array of pointer look likes
            > >> > unsafe SEventQ [] *EvQ = new SEventQ[10];
            > >> >
            > >> >
            > >> > Thanks,
            > >> >
            > >> > Kathy Tran
            > >>
            > >>
            > >>[/color][/color]
            >
            >
            >[/color]

            Comment

            • Kathy Tran

              #7
              Re: Array of pointer in C#

              Hi Nicholas Paldino ,
              The original declaration is
              typedef struct TBufferHeader_t ag
              {
              PK_PCHAR lpData;
              PK_U32 dwBufferLength;
              PK_U32 dwBytesRecorded ;
              PK_U32 dwUser;
              PK_U32 dwFlags;
              PK_U32 dwTimeStamp;
              struct TBufferHeader_t ag *lpNext;
              PK_U32 reserved;
              } TBufferHeader, *PBufferHeader;
              The functon my program are going to call is
              Syntax
              PK_STATUS PK_AUDIO_InputA ddBuffer
              (
              IN TResourceHandle hPort, // TResourceHandle is an int
              IN PBufferHeader pBuffer
              );

              I am trying to use
              [DllImport("Pika API.dll")]
              unsafe public static extern int PK_AUDIO_InputA ddBuffer(uint
              TResourceHandle , TBufferHeader *pBuffer);

              But I got errors when I compiled
              Cannot take the address or size of a variable of a managed type
              (TBufferHeader)

              Thanks for any help
              Kathy Tran

              "Nicholas Paldino [.NET/C# MVP]" wrote:
              [color=blue]
              > Kathy,
              >
              > What is the original declaration (in C/C++) of the method? The one that
              > you have is not an array of pointers, but rather, just a pointer to the
              > structure, which is the same as an array of structures (not an array of
              > pointers to a structure).
              >
              >
              > --
              > - Nicholas Paldino [.NET/C# MVP]
              > - mvp@spam.guard. caspershouse.co m
              >
              > "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
              > news:E9E0B4C6-1DAB-446C-BA5D-55DAB4EFF3CB@mi crosoft.com...[color=green]
              > > Hi Nicholas Paldino ,
              > > Thanks for responding my question. I am performing some of interop which
              > > requires an array of pointers to these structures to be taken. In my
              > > program
              > > I want to go through an array of pointer, with each element in array I
              > > assign
              > > some values in structure and then pass each element to un unmanaged code
              > > function
              > >
              > > [DllImport("Pika API.dll")]
              > > unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);
              > >
              > > but I am very confusing and do not how to declare an array of pointer.
              > >
              > > Thank,
              > > Kathy Tran
              > >
              > > "Nicholas Paldino [.NET/C# MVP]" wrote:
              > >[color=darkred]
              > >> Kathy,
              > >>
              > >> Why do you want an array of pointers. Are you performing some sort
              > >> of
              > >> interop which requires an array of pointers to these structures to be
              > >> taken,
              > >> or do you need an array of structure?
              > >>
              > >> If you wanted an array of pointers, I believe you would do:
              > >>
              > >> SEventQ **EvQ;
              > >>
              > >> However, you couldn't say "new SEventQ*", since you can't "create" a
              > >> pointer in that manner.
              > >>
              > >> What is it that you are trying to do?
              > >>
              > >>
              > >> --
              > >> - Nicholas Paldino [.NET/C# MVP]
              > >> - mvp@spam.guard. caspershouse.co m
              > >>
              > >>
              > >>
              > >> "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
              > >> news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...
              > >> > Hi,
              > >> > Could you please help me how to declare an araay of pointer in C#.
              > >> > In my program I declared an structure
              > >> >
              > >> > public struct SEventQ
              > >> > {
              > >> > public uint uiUserData;
              > >> > public uint uiEvent;
              > >> > public uint uiParam0;
              > >> > public uint uiParam1;
              > >> > }
              > >> >
              > >> > Now I want to delare an array of pointer look likes
              > >> > unsafe SEventQ [] *EvQ = new SEventQ[10];
              > >> >
              > >> >
              > >> > Thanks,
              > >> >
              > >> > Kathy Tran
              > >>
              > >>
              > >>[/color][/color]
              >
              >
              >[/color]

              Comment

              • Daniel Jin

                #8
                Re: Array of pointer in C#

                what you have is not an array, it's a linked list. and your function
                signature is wrong. My guess is that the resource handle should be an opaque
                pointer that's marshalled as IntPtr. And you'd have to marshal the
                TBufferHeader* as a IntPtr as well. In your structure definition, lpData
                should be an IntPtr to a unmanaged buffer, and lpNext should be a IntPtr to
                the next element in the linked list. problem is that you'd have to make a
                lot of manual calls to Marshal.AlocHGl obal and Marshal.Structu reToPtr to get
                your data set up back and forth.

                I'd suggest that you read and understand platform invoke fully before
                attempting.
                http://msdn.microsoft.com/library/de...lfunctions.asp

                "Kathy Tran" wrote:
                [color=blue]
                > Hi Nicholas Paldino ,
                > The original declaration is
                > typedef struct TBufferHeader_t ag
                > {
                > PK_PCHAR lpData;
                > PK_U32 dwBufferLength;
                > PK_U32 dwBytesRecorded ;
                > PK_U32 dwUser;
                > PK_U32 dwFlags;
                > PK_U32 dwTimeStamp;
                > struct TBufferHeader_t ag *lpNext;
                > PK_U32 reserved;
                > } TBufferHeader, *PBufferHeader;
                > The functon my program are going to call is
                > Syntax
                > PK_STATUS PK_AUDIO_InputA ddBuffer
                > (
                > IN TResourceHandle hPort, // TResourceHandle is an int
                > IN PBufferHeader pBuffer
                > );
                >
                > I am trying to use
                > [DllImport("Pika API.dll")]
                > unsafe public static extern int PK_AUDIO_InputA ddBuffer(uint
                > TResourceHandle , TBufferHeader *pBuffer);
                >
                > But I got errors when I compiled
                > Cannot take the address or size of a variable of a managed type
                > (TBufferHeader)
                >
                > Thanks for any help
                > Kathy Tran
                >
                > "Nicholas Paldino [.NET/C# MVP]" wrote:
                >[color=green]
                > > Kathy,
                > >
                > > What is the original declaration (in C/C++) of the method? The one that
                > > you have is not an array of pointers, but rather, just a pointer to the
                > > structure, which is the same as an array of structures (not an array of
                > > pointers to a structure).
                > >
                > >
                > > --
                > > - Nicholas Paldino [.NET/C# MVP]
                > > - mvp@spam.guard. caspershouse.co m
                > >
                > > "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
                > > news:E9E0B4C6-1DAB-446C-BA5D-55DAB4EFF3CB@mi crosoft.com...[color=darkred]
                > > > Hi Nicholas Paldino ,
                > > > Thanks for responding my question. I am performing some of interop which
                > > > requires an array of pointers to these structures to be taken. In my
                > > > program
                > > > I want to go through an array of pointer, with each element in array I
                > > > assign
                > > > some values in structure and then pass each element to un unmanaged code
                > > > function
                > > >
                > > > [DllImport("Pika API.dll")]
                > > > unsafe public static extern int PK_AUDIO_InputA ddBuffer(SEvent Q *pEvent);
                > > >
                > > > but I am very confusing and do not how to declare an array of pointer.
                > > >
                > > > Thank,
                > > > Kathy Tran
                > > >
                > > > "Nicholas Paldino [.NET/C# MVP]" wrote:
                > > >
                > > >> Kathy,
                > > >>
                > > >> Why do you want an array of pointers. Are you performing some sort
                > > >> of
                > > >> interop which requires an array of pointers to these structures to be
                > > >> taken,
                > > >> or do you need an array of structure?
                > > >>
                > > >> If you wanted an array of pointers, I believe you would do:
                > > >>
                > > >> SEventQ **EvQ;
                > > >>
                > > >> However, you couldn't say "new SEventQ*", since you can't "create" a
                > > >> pointer in that manner.
                > > >>
                > > >> What is it that you are trying to do?
                > > >>
                > > >>
                > > >> --
                > > >> - Nicholas Paldino [.NET/C# MVP]
                > > >> - mvp@spam.guard. caspershouse.co m
                > > >>
                > > >>
                > > >>
                > > >> "Kathy Tran" <Kathy Tran@discussion s.microsoft.com> wrote in message
                > > >> news:C4061BD4-39EE-4DDA-A28A-BE68F0AAC0DF@mi crosoft.com...
                > > >> > Hi,
                > > >> > Could you please help me how to declare an araay of pointer in C#.
                > > >> > In my program I declared an structure
                > > >> >
                > > >> > public struct SEventQ
                > > >> > {
                > > >> > public uint uiUserData;
                > > >> > public uint uiEvent;
                > > >> > public uint uiParam0;
                > > >> > public uint uiParam1;
                > > >> > }
                > > >> >
                > > >> > Now I want to delare an array of pointer look likes
                > > >> > unsafe SEventQ [] *EvQ = new SEventQ[10];
                > > >> >
                > > >> >
                > > >> > Thanks,
                > > >> >
                > > >> > Kathy Tran
                > > >>
                > > >>
                > > >>[/color]
                > >
                > >
                > >[/color][/color]

                Comment

                Working...