473,508 Members | 4,779 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Conversion between managed class and unmanaged structure


I want to convert a object of a managed class to a unmanaged structure
that has the same member with that managed class. Can anybody tell me
how i can do it?
Thanks in advance.

--
zhphust
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 17 '05 #1
3 3484
As long as all of the structure parameters are value types you can just cast
the a pointer of managed structure to a pointer of the same unmanaged
structure, e.g.:

void GetUnmanagedLayout(IN ManagedStruct *pms, OUT UnmanagedStruct &ums)
{
ManagedStruct __pin* pPtr = pms;

memcpy(&ums, (PBYTE)pPtr, sizeof(ums));
OR
ums = *(UnmanagedStruct*)pPtr;
}
make sure to verify the compiler byte alignment ( #pragma pack... )

However, when the structure contain properties other then value types ( such
as arrays ) the memory layout of the managed struct will be different from
the layout of the same unmanaged struct ( as managed arrays are resembled by
'classes' rather then direct heap/stack pointers ) so direct conversion could
not be done, rather the struct to be accessed through unmanaged code could be
exposed through COMInterop ( which will impose a development time increase ).

Hope this helps...

Nadav
http://www.ddevel.com

"zhphust" wrote:

I want to convert a object of a managed class to a unmanaged structure
that has the same member with that managed class. Can anybody tell me
how i can do it?
Thanks in advance.

--
zhphust
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 17 '05 #2
Thanks for answering my question!
I think the scene that you have described is a little defferent from my
original needs.

The source type that I want to convert is a mananged gc object such as
__gc class User
{
public:
int user_id;
String *user_name;
};

Then, the target type that I want to covert to is a umanaged structure such as
struct User
{
int user_id;
char *user_name;
};

I do can use P/Invoke and unmanaged dll to realize the above scene in my
project, but I think if I can do this conversion with mixing managed code and
unmanaged code in VC++.NET instead of using P/Invoke, it will be great help
to shorten the development time.

"Nadav" wrote:
As long as all of the structure parameters are value types you can just cast
the a pointer of managed structure to a pointer of the same unmanaged
structure, e.g.:

void GetUnmanagedLayout(IN ManagedStruct *pms, OUT UnmanagedStruct &ums)
{
ManagedStruct __pin* pPtr = pms;

memcpy(&ums, (PBYTE)pPtr, sizeof(ums));
OR
ums = *(UnmanagedStruct*)pPtr;
}
make sure to verify the compiler byte alignment ( #pragma pack... )

However, when the structure contain properties other then value types ( such
as arrays ) the memory layout of the managed struct will be different from
the layout of the same unmanaged struct ( as managed arrays are resembled by
'classes' rather then direct heap/stack pointers ) so direct conversion could
not be done, rather the struct to be accessed through unmanaged code could be
exposed through COMInterop ( which will impose a development time increase ).

Hope this helps...

Nadav
http://www.ddevel.com

"zhphust" wrote:

I want to convert a object of a managed class to a unmanaged structure
that has the same member with that managed class. Can anybody tell me
how i can do it?
Thanks in advance.

--
zhphust
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 17 '05 #3
Well, there are several problems I see here:
1. The managed class include a String* variable which is not a value type,
hence, simple unmanaged casting is not appropriate.
2. The managed class contains a String* variable while the unmanaged strict
contain char* variable, it is not possible to directly reference the String*
variable by the char* variable as managed strings are UNICODE e.g. 2 bytes
aligned in contrast to char* which is 1 bye aligned, converting a String* to
char*in this manner will require a W2A conversion AND memory copying which
may effect the performance of the application.

Resolution:
1. .NET doesn't provide direct memory access in the same manner C++ does (
this what makes it managed ) this is why managed strings are defined as
classes and not as direct memory pointers, so as long as there are reference
types in your class there is nothing realy you can do to have a direct
unmanaged access to your class.
2. using BSTR or USHORT* rather then char* in your unmanaged strict may
resolve the memory duplication issue ( later explained in details ).

Conclusion:
Direct reference of a managed class containing reference types to an
unmanaged pointer is not possible, rather I would suggest to replace the
char* of the unmanaged struct with USHORT* and to use the 'PtrToStringChars'
API to get direct string memory access ( this will resolve the memory copying
), the following snippet demonstrate what was just said:
#using <mscorlib.dll>

#include <vcclr.h>

using namespace System;

#pragma unmanaged

struct User
{
int user_id;
const unsigned short* user_name;
};

void PrintUser(User &u) {
wprintf(L"id: %i\nname: %s\n", u.user_id, u.user_name);
}

#pragma managed

__gc class CTest
{
protected:
String *user_name;
public:
System::Int32 user_id;
__property String* get_UserName() { return user_name; }
__property void set_UserName(String* value) { user_name = value; }
__property const System::Char* get_UserRawName()
{ return PtrToStringChars(user_name); }
};

int _tmain() {
CTest *pTest = new CTest();
const unsigned short __pin *pustmp = pTest->get_UserRawName();
User u = { pTest->user_id, pustmp };
PrintUser(u);
return 0;
}

Still this solution impose some limitations: the usage of 'pustmp' is
limited to the scope of it's stack frame, out of this stack frame it is not
Guaranteed that the value of 'pustmp' will be valid as the GC may move it, to
overcome this limitation the string buffer should be copied... Another thing
to keep in mind while pinning variables is that variables should not be
pinned for long periods of time, pinning variables for long periods of time
prevents the GC from moving managed blocks of memory which may cause a
Derogation of the GC and the application performance...

Hope this helps.
Nadav.

"DotNetFan" wrote:
Thanks for answering my question!
I think the scene that you have described is a little defferent from my
original needs.

The source type that I want to convert is a mananged gc object such as
__gc class User
{
public:
int user_id;
String *user_name;
};

Then, the target type that I want to covert to is a umanaged structure such as
struct User
{
int user_id;
char *user_name;
};

I do can use P/Invoke and unmanaged dll to realize the above scene in my
project, but I think if I can do this conversion with mixing managed code and
unmanaged code in VC++.NET instead of using P/Invoke, it will be great help
to shorten the development time.

"Nadav" wrote:
As long as all of the structure parameters are value types you can just cast
the a pointer of managed structure to a pointer of the same unmanaged
structure, e.g.:

void GetUnmanagedLayout(IN ManagedStruct *pms, OUT UnmanagedStruct &ums)
{
ManagedStruct __pin* pPtr = pms;

memcpy(&ums, (PBYTE)pPtr, sizeof(ums));
OR
ums = *(UnmanagedStruct*)pPtr;
}
make sure to verify the compiler byte alignment ( #pragma pack... )

However, when the structure contain properties other then value types ( such
as arrays ) the memory layout of the managed struct will be different from
the layout of the same unmanaged struct ( as managed arrays are resembled by
'classes' rather then direct heap/stack pointers ) so direct conversion could
not be done, rather the struct to be accessed through unmanaged code could be
exposed through COMInterop ( which will impose a development time increase ).

Hope this helps...

Nadav
http://www.ddevel.com

"zhphust" wrote:

I want to convert a object of a managed class to a unmanaged structure
that has the same member with that managed class. Can anybody tell me
how i can do it?
Thanks in advance.

--
zhphust
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------

Nov 17 '05 #4

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

Similar topics

1
741
by: Bob Rock | last post by:
Hello, in the last few days I've made my first few attempts at creating mixed C++ managed-unmanaged assemblies and looking aftwerwards with ILDASM at what is visible in those assemblies from a...
5
3323
by: john | last post by:
Here is the short story of what i'm trying to do. I have a 4 sided case labeling printer setting out on one of our production lines. Now then i have a vb.net application that sends data to this...
6
2535
by: Omid Hodjati | last post by:
Hi All, I implemented an encryption algorithm using C#, native C++ and managed C++. Then I measured the CPU time used for executing this algorithm in all implementation. The managed version of...
0
930
by: charlee strong | last post by:
I'm seeing some strange behaviour: i have a managed wrapper class that wraps around unmanaged object, i want to convert back and forth between managed and unmanaged object, so i have overloaded...
3
1782
by: WithPit | last post by:
I am trying to create an managed wrapper but have some problems with it by using abstract classes. In my unmanaged library code i had the following three classes with the following hierarchy ...
6
5883
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...
3
2172
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm...
3
4681
by: frank | last post by:
Hi I've got aplication, which one is written in unmanaged c++ with stl, i've made for it gui in managed c++. Problem becomes when I'm starting to filling up for example datagrids, when I'm...
16
8757
by: pkoniusz | last post by:
Hello everybody, Been just thinking how actually one could convert the following unmanaged code to the managed C++: struct JustAnExample { char value1; int value2; // etc ....
0
7228
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
7128
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
7332
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
7393
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
7502
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
5635
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
4715
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
3191
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1565
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.