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?