Click here to Skip to main content
11,502,348 members (65,166 online)
Click here to Skip to main content

Using IJW in Managed C++

, 2 May 2002 224.6K 27
Rate this:
Please Sign up or sign in to vote.
A basic introduction to using IJW instead of P/Invoke in Managed C++. This is also the first CP article on IJW.

Prologue

I have always loathed P/Invoke in an intense manner. I guess it's perhaps due to the fact that I am a very simple human being and thus I naturally disliked anything which was not simple. P/Invoke in my opinion was ugly and so pathetically unnatural. These two facets made it an utterly complicated entity. Then I came across these beautiful words by Nick Hodapp.

"IJW in C++ is syntactically easier than P/Invoke, and as I said, slightly more performant." - Nick Hodapp, Microsoft

Two things struck me immediately after I read those words. The first one, naturally was that there was no word called "performant" in the English dictionary, though I could actually understand very clearly what Nick Hodapp had meant by that word. The second more glaring point was that I didn't know what IJW meant. Later on when I realized that IJW simply meant, "It just works", I had this feeling for a few seconds that I was stuck in a world of lunacy. But after I tried it out, I simply said aloud, "It just works". Because, it really does work. And it's not ugly or unnatural like P/Invoke is. And as Nick Hodapp said, it's slightly more performant.

Using IJW

All you do is to simply #include the required C++ header file. Of course there is always a danger that there will be several name clashes between the definitions in the header file and the .NET framework classes and their member functions. I found this out the hard way when I got 100s of compilation errors. All of them simply said :- "error C2872: 'blahblahblah' : ambiguous symbol". Now as you can assume, this was a most distressing situation as far as I was concerned. It took my rather simple brain a couple of minutes to figure out that, I had to include the header file before all my using namespace directives.

Unlike P/Invoke, where all the data marshalling between .NET types and native types is done by the compiler, here we must do it ourselves. It's not a complicated issue at all once you take a look at the System.Runtime.InteropServices.Marshal class in the framework. Jolly nice class I tell ya, with jolly nice functions.

Without further tête-à-tête, let's see some sample code. In the tiny example program listed below I shall show you how to create a managed class, which can be instantiated from a managed block, and which uses IJW to call a native API call. You'll see how much more nicer this looks like when compared to the foul looking P/Invoke code.

Code Listing

#include "stdafx.h"

#using <mscorlib.dll>
#include <tchar.h>
#include <windows.h>

using namespace System;
using namespace System::Runtime::InteropServices;

public __gc class MsgBox
{
public:
    MsgBox(String *str)
    {
        IntPtr ptrtxt = Marshal::StringToCoTaskMemUni(str);
        MessageBoxW(0,(LPCWSTR)ptrtxt.ToPointer(),
            L"IJW is cool",0);
        Marshal::FreeCoTaskMem(ptrtxt);
    }
};

int _tmain(void)
{
 
    String *str;
    str = "Nish was here";
    MsgBox *m_msgbox = new MsgBox(str);
    return 0;
}

I have used StringToCoTaskMemUni which copies the string to an unmanaged area in the heap. Once I have made my call, I must free the string that has been allocated in the unmanaged heap area, because this will not get garbage collected. Isn't it truly amazing that when IJW existed, a lot of us were wasting our time with P/Invoke! Of course this is available only for Managed C++ programmers. The poor C# and VB .NET guys will have to suffer the P/Invoke monster as that's their only option.

I guess this is one very good reason for the use of Managed C++ ahead of C#. I am also hoping that this is the first IJW article on CP or perhaps on any non-Microsoft site. I guess I'll have to wait for Chris M to confirm that. I also do hope that it has served it's simple purpose. Thank you.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
GeneralI'm kind of surprised you think this is simpler.... Pin
TheWerewolf20-Jan-11 11:07
memberTheWerewolf20-Jan-11 11:07 
QuestionIJW seems not to work with more complicate controls Pin
gunag19-Jul-06 3:48
membergunag19-Jul-06 3:48 
QuestionHow about resources in the mfc-DLLs Pin
gunag6-Jul-06 3:35
membergunag6-Jul-06 3:35 
GeneralIJW does not work!!! Pin
SleepingPedant23-Nov-03 19:06
memberSleepingPedant23-Nov-03 19:06 
General[Message Removed] Pin
immetoz1-Oct-08 9:47
memberimmetoz1-Oct-08 9:47 
GeneralThat's the smartest interop article... Pin
.S.Rod.16-Apr-03 11:29
member.S.Rod.16-Apr-03 11:29 
GeneralRe: That's the smartest interop article... Pin
Nishant S16-Apr-03 16:26
editorNishant S16-Apr-03 16:26 
GeneralRe: That's the smartest interop article... Pin
.S.Rod.16-Apr-03 18:49
member.S.Rod.16-Apr-03 18:49 
GeneralRe: That's the smartest interop article... Pin
Nishant S16-Apr-03 18:58
editorNishant S16-Apr-03 18:58 
GeneralRe: That's the smartest interop article... Pin
Tom Archer28-Apr-03 13:28
memberTom Archer28-Apr-03 13:28 
GeneralRe: That's the smartest interop article... Pin
Nishant S28-Apr-03 15:49
editorNishant S28-Apr-03 15:49 
GeneralIJW doesn´t works!!!!!!!!!!!!!!! Pin
tomjackson15-Apr-03 20:08
membertomjackson15-Apr-03 20:08 
GeneralRe: IJW doesn´t works!!!!!!!!!!!!!!! Pin
Nishant S16-Apr-03 16:29
editorNishant S16-Apr-03 16:29 
QuestionHow to resolve names clash? Pin
Alex Farber13-Apr-03 23:08
memberAlex Farber13-Apr-03 23:08 
AnswerRe: How to resolve names clash? Pin
Nishant S14-Apr-03 3:25
editorNishant S14-Apr-03 3:25 
GeneralRe: How to resolve names clash? Pin
Alex Farber14-Apr-03 4:05
memberAlex Farber14-Apr-03 4:05 
GeneralRe: How to resolve names clash? Pin
Nishant S15-Apr-03 17:38
editorNishant S15-Apr-03 17:38 
GeneralRe: How to resolve names clash? Pin
Alex Farber26-Apr-03 19:27
memberAlex Farber26-Apr-03 19:27 
QuestionNish & Nick!!!??? Pin
OneAngryProgrammer15-May-02 19:23
memberOneAngryProgrammer15-May-02 19:23 
GeneralPerformant Pin
Nick Hodapp (MSFT)6-May-02 8:52
sitebuilderNick Hodapp (MSFT)6-May-02 8:52 
GeneralRe: Performant Pin
Nish [BusterBoy]6-May-02 15:05
memberNish [BusterBoy]6-May-02 15:05 
GeneralRe: Performant Pin
Jamie Hale8-May-02 9:17
memberJamie Hale8-May-02 9:17 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 9:35
memberNish [BusterBoy]8-May-02 9:35 
GeneralRe: Performant Pin
Jamie Hale8-May-02 9:48
memberJamie Hale8-May-02 9:48 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 9:55
memberNish [BusterBoy]8-May-02 9:55 
GeneralRe: Performant Pin
Jamie Hale8-May-02 10:07
memberJamie Hale8-May-02 10:07 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:15
memberNish [BusterBoy]8-May-02 10:15 
GeneralRe: Performant Pin
Bruno3-Oct-03 2:41
memberBruno3-Oct-03 2:41 
GeneralRe: Performant Pin
Thomas Freudenberg8-May-02 10:04
memberThomas Freudenberg8-May-02 10:04 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:07
memberNish [BusterBoy]8-May-02 10:07 
GeneralRe: Performant Pin
Thomas Freudenberg8-May-02 10:15
memberThomas Freudenberg8-May-02 10:15 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:19
memberNish [BusterBoy]8-May-02 10:19 
GeneralRe: Performant Pin
Thomas Freudenberg8-May-02 10:24
memberThomas Freudenberg8-May-02 10:24 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:28
memberNish [BusterBoy]8-May-02 10:28 
GeneralRe: Performant Pin
Thomas Freudenberg8-May-02 10:29
memberThomas Freudenberg8-May-02 10:29 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:32
memberNish [BusterBoy]8-May-02 10:32 
GeneralRe: Performant Pin
Jamie Hale8-May-02 10:11
memberJamie Hale8-May-02 10:11 
GeneralRe: Performant Pin
Nish [BusterBoy]8-May-02 10:17
memberNish [BusterBoy]8-May-02 10:17 
GeneralGood job Nish Pin
Shog93-May-02 20:33
memberShog93-May-02 20:33 
GeneralRe: Good job Nish Pin
Nish [BusterBoy]3-May-02 20:38
memberNish [BusterBoy]3-May-02 20:38 
GeneralRe: Good job Nish Pin
Lee Alexander4-May-02 0:49
memberLee Alexander4-May-02 0:49 
GeneralRe: Good job Nish Pin
Nish [BusterBoy]4-May-02 6:00
memberNish [BusterBoy]4-May-02 6:00 
QuestionThis is the first one, right? Pin
Nish [BusterBoy]3-May-02 7:29
memberNish [BusterBoy]3-May-02 7:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 3 May 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid