IJW still works!

It would have been highly unlikely and extremely dumb if IJW did not work in C++/CLI, considering that the single most momentous feature in the Managed C++ language extensions was IJW. But I still wanted to try it out and did the usual <windows.h> inclusion and a call to the MessageBoxW API.

using namespace System;
using namespace stdcli::language;

#include <vcclr.h>
#include <windows.h>

ref class Hello
    void Show(String^ text, String^ title)
        pin_ptr<const Char> txt = PtrToStringChars(text);
        pin_ptr<const Char> titl = PtrToStringChars(title);
        MessageBoxW(GetDesktopWindow(), txt, titl, MB_OK);

void _tmain()
    Hello^ h = gcnew Hello();
    h->Show("Hello Nish","IJW still works");

Oh boy, despite fully expecting it to work, I still felt most thrilled when I saw the message box pop up on my screen. For those who are not aware of managed-unmanaged string conversions, PtrToStringChars takes a String^ and returns an interior pointer to the CLR-heap location where the String object currently resides. But we cannot pass this pointer directly to the MessageBoxW function as the GC might come into play and invalidate the address pointed to by the interior pointer. So we need to pin the pointer in memory for the duration of the unmanaged function call.

C++/CLI introduces the pin_ptr keyword (or rather operator) which is defined in the stdcli::language namespace (which explains why I had to refer it on top of the program). All we need to do is to declare pinned local variables which we pass to the MessageBoxW function and voila – IJW does the rest!


12 thoughts on “IJW still works!

  1. Are there any new constructs introduced which reduces the plumbing effort of converting between strings. I hated that when I had to do that in COM and equally hate it when I’m forced to do that in C++/CLI. By the way any idea/speculation on what are they planning to call the C++/CLI is it going to be a C++ V2 ๐Ÿ™‚

  2. Hi Kannan

    I believe that by the time VC++ Whidbey Beta is out, we will be able to use the marshal_as template as follows :-

    String^ str = "Managed string";

    Internally it would probably end up calling PtrToStringChars but it certainly would save us all some typing/headaches.


  3. OT: Nish, your RSS feed is marking each item as new everytime it updates. So in RSS readers the items all show up as new rather than leaving old items as, well, old.

  4. Hmmmm Paul, I’ll look into it. I use Sharp Reader and this doesn’t seem to happen to me. And Stephane Rodriguez has reported another problem too where he says the RSS feed itself is not valid.

    I did an update of my WordPress PHP-blog-software and perhaps the upgrade has introduced these issues. James and I use the same software and we are also on the same web host, so what we usually do is to see if the other guy’s blog has the same error ๐Ÿ™‚

    Thanks for letting me know, Paul.


  5. I think the problem is caused by someone forgetting to fill in a suitable default value for a new field introduced in WP. They added a post_date_gmt field in one of the later updates to WP but aside from the default value in mySQL they never updated the old posts to have suitable values. The quick fix is to just copy the regular post date over to the GMT one. It will be off by some number of hours from the real value but like I said, its a quick fix.

    The error that Stephan pointed out would definately be fixed by this, I’m taking a guess that since the RSS was invalid before the fix that this will fix Paul’s problem as well.

  6. The feed is invalid because anytime it’s refreshed it returns all blog entries including the first 7 ones that were not exactly well formed (content tag missing, pubdate tag empty). Cleaning those older blog entries by hand should suffice.

  7. Although the RSS feed is valid, I still have the error in SharpReader, even when removing the subscription, cleaning up the cache, restarting SharpReader, and then subscribing again.

    I believe it’s more about how RSS 2.0 is used in your feed. I believe SharpReader does not know the content:encoded tag, and probably finds crazy the idea of finding the blog body outside the tag. I have checked many other feeds, and no one else does that. The RSS feed that most resemble yours is that one : http://weblog.infoworld.com/udell/rss.xml. You’ll see John does not use the content tag at all, even though the content namespace is declared in the xml header.

  8. One question: does namespace stdcli really exist? Intellisense in my VS shows namespace cli and there are declarations of pin_ptr, interior_ptr, array and safe_cast. Could you explain me that?

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s