[C#] PtrToStructure problem

Discussion in 'C Programming' started by Mateusz [PEYN] Adamus, Mar 11, 2005.

  1. Hi

    First of all please let me know if there is some other, more suitable
    group for this news - TIA :)


    Now, my problem. I'm writing an C# application in which I'm using some
    functions from DLL. This dll has been written in C.

    One of these functions returns as a result a structure, in example:

    [StructLayout(LayoutKind.Sequential, Pack=2)]
    internal class myStruct
    {
    public short someNumber;
    public IntPtr handle;
    }


    OK. Now in this handle there is a pointer to a structure with actuall
    results.

    [StructLayout(LayoutKind.Sequential, Pack=2)]
    internal class myStruct2
    {
    public uint count; //number of values return from function
    public uint current; //current value
    [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)]
    public short[] items; //array of results
    }

    OK. Hope you're still with me :D

    Now, as you can see there is another array in myStruct2. This array
    contains result which I want to get :D

    In this function description I have something like this:
    <description>
    The items value is simply a placeholder for the start of the actual
    array, which must be allocated when the container is allocated.
    </description>


    In my application I have something like this:

    <code>
    [DllImport("some.dll", EntryPoint="#1")]
    private static extern short getValues([In, Out] myStruct cap);

    myStruct ms = new myStruct();
    getValues(ms);

    myStruct2 ms2 = new myStruct2();
    ms2 = (myStruct2)Marshal.PtrToStructure(ms.handle, typeof(myStruct2));
    </code>

    In return I get some really strange values in ms2.count and ms2.current.
    Not mentioning that I can't get the values from the items :-(

    What am I doing wrong? Meybe there is something missing?

    best regards
    Mateusz [PEYN] Adamus
    Mateusz [PEYN] Adamus, Mar 11, 2005
    #1
    1. Advertising

  2. Mateusz [PEYN] Adamus

    Richard Bos Guest

    "Mateusz [PEYN] Adamus" <> wrote:

    > First of all please let me know if there is some other, more suitable
    > group for this news - TIA :)


    There is undoubtedly a more suitable group somewhere in microsoft.* for
    C-flat. Despite the dishonestly chosen name, that language has not that
    much to do with C, except superficially.

    > Now, my problem. I'm writing an C# application in which I'm using some
    > functions from DLL. This dll has been written in C.


    That's no excuse.

    > One of these functions returns as a result a structure, in example:
    >
    > [StructLayout(LayoutKind.Sequential, Pack=2)]
    > internal class myStruct
    > {
    > public short someNumber;
    > public IntPtr handle;
    > }


    No function written in C can return a structure like that, because it's
    not C.
    If you wish to know how a specifically laid-out structure can be read in
    Cb, you'll need to ask in a Cb newsgroup. This one only deals with real
    C, not with counterfeit Java.
    If, OTOH, you wish to know how a specific _C_ struct is laid out in
    memory, then you'll need to ask in a newsgroup for the specific compiler
    that was used to compile the program, because the C Standard doesn't
    make any demands beyond that items must appear in order. Sizes,
    alignments, padding bytes and endianness are all left to the discretion
    of the implementation.

    Richard
    Richard Bos, Mar 11, 2005
    #2
    1. Advertising

  3. Mateusz [PEYN] Adamus

    CBFalconer Guest

    "Mateusz [PEYN] Adamus" wrote:
    >
    > First of all please let me know if there is some other, more
    > suitable group for this news - TIA :)
    >
    > Now, my problem. I'm writing an C# application in which I'm using
    > some functions from DLL. This dll has been written in C.


    ..... snip unread ....

    There certainly is something more suitable. Some newsgroup with
    microsoft or windows in its name. Neither C# nor DLLs are
    mentioned in the ISO C standard, so they are totally unknown here.
    This group deals only with the portable C language, which will
    operate unchanged on multiple systems, and is defined by the C
    standard.

    Your question could have been on-topic even though it mentioned
    DLLs, depending on how it was couched. But C# <> C by any stretch
    of the imagination.

    --
    Chuck F () ()
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net> USE worldnet address!
    CBFalconer, Mar 11, 2005
    #3
  4. Mateusz [PEYN] Adamus

    Jens Marder Guest

    How about :

    Newsserver: msnews.microsoft.com

    group : microsoft.public.dotnet.languages.csharp

    Other groups in different languages exist there, too


    "Mateusz [PEYN] Adamus" <> schrieb im Newsbeitrag
    news:d0s00j$klj$...
    > Hi
    >
    > First of all please let me know if there is some other, more suitable
    > group for this news - TIA :)
    >
    >
    > Now, my problem. I'm writing an C# application in which I'm using some
    > functions from DLL. This dll has been written in C.
    >
    > One of these functions returns as a result a structure, in example:
    >
    > [StructLayout(LayoutKind.Sequential, Pack=2)]
    > internal class myStruct
    > {
    > public short someNumber;
    > public IntPtr handle;
    > }
    >
    >
    > OK. Now in this handle there is a pointer to a structure with actuall
    > results.
    >
    > [StructLayout(LayoutKind.Sequential, Pack=2)]
    > internal class myStruct2
    > {
    > public uint count; //number of values return from function
    > public uint current; //current value
    > [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)]
    > public short[] items; //array of results
    > }
    >
    > OK. Hope you're still with me :D
    >
    > Now, as you can see there is another array in myStruct2. This array
    > contains result which I want to get :D
    >
    > In this function description I have something like this:
    > <description>
    > The items value is simply a placeholder for the start of the actual array,
    > which must be allocated when the container is allocated.
    > </description>
    >
    >
    > In my application I have something like this:
    >
    > <code>
    > [DllImport("some.dll", EntryPoint="#1")]
    > private static extern short getValues([In, Out] myStruct cap);
    >
    > myStruct ms = new myStruct();
    > getValues(ms);
    >
    > myStruct2 ms2 = new myStruct2();
    > ms2 = (myStruct2)Marshal.PtrToStructure(ms.handle, typeof(myStruct2));
    > </code>
    >
    > In return I get some really strange values in ms2.count and ms2.current.
    > Not mentioning that I can't get the values from the items :-(
    >
    > What am I doing wrong? Meybe there is something missing?
    >
    > best regards
    > Mateusz [PEYN] Adamus
    Jens Marder, Mar 11, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andreas Suurkuusk
    Replies:
    0
    Views:
    3,949
    Andreas Suurkuusk
    Jul 27, 2003
  2. Ted Miller
    Replies:
    0
    Views:
    5,135
    Ted Miller
    Sep 13, 2003
  3. Merek
    Replies:
    0
    Views:
    1,942
    Merek
    Dec 3, 2003
  4. Scott Meddows
    Replies:
    1
    Views:
    379
    John Saunders
    Jun 8, 2004
  5. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    528
    Bullschmidt
    May 11, 2004
Loading...

Share This Page