473,785 Members | 2,557 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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(dou ble 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.ValueTyp e
{
.custom instance void
[mscorlib]System.Runtime. CompilerService s.NativeCppClas sAttribute::.ct or()
.custom instance void
[Microsoft.Visua lC]Microsoft.Visua lC.Miscellaneou sBitsAttribute: :.ctor(int32)
= ( int32(0x0000004 0) )
.custom instance void
[Microsoft.Visua lC]Microsoft.Visua lC.DebugInfoInP DBAttribute::.c tor()
}

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 2104
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<Syst em::String ^> ^args)
{
MyNativeClass* nc = new MyNativeClass() ;
nc->DoSomething(0x badf00d);
nc->iValue=0xfad e;
}

[disassembled to C#]
internal static unsafe int main(string[] args)
{
MyNativeClass* classPtr2 = @new(8);
MyNativeClass* classPtr1 = (classPtr2 == null) ? null :
((MyNativeClass *) MyNativeClass.{ ctor}((MyNative Class* modopt(IsConst)
modopt(IsConst) ) classPtr2));
MyNativeClass.D oSomething((MyN ativeClass* 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. CompilerService s.CallConvCdecl )
<Module>::new(u nsigned 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. CompilerService s.CallConvThisc all)
<Module>::MyNat iveClass.{ctor} (MyNativeClass*
modopt([mscorlib]System.Runtime. CompilerService s.IsConst)
modopt([mscorlib]System.Runtime. CompilerService s.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. CompilerService s.CallConvThisc all)
<Module>::MyNat iveClass.DoSome thing(MyNativeC lass*
modopt([mscorlib]System.Runtime. CompilerService s.IsConst)
modopt([mscorlib]System.Runtime. CompilerService s.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(dou ble 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.ValueTyp e
{
.custom instance void
[mscorlib]System.Runtime. CompilerService s.NativeCppClas sAttribute::.ct or()
.custom instance void
[Microsoft.Visua lC]Microsoft.Visua lC.Miscellaneou sBitsAttribute: :.ctor(int32)
= ( int32(0x0000004 0) )
.custom instance void
[Microsoft.Visua lC]Microsoft.Visua lC.DebugInfoInP DBAttribute::.c tor()
}

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
11478
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 this a managed struct by adding either the value struct or ref struct I received a compile error stating that mixed types are not supported. I understand this, but I don't know how to create a char array in
81
4453
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 position are ones in which the companies are looking to migrate this to C# and .Net. I have NOT even seen one position requesting C++/CLI, let alone any recruiters who have even heard of it! I can understand those companies looking to create new...
12
2709
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
6640
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 that it is being instantiated twice. The first instantiation is down to me calling singleton.instance() in the C++/CLR DLL, the second instantiation is down to the library internally calling singleton.instance(). I'm relatively new to...
9
2333
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 generate an assertion failure for unoptimized, debug builds. We did this to find and fix cases where we were relying upon finalization rather than pro-active Dispose() calls. For classes that introduced IDisposable() into the class hierarchy...
62
2770
by: Born | last post by:
GC is really garbage itself Reason 1: There is delay between the wanted destruction and the actual destruction.
1
5561
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
4596
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: C#: class Test1 {
4
2874
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. I've got a C++/CLI program with a wrapper around some functions in the singleton of the native lib. 3. When I run my program, the wrappers instantiate their own copy of the singleton, i.e.
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10161
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10098
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9958
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8986
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6743
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5523
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2890
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.