473,385 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

[C#] PtrToStructure problem

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
Nov 14 '05 #1
3 3681
"Mateusz [PEYN] Adamus" <pe************@tlen.pl> 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
Nov 14 '05 #2
"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 (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
How about :

Newsserver: msnews.microsoft.com

group : microsoft.public.dotnet.languages.csharp

Other groups in different languages exist there, too
"Mateusz [PEYN] Adamus" <pe************@tlen.pl> schrieb im Newsbeitrag
news:d0**********@nemesis.news.tpi.pl...
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

Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: alfacom | last post by:
Hi, I have two C++ structures like these : typedef struct answer_series_item { int32_t contract_size_i; int32_t price_quot_factor_i; char ins_id_s ; char isin_code_s ; uint8_t suspended_c;
1
by: Ken Allen | last post by:
The documentation is not clear on the exact behaviour of the Marshal.PtrToStructure method and whether it copies the contents of the IntPtr region to a new managed object or whether it creates the...
2
by: Andre | last post by:
Hi, I have a question about Marshal.PtrToStructure method. I have a function Func() in unmanaged C++ which returns a pointer to a structure Str which is held on the unmanged site: Str* a =...
2
by: John | last post by:
public const int FRAME_LENGTH=144; public class FRAME_T { public byte Block = new byte; } public void MyDataHandler(IntPtr appContext, IntPtr data) FRAME_T frame = new FRAME_T();...
3
by: Mateusz [PEYN] Adamus | last post by:
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...
4
by: C Learner | last post by:
Hi, I have an application which is using a dll written in C++. When the program go to the function below, it would raise an NullReferenceException at the line of PtrToStructure. public bool...
1
by: Jay | last post by:
Hi, In my application, C++ dll is posting some message,which is processed by a form in C# ,where I use Message.Lparam to convert it in structure using Marshal.PtrToStructure() mehtod ,but it...
1
by: spamacon | last post by:
Hello, I have a strange situation using .Net FW 1.1. I want to use Marshal.PtrToStructure to fill the structure below. The first 3 fields get filled correctly: ulStruct describes how big the...
6
by: carles | last post by:
Hi, Here, sample code where a byte array is used to fill a particular structure: fs = File.OpenRead(path); // FileStream BITMAPFILEHEADER bfh = new BITMAPFILEHEADER(); b = new byte;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.