473,387 Members | 1,619 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,387 software developers and data experts.

[Q] A PtrToStructure question

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:

[DllImport("UnmanagedFuncs.dll")] IntPtr UnmanagedFunc(); //managed
import of the function
I want to manipulate this structure in managed C# and I use:

IntPtr b = UnmanagedFunc();
Str c = (Str)Marshal.PtrToStructure(b,typeof(Str));
c.Value = 10;

Now my question is:
1) Is c a copy of the structure where b is pointing to?
2) When I manipulate c.Value is this also changed on the unmanaged
site?
3) When 1) is yes and 2) is No how can I change values in the memory
where b is pointing?

Thanks
André Betz
http://www.andrebetz.de
Nov 16 '05 #1
2 4329
Hi,

"Andre" <sk******@gmx.de> wrote in message
news:be**************************@posting.google.c om...
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:

[DllImport("UnmanagedFuncs.dll")] IntPtr UnmanagedFunc(); //managed
import of the function
I want to manipulate this structure in managed C# and I use:

IntPtr b = UnmanagedFunc();
Str c = (Str)Marshal.PtrToStructure(b,typeof(Str));
c.Value = 10;

Now my question is:
1) Is c a copy of the structure where b is pointing to? Yes.
2) When I manipulate c.Value is this also changed on the unmanaged
site? No.
3) When 1) is yes and 2) is No how can I change values in the memory
where b is pointing?
Call Marshal.StructureToPtr after you changed the structure.
(You can also use any of the Marshal.Write* functions to change unmanaged
memory.)
HTH,
greetings

Thanks
André Betz
http://www.andrebetz.de

Nov 16 '05 #2
See inline ****

Willy.
"Andre" <sk******@gmx.de> wrote in message
news:be**************************@posting.google.c om...
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:

[DllImport("UnmanagedFuncs.dll")] IntPtr UnmanagedFunc(); //managed
import of the function
I want to manipulate this structure in managed C# and I use:

IntPtr b = UnmanagedFunc();
Str c = (Str)Marshal.PtrToStructure(b,typeof(Str));
c.Value = 10;

Now my question is:
1) Is c a copy of the structure where b is pointing to? *** YES. 2) When I manipulate c.Value is this also changed on the unmanaged
site? *** NO.
3) When 1) is yes and 2) is No how can I change values in the memory
where b is pointing?

*** If you don't like the marshaling overhead, use the struct pointer
returned in an unsafe context like:

struct Str {
int i;
....
}

unsafe {
Str* a = Func();
a->i = 123;
...
}


Nov 16 '05 #3

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

Similar topics

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...
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: 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: Tyron | last post by:
I want to get the Position of the Mouse when the User click in the non-client area of a Window. WM_NCLBUTTONDOWN seems to be the right Message for this - and the LParam contains a POINTS struct of...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.