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

[c++/CLI] What does EXACTLY happen when I compile a native classwith CLR ?

Hello,

I am currently trying to wrap my head around what actually happens when
I compile a normal (native) c++ class with the /CLR Flag in Visual C++
2005 (C++/CLI).

Suppose I have the following class deklared in c++:

// #pragma managed or #pragma unmanaged
// does not seem to make any differnce here
class MyNativeClass
{
public:
int iValue;
public:
void DoSomething(double param)
{
// do something here
}
virtual ~MyNativeClass();
};

If compile that with /CLR we get the follow typedenfintion in the
Assembly (IL)

..class private sequential ansi sealed beforefieldinit MyNativeClass
extends [mscorlib]System.ValueType
{
.custom instance void
[mscorlib]System.Runtime.CompilerServices.NativeCppClassAttr ibute::.ctor()
.custom instance void
[Microsoft.VisualC]Microsoft.VisualC.MiscellaneousBitsAttribute::.cto r(int32)
= ( int32(0x00000040) )
.custom instance void
[Microsoft.VisualC]Microsoft.VisualC.DebugInfoInPDBAttribute::.ctor()
}

Now where did the Members (iValue and DoSomething) of that Type go?
Not only that they vanished, none of the Attributes listed here hints
where the CLR has to look for the Method implemenetation or for iVlaue.
Are the members compiled to IL too? If so where do I find them? Or are
they compiled to machinecode? That can't be, sinc it is possible to
compile that class with /CLR:pure too and as far as I understand
/CLR:pure prohibits machinecode. How does the CLR know where to look for
the members?
Feb 9 '06 #1
2 2069
Here is an example of what IL is actually generated when I call acess
those members. Now is anyone able to explain what actually happens here?

[c++/CLI]
int main(array<System::String ^> ^args)
{
MyNativeClass* nc = new MyNativeClass();
nc->DoSomething(0xbadf00d);
nc->iValue=0xfade;
}

[disassembled to C#]
internal static unsafe int main(string[] args)
{
MyNativeClass* classPtr2 = @new(8);
MyNativeClass* classPtr1 = (classPtr2 == null) ? null :
((MyNativeClass*) MyNativeClass.{ctor}((MyNativeClass* modopt(IsConst)
modopt(IsConst)) classPtr2));
MyNativeClass.DoSomething((MyNativeClass* modopt(IsConst)
modopt(IsConst)) classPtr1, 69);
*(((int*) (classPtr1 + 4))) = 0x60;
return 0;
}

[disassembled to IL]
..method assembly static int32 main(string[] args) cil managed
{
// Code Size: 43 byte(s)
.maxstack 2
.locals (
MyNativeClass* classPtr1,
MyNativeClass* classPtr2)
L_0000: ldc.i4.8
L_0001: call void*
modopt([mscorlib]System.Runtime.CompilerServices.CallConvCdecl)
<Module>::new(unsigned int32)
L_0006: stloc.1
L_0007: ldloc.1
L_0008: brfalse.s L_0012
L_000a: ldloc.1
L_000b: call MyNativeClass*
modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)
<Module>::MyNativeClass.{ctor}(MyNativeClass*
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst))
L_0010: br.s L_0013
L_0012: ldc.i4.0
L_0013: stloc.0
L_0014: ldloc.0
L_0015: ldc.r8 69
L_001e: call void
modopt([mscorlib]System.Runtime.CompilerServices.CallConvThiscall)
<Module>::MyNativeClass.DoSomething(MyNativeClas s*
modopt([mscorlib]System.Runtime.CompilerServices.IsConst)
modopt([mscorlib]System.Runtime.CompilerServices.IsConst), float64)
L_0023: ldloc.0
L_0024: ldc.i4.4
L_0025: add
L_0026: ldc.i4.s 96
L_0028: stind.i4
L_0029: ldc.i4.0
L_002a: ret
}
bonk schrieb:
Hello,

I am currently trying to wrap my head around what actually happens when
I compile a normal (native) c++ class with the /CLR Flag in Visual C++
2005 (C++/CLI).

Suppose I have the following class deklared in c++:

// #pragma managed or #pragma unmanaged
// does not seem to make any differnce here
class MyNativeClass
{
public:
int iValue;
public:
void DoSomething(double param)
{
// do something here
}
virtual ~MyNativeClass();
};

If compile that with /CLR we get the follow typedenfintion in the
Assembly (IL)

.class private sequential ansi sealed beforefieldinit MyNativeClass
extends [mscorlib]System.ValueType
{
.custom instance void
[mscorlib]System.Runtime.CompilerServices.NativeCppClassAttr ibute::.ctor()
.custom instance void
[Microsoft.VisualC]Microsoft.VisualC.MiscellaneousBitsAttribute::.cto r(int32)
= ( int32(0x00000040) )
.custom instance void
[Microsoft.VisualC]Microsoft.VisualC.DebugInfoInPDBAttribute::.ctor()
}

Now where did the Members (iValue and DoSomething) of that Type go?
Not only that they vanished, none of the Attributes listed here hints
where the CLR has to look for the Method implemenetation or for iVlaue.
Are the members compiled to IL too? If so where do I find them? Or are
they compiled to machinecode? That can't be, sinc it is possible to
compile that class with /CLR:pure too and as far as I understand
/CLR:pure prohibits machinecode. How does the CLR know where to look for
the members?

Feb 9 '06 #2
bonk wrote:
Hello,

I am currently trying to wrap my head around what actually happens
when I compile a normal (native) c++ class with the /CLR Flag in Visual
C++
2005 (C++/CLI).
[ snipped example code ]

Now where did the Members (iValue and DoSomething) of that Type go?
Not only that they vanished, none of the Attributes listed here hints
where the CLR has to look for the Method implemenetation or for
iVlaue. Are the members compiled to IL too? If so where do I find them? Or
are
they compiled to machinecode? That can't be, sinc it is possible to
compile that class with /CLR:pure too and as far as I understand
/CLR:pure prohibits machinecode. How does the CLR know where to look
for the members?


The CLR knows absolutely nothing about the class beyond how much memory it
occupies. All of the member functions are compiled to IL that uses
burned-in offsets to access the fields of the class. Such classes are
accessible only from C++ since they're lacking CLR metadata to describe the
fields, properties and methods of the class. In this mode, you can think of
the CLR as simply being a different CPU.

-cd
Feb 9 '06 #3

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

Similar topics

3
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
81
by: Don Kim | last post by:
I've been looking for a new IT position, and so far, the majority of work with respect to the Windows platform is C#/.Net, with some vb.net requests every so often. Even many of the C++/MFC/ATL...
12
by: Edward Diener | last post by:
Given value class X { public: // Not allowed: X():i(100000),s(10000) { } // Allowed void InitializeDefaults() { i = 100000; s = 10000; } private: int i;
7
by: Adrian | last post by:
Hi, I have a large unmanaged static C++ library which I've wrapped using a small C++/CLR DLL. This is called from a C# client application. The static library has a singleton, however it appears...
9
by: Bern McCarty | last post by:
I am porting stuff from MEC++ syntax to the new C++/CLI syntax. Something that we did in the old syntax that proved to be very valuable was to make sure that the finalizer would purposefully...
62
by: Born | last post by:
GC is really garbage itself Reason 1: There is delay between the wanted destruction and the actual destruction.
1
by: Noah Roberts | last post by:
Trying to use boost::function in a C++/CLI program. Here is code: pragma once #include <boost/function.hpp> #include <boost/shared_ptr.hpp> #include <vector> using namespace System;
7
by: Sumedh | last post by:
Hi everyone There is a C# project which calls C++/CLI dll to be able to call native C++ including templates. But the C++/CLI code itself also requires the C# dll to get the types. For example: ...
4
by: joes.staal | last post by:
Hi, I know this has been asked earlier on, however, none of the other threads where I looked solved the following problem. 1. I've got a native C++ library (lib, not a dll) with a singleton. 2....
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.