[C#] PtrToStructure problem

  • Thread starter Mateusz [PEYN] Adamus
  • Start date
M

Mateusz [PEYN] Adamus

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
 
R

Richard Bos

Mateusz [PEYN] Adamus said:
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
 
C

CBFalconer

Mateusz [PEYN] Adamus said:
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.
 
J

Jens Marder

How about :

Newsserver: msnews.microsoft.com

group : microsoft.public.dotnet.languages.csharp

Other groups in different languages exist there, too
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top