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

marshaling an unmanaged struct

I have several unmanaged struct types that I've created equivalent managed
struct types for. How do I marshal the managed structs into their unmanaged
versions and vice versa?

Here's a quick example of what I'm working with:

unmanaged C++:

typedef struct _UStruct
{
UCHAR length;
USHORT type;
}UStruct;

and the managed version:

[StructLayout(LayoutKind::Sequential, Pack=1)]
__value public struct _M_UStruct
{
unsigned char length;
unsigned short type;
};

Lee
Nov 17 '05 #1
1 1312
Alright, after some work, I've found something that, at the very least,
compiles. Can anyone tell me if this is going to work?

_M_UStruct manStruct = some init value;
System::Object* obj = __box(manStruct);

IntPtr pnt = Marshal::AllocHGlobal(Marshal::SizeOf(obj));

Marshal::StructureToPtr(obj, pnt, true);

functionCall(static_cast<UStruct*>(const_cast<void *>(static_cast<const
void*>(pnt))));

Marshal::FreeHGlobal(pnt);

God, that is so gross looking.

"Lee Crabtree" <lc*******@goisi.com> wrote in message
news:ea*************@TK2MSFTNGP10.phx.gbl...
I have several unmanaged struct types that I've created equivalent managed
struct types for. How do I marshal the managed structs into their
unmanaged versions and vice versa?

Here's a quick example of what I'm working with:

unmanaged C++:

typedef struct _UStruct
{
UCHAR length;
USHORT type;
}UStruct;

and the managed version:

[StructLayout(LayoutKind::Sequential, Pack=1)]
__value public struct _M_UStruct
{
unsigned char length;
unsigned short type;
};

Lee

Nov 17 '05 #2

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

Similar topics

3
by: Nikolay Petrov | last post by:
Guys, please help. I am trying to make this work from at least 4 months. I am new to programming and some things are difficult to me, but I really need to make my project work. I can't find...
5
by: VM | last post by:
What's marshalling? I've had to use it extensively for a project but I don't know what it means. I tried to look for a definition in the Internet but I couldn't find anything that would explain...
3
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
1
by: Nadav | last post by:
Hi I am about to write a performance crutial system, I am considering writing this system based on native COM or unmanaged C++ exposed as CLI, Now, I Wonder... does exposing a native code through...
2
by: Pas de Spam | last post by:
Hi, I have some problem in marshaling array of structure. I have to exchange a structure of data with a C++ program. This structure data contains a field which is an array of a custom type. I...
3
by: Sin | last post by:
Hello everybody, I'm currently trying to understand how marshaling can use used for accessing Win32 API functions as well as custom C/C++ code we design which exposes functions the same way as...
6
by: Aston Martin | last post by:
Hi All, ********************** My Situation ********************** I am working on project that involves passing a structure to unmanaged code from .Net world (well using C#). Perhaps an example...
0
by: tomwolfstein | last post by:
Hi. I am trying to write a wrapper for the standard VC1 decoder, and I need to resolve a "TypeLoadException" The decoder comes an an executable which I've turned into a .dll. This decoder has about...
0
by: swts | last post by:
hi, the following marshaling code gives me an errror of "type packet cannot be marshaled as an unmanaged structure; no meaningful size or offset can be managed". public static byte...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.