Posted by: Zeeshan Amjad | November 9, 2012

WinRT internals using C++. Part 1


Today I decided to play with WinRT using C++. I created a simple Blank Windows Store program and start looking at the source code generated by the Visual Studio. I discover that Visual Studio generate MainPage and App class as a ref class. Once I came across the ref classes when working on .Net using Managed C++ here is one example of it.

In my first experiment, I created two classes, one with the ref keyword and one without it.

Code Snippet
class MyClassA
{
};

ref class MyClassB
{
};

 

Then I used the sizeof operator to see how much memory is allocated for both of them.

Code Snippet
    int s1 = sizeof(MyClassA);

    int s2 = sizeof(MyClassB);

 

As expected the size of first one is 1 byte, but the size of second class is 24 bytes.

WinRT_Internal_01

It clearly shows that the second object must contain some extra information. I wanted to confirm the same thing with .Net, therefore I created one empty project in Visual Studio and set the property to use the common language runtime support i.e. /clr switch.

WinRT_Internal_02

And created exactly the same two classes. Here is our code.

Code Snippet
class A
{
};

ref class B
{
};

int main()
{
    int s1 = sizeof(A);

    int s2 = sizeof(B);
}

 

But I am not even able to compile the project. I am getting the following error message.

Error    1    error C2847: cannot apply sizeof to managed type ‘B’    C:\zamjad\Projects\WinRT\CppProject1\CppProject1\Source.cpp    14    1    CppProject1

It means that ref class in .Net and WinRT are two different things. Let’s go back to our WinRT program and play with it little bit more. Let’s generate the listing file of the source code.

WinRT_Internal_03

And then search for MyClassB to see what is special about this class. We will see the following entries in the file.

PUBLIC    ??0MyClassB@@Q$AAA@XZ                ; MyClassB::MyClassB
PUBLIC    ?__abi_QueryInterface@?QObject@Platform@@MyClassB@@U$AAGJAAVGuid@2@PAPAX@Z ; MyClassB::[Platform::Object]::__abi_QueryInterface
PUBLIC    ?__abi_AddRef@?QObject@Platform@@MyClassB@@U$AAGKXZ ; MyClassB::[Platform::Object]::__abi_AddRef
PUBLIC    ?__abi_Release@?QObject@Platform@@MyClassB@@U$AAGKXZ ; MyClassB::[Platform::Object]::__abi_Release
PUBLIC    ?__abi_GetIids@?QObject@Platform@@MyClassB@@U$AAGJPAKPAPAVGuid@2@@Z ; MyClassB::[Platform::Object]::__abi_GetIids
PUBLIC    ?__abi_GetRuntimeClassName@?QObject@Platform@@MyClassB@@U$AAGJPAPAUHSTRING__@@@Z ; MyClassB::[Platform::Object]::__abi_GetRuntimeClassName
PUBLIC    ?__abi_GetTrustLevel@?QObject@Platform@@MyClassB@@U$AAGJPAW4__abi_TrustLevel@@@Z ; MyClassB::[Platform::Object]::__abi_GetTrustLevel
PUBLIC    ?GetWeakReference@?QIWeakReferenceSource@Details@Platform@@MyClassB@@U$AAAP$AAUIWeakReference@23@XZ ; MyClassB::[Platform::Details::IWeakReferenceSource]::GetWeakReference
PUBLIC    ?__abi_Platform_Details_IWeakReferenceSource____abi_GetWeakReference@?QIWeakReferenceSource@Details@Platform@@MyClassB@@U$AAGJPAP$AAUIWeakReference@23@@Z ; MyClassB::[Platform::Details::IWeakReferenceSource]::__abi_Platform_Details_IWeakReferenceSource____abi_GetWeakReference

 

We can get some extra information with the help of this listing file. It shows the class named Object in Platform namespace and few familiar methods. First take a look at the size of the Object class.

Code Snippet
int s0 = sizeof(Platform::Object);

 

And its size is 4 bytes. Just like any class that implement one interface or has at least one virtual function. These methods are from three interfaces, IUnknown, IInspectable and IWeakReferenceSource.

It means that when we create ref class in WinRT, it is actually implanting these three interfaces. Here is class diagram of these interfaces.

 

WinRT_Internal_04

Advertisements

Responses

  1. […] further explore the WinRT and continue the discussion we started here. We created a simple program to see the ref class and simple class to compile in .Net. Let’s try […]

  2. […] first part that there is a different object size when we are using ref keyword when creating class here. We can use one undocumented C++ command line switch “reportAllClassLayout” to see the class […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: