472,981 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,981 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 2053
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: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.