473,385 Members | 2,004 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,385 software developers and data experts.

Mixed mode assembly wrapper

Hi,

I am making a wrapper class so I can call unmanaged classes from C#, I
have these structs that I want to pass and get back from the unmanaged
classes, do I...
1) Create a copy of these structs for the managed world like
public __value struct SomeStruct
{
System::UInt32 SomeVal;
System::UInt32 SomeVal2;
System::UInt32 SomeVal3;
}

and use these from C# and then pass this to the method wrapper, that is
calling the unmanaged method like...
someReferenceToObject->someMethod(..., SomeStruct);

or 2)
do I make it like so..

public __gc class SomeThing
{
private: SOMEUNMANAGEDTHING* someUnmanagedThing;

public: SomeThing()
{
someUnamangedThing = new SOMEUNMANAGEDTHING();
}

public: ~SomeThing()
{
delete someUnmanagedThing;
}

// private so only the genereated prop is visible
private: __property System::UInt32 get_SomeVal()
{
return someUnmanagedThing->SomeVal;
}

// private so only the genereated prop is visible
private: __property void get_SomeVal(System::UInt32 value)
{
someUnmanagedThing->SomeVal = value;
}
}

and passt his to the unmanaged method like previously?

Whats the best way to make unmanaged structs visible to the managed world?

Thanks
Nov 17 '05 #1
3 5098
Im still learning this mix mode stuff..
If I make a duplicate of the struct, can I somehow pass this into the
method without doing.

System::UInt32 someMethod(SomeStruct s)
{
SOMEUNMANAGEDSTRUCT* something = new SOMEUNMANAGEDSTRUCT;

something->someVal = s.SomeVal;
something->someVal2 = s.SomeVal2;
something->someVal3 = s.SomeVal3;

return someObj->someMethod(something);
}

<.> wrote in message news:%2******************@TK2MSFTNGP10.phx.gbl...
Hi,

I am making a wrapper class so I can call unmanaged classes from C#, I
have these structs that I want to pass and get back from the unmanaged
classes, do I...
1) Create a copy of these structs for the managed world like
public __value struct SomeStruct
{
System::UInt32 SomeVal;
System::UInt32 SomeVal2;
System::UInt32 SomeVal3;
}

and use these from C# and then pass this to the method wrapper, that is calling the unmanaged method like...
someReferenceToObject->someMethod(..., SomeStruct);

or 2)
do I make it like so..

public __gc class SomeThing
{
private: SOMEUNMANAGEDTHING* someUnmanagedThing;

public: SomeThing()
{
someUnamangedThing = new SOMEUNMANAGEDTHING();
}

public: ~SomeThing()
{
delete someUnmanagedThing;
}

// private so only the genereated prop is visible
private: __property System::UInt32 get_SomeVal()
{
return someUnmanagedThing->SomeVal;
}

// private so only the genereated prop is visible
private: __property void get_SomeVal(System::UInt32 value)
{
someUnmanagedThing->SomeVal = value;
}
}

and passt his to the unmanaged method like previously?

Whats the best way to make unmanaged structs visible to the managed world?

Thanks

Nov 17 '05 #2
Hello,

If I understand the question correctly, a Managed C++ (MC++) wrapper class
is being written to expose unmanaged structs in an existing C++ class to
C#. Please let me know if this is not correct.

Either of the two methods that have been proposed will work, but each has
its own caveats.

Whenever a managed type is created using either __gc or __value keywords,
the CLR makes determinations about the layout and management of memory as
necessary. Because of this, a couple of things are needed to ensure that
interop will work as expected.

For number 1, we must use the following to specify that they type must have
its data members laid out in the given order:

using namespace System::Runtime::InteropServices;

[StructLayoutAttribute(LayoutKind::Sequential)]
public __value struct SomeStruct
{
System::UInt32 SomeVal;
System::UInt32 SomeVal2;
System::UInt32 SomeVal3;
}
If you want to go with number 2, then when the SomeThing object is
allocated with new it is placed on the GC heap and then the GC can move the
object around whenever and wherever it wants. To prevent this, use the
__pin keyword to pin the pointer before making calls to unmanaged code.
Thanks!
Niel Sutton
Microsoft C++/C# Developer Support
This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #3
If you want to go with number 2, then when the SomeThing object is
allocated with new it is placed on the GC heap and then the GC can move the object around whenever and wherever it wants. To prevent this, use the
__pin keyword to pin the pointer before making calls to unmanaged code.


If the unmanaged object is being called from managed code - why does the
managed object being moved around matter? (If the unmanaged code was calling
into the managed code, I'd understand),

thanks

JB
Nov 17 '05 #4

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

Similar topics

1
by: Steve Terepin | last post by:
I've found some rather worrying articles (Mixed Mode Library Assembly bug, Richard Grimes, Windows Developer Network Sept 2003 ; and Knowledge Base Article 814472 ) that point out the need to use...
9
by: Edward Diener | last post by:
I received no answers about this the first time I posted, so I will try again. My inability to decipher an MSDN topic may find others who have the same inability and someone who can decipher and...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
1
by: | last post by:
Hi, I am making a wrapper class so I can call unmanaged classes from C#, I have these structs that I want to pass and get back from the unmanaged classes, do I... 1) Create a copy of these...
8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
7
by: Staale L. Hansen | last post by:
We have an application which uses some mixed-mode code to expose a .NET 1.1 managed API. Only the necessary files are compiled with /clr. We want to be able to load the application without .NET...
7
by: Kevin Frey | last post by:
Using .NET 1.1. We have a mixed-mode assembly written in Managed C++ that we are using from an ASP.NET application that has been coded using C#. The mixed-mode assembly has its own...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
by: Russ Barrett | last post by:
Hello, I'm new to ASP.Net and I'm trying to interface an ASP.Net (C#) application to functionality resident in a C++ static lib. From posts I've seen around I understand this requires creation of a...
5
by: DFB | last post by:
I am the author of the ZLibNetWrapper project on SourceForge (located at zlibnetwrapper.sf.net). This project is a simple mixed-mode .NET wrapper around the ZLib compression library. The ZLib...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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,...

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.