473,480 Members | 1,996 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Marshal managed struct to native pointer

Given this native struct:

typedef struct vendor
{
char name[20];
} VENDOR

I want to make managed equivalent, so I did this:

public value struct Vendor
{
public: String^ Name;
}

The managed Vendor type needs to be marshaled to void* so that an instance
of it can be passed to a native function (in the BerkeleyDB API):

Vendor^ vendor = gcnew Vendor();
vendor->Name = "Ron";

IntPtr vendorptr = Marshal::AllocHGlobal(Marshal::SizeOf(Vendor));
Marshall::StructureToPtr( vendor, vendorptr, false );
mydb.put( NULL, &key, &vendorptr, 0 );
Marshal::FreeHGlobal(vendorptr);

The "mydb.put()" method writes data to a file in a format used by
BerkeleyDB.

Note that an example application using the C-style structure above allows me
to write and read data to that file.

When I try using my managed structure, I can write but the read fails. This
leads me to believe that I need to define my structure differently (?)

Any comments on that question and the code is appreciated.

Ron
Mar 11 '06 #1
2 3165
Just wanted to correct a portion of code I posted below:
Marshall::StructureToPtr( vendor, vendorptr, false );
mydb.put( NULL, &key, &vendorptr, 0 );
- corrected here
Marshal:StructureToPtr(vendor, vendorptr, false);

Dbt data( vendorptr.ToPointer(), sizeof(Vendor));
mydb.put(NULL, &key, &data, 0);

the signature for Dbt constructor, and Db.put() is:

Dbt( void* ptr, u_int32_t size );
Db::put( DbTxn* txn, Dbt* key, Dbt* data, u_int32_t flags);

Ron

"RYoung" <r@hotmail.com> wrote in message
news:eI**************@TK2MSFTNGP11.phx.gbl... Given this native struct:

typedef struct vendor
{
char name[20];
} VENDOR

I want to make managed equivalent, so I did this:

public value struct Vendor
{
public: String^ Name;
}

The managed Vendor type needs to be marshaled to void* so that an instance
of it can be passed to a native function (in the BerkeleyDB API):

Vendor^ vendor = gcnew Vendor();
vendor->Name = "Ron";

IntPtr vendorptr = Marshal::AllocHGlobal(Marshal::SizeOf(Vendor));
Marshall::StructureToPtr( vendor, vendorptr, false );
mydb.put( NULL, &key, &vendorptr, 0 );
Marshal::FreeHGlobal(vendorptr);

The "mydb.put()" method writes data to a file in a format used by
BerkeleyDB.

Note that an example application using the C-style structure above allows
me to write and read data to that file.

When I try using my managed structure, I can write but the read fails.
This leads me to believe that I need to define my structure differently
(?)

Any comments on that question and the code is appreciated.

Ron

Mar 11 '06 #2
"RYoung" <r@hotmail.com> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Just wanted to correct a portion of code I posted below:
Marshall::StructureToPtr( vendor, vendorptr, false );
mydb.put( NULL, &key, &vendorptr, 0 );


From MSDN2:
StructureToPtr copies the contents of structure to the pre-allocated block
of memory pointed to by the ptr parameter.

This means that whatever you do with the vendorptr is irrelevant from the
managed structure 's point of view.
a read function that changes your unmanaged block of memory does not change
your managed structure.
Did you check your unmanaged ptr to see if that changed?

Btw: Is there a reason your string is a struct member, instead of just a
string?

Check out the different articles in MSDN2 on string marshalling. using the
ref keyword will allow you to read back data.
there are a number of articles on codeproject too. one of them is this:
http://www.codeproject.com/netcf/compframe2.asp#strings

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"
Mar 12 '06 #3

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

Similar topics

0
1472
by: Ivan | last post by:
Hi All, I have a problem with marshaling complex structures (containing numbers, strings, arrays of another structures) to native C function in dll. I have already posted same question to...
0
1325
by: rbfish | last post by:
Hi, I have some code like the following. It trys to return a unmanaged pointer to a struct, which is accessed in Managed C++ directly. But I always get this error message: HEAP: Invalid Address...
5
4304
by: Daniel Brown | last post by:
I am coding a managed C# wrapper for an unmanaged C DLL and I am unable to marshal a structure that contains an array of structures. When executed, the following code throws an ArgumentException...
1
4185
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When...
2
2300
by: Martin Zenkel | last post by:
Dear VS Team, using the Beta 2 of VS 2005 I've encontered the following problem. Let's assume threre are three Dll's, one unmanaged and two managed. In the unmanaged we put a simple unmanged...
0
3217
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
0
1721
by: jim4u | last post by:
I am porting a vb library to vb.net. The vb library has an external call to an unmanaged dll. Existing code: //External function declaration Private Declare Function Uncompress& Lib...
1
1290
by: Goran | last post by:
Hi all! I need to pass managed String from to C-style APIs. I see I can use Marshal::StringToXXX functions. Is this the best we have? I understand this will allocate a new string and create copy...
7
6456
by: repekto.argosoft | last post by:
Hi, I'm working with Marshalling. I got an idea to pass an object to the Native side as a byte array i.e. as void* in terms of C. For example: I have a .NET structure Point that contains...
0
7041
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
7043
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,...
0
7081
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...
0
6921
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...
0
5336
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,...
0
4481
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...
0
2995
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...
0
2984
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1300
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 ...

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.