Click here to Skip to main content
Licence 
First Posted 19 May 2001
Views 84,698
Bookmarked 17 times

Using P/Invoke in a managed C++ application

By | 15 Oct 2001 | Article
This article demonstrates interoperability between .NET managed code and old unmanaged code. It uses the P/Invoke mechanism to call unmanaged DLL entry points.

Introduction

This article demonstrates interoperability between .NET managed code and old unmanaged code. It uses the P/Invoke mechanism to call unmanaged DLL entry points.

To demonstrate this technique let's create a managed C++ console program that will run an application specified as a parameter of its command line. We use the ShellExecute function exported by the unmanaged DLL shell32.dll

First we create a new C++ project based on the managed C++ Console Application. The application wizard creates a class that has one static method main() that is an entry point to our application.

To access the .NET classes for P/Invoke support add the following line to your code:

using namespace System::Runtime::InteropServices;

In order to call a DLL export, we need to declare the method with the attached a DllImport attribute.

// Declare the function that is exported from unmanaged dll (shell32.dll).
[DllImport("shell32.dll")]
extern "C" int _cdecl ShellExecute(int hwnd,              // Handle to a parent window.
                                   String *strVerb,       // Action to be performed.
                                   String *strFileName,   // File or object on which to execute the specified verb.
                                   String *strParameters, // Parameters to be passed to the application.
                                   String *strDirectory,  // Default directory.
                                   int nShowCmd);         // Flags.

The marshaling service will marshal the managed types to the unmanaged types according its built-in rules. During the P/Invoke call the marshaler will automatically copy the managed integer type into an unmanaged integer type, it will convert (copy) the Unicode character buffer of the String objects to the ANSI character buffer.

Other data types have other marshalling characteristics. You can override the default behavior by specifying custom marshalling. Refer to the Data Marshaling Specification section of the .NET Beta Specification in MSDN.

Now you can call your declared method. The following code illustrates this.

#using <mscorlib.dll>

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

// Declare the function that is exported from unmanaged dll (shell32.dll).
[DllImport("shell32.dll")]
extern "C" int _cdecl ShellExecute(int hwnd,              // Handle to a parent window.
                                   String *strVerb,       // Action to be performed.
                                   String *strFileName,   // File or object on which to execute the specified verb.
                                   String *strParameters, // Parameters to be passed to the application.
                                   String *strDirectory,  // Default directory.
                                   int nShowCmd);         // Flags.

// Managed class demonstrates Runtime's Platform Invocation Service
// (P/Invoke) to call unmanaged code from managed code.

 __gc class Launcher
{
public:
    // Starts program that specified by strFileName parameter
    static int StartProgram(String *strFileName)
    {
        return ShellExecute(0,
                            S"Open",
                            strFileName,
                            String::Empty,
                            String::Empty,
                            1 /*SW_SHOWNORMAL*/);
    }
};

// This is the entry point for this application
int main( int argc, char *argv[ ])
{
    // Check parameters.
    if(argc < 2)
    {
        Console::Write(S"Not enough parameters.");
        return 0;
    }

    // Call static function of the Launcher class and start program.
    if(Launcher::StartProgram(new String(argv[1])) < 33)
        Console::Write(S"Couldn't launch the program!");

    return 0;
}

History

16 Oct 2001 - updated source for VS .NET beta 2

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

About the Author

Igor Chouvalov



United States United States

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generala problem Pinmemberclark_xiao19:05 7 Sep '04  
GeneralQuery regarding ShellExecute( ) Pinmemberjssuthar22:19 15 Jan '04  
Generalold unmanaged code PinmemberRodrigo Strauss7:19 12 Jun '02  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 16 Oct 2001
Article Copyright 2001 by Igor Chouvalov
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid