473,770 Members | 3,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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(La youtKind.Sequen tial, 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(La youtKind.Sequen tial, Pack=2)]
internal class myStruct2
{
public uint count; //number of values return from function
public uint current; //current value
[MarshalAs(Unman agedType.ByValA rray, 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)Mars hal.PtrToStruct ure(ms.handle, typeof(myStruct 2));
</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 3709
"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(La youtKind.Sequen tial, 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********@yah oo.com) (cb********@wor ldnet.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.microsof t.com

group : microsoft.publi c.dotnet.langua ges.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(La youtKind.Sequen tial, 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(La youtKind.Sequen tial, Pack=2)]
internal class myStruct2
{
public uint count; //number of values return from function
public uint current; //current value
[MarshalAs(Unman agedType.ByValA rray, 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)Mars hal.PtrToStruct ure(ms.handle, typeof(myStruct 2));
</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
3975
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
3477
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 managed object to reference the same region of memory. Question 1: should I call Marshal.FreeCoTaskMem after calling Marshal.PtrToStructure? Question 2: the Marshal.PtrToStructure method seems to create an object for the structure -- should...
2
4346
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 = Func(); //unmanaged function On the managed site I wrote it like this:
2
3080
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(); Marshal.PtrToStructure(data, frame);
3
3217
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 DLL. This dll has been written in C. One of these functions returns as a result a structure, in example:
4
3554
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 ServerCallBack(IntPtr log) { DB_T acclog = (DB_T)Marshal.PtrToStructure(log, typeof(DB_T)); }
1
3799
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 throws ArgumentException. can anybody give sample code for Marshal.PtrToStructure() .
1
2750
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 structure is in bytes (120 bytes, assuming 32-bit IntPtrs), pWmSnapshot gets 0, and usNumWmSnapshots gets 0. The next field, pICView, is a pointer to another structure, and gets filled with some garbage location, which is actually the lower 2 bytes...
6
4879
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
9591
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10002
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9869
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8883
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7415
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6676
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5312
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3970
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3575
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.