HOW TO: Create a Library Using the Out Parameter Function in Visual C++ .NET (818271)



The information in this article applies to:

  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework 1.0
  • Microsoft Visual C++ .NET (2002)
  • Microsoft Visual C++ .NET (2003)

This article refers to the following Microsoft .NET Framework Class Library namespace:

System.Runtime.InteropServices

IN THIS TASK

SUMMARY

This step-by-step article describes how to create a Managed C++ library that has a function that uses an Out parameter. The PARAMFLAG::Out parameter attribute is used to create such functions. This article also details how to access this function from a Microsoft Visual C# .NET console application.

back to the top

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Windows 2000 Professional, Microsoft Windows 2000 Server or Microsoft Windows 2000 Advanced Server.
  • Microsoft Visual Studio .NET.
This article assumes that you are familiar with the following topics:
  • Visual Studio .NET
back to the top

Create a Managed C++ DLL

  1. Start Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates. Name the project ManagedFunction.
  4. Open the ManagedFunction.h code file. Import the System.Runtime.InteropServices namespace so that you do not have to fully qualify names later in your code.
    using namespace System;
    using namespace System::Runtime::InteropServices;
  5. Add the following class to the ManagedFunction namespace:
    public __value class Class2
    {
    };
    Note __value classes are intended to hold small data items with short lifetimes. A __value type differs from a __gc type. __value type variables directly contain their data, and managed variables point to their data. __value type data is stored on the common language runtime heap.
  6. Add the ProvideString public method to the Class2 class that takes an Out parameter String object. Your Class2 class should appear as follows:
    public __value class Class2
    {
    	public:
    	static void ProvideString([PARAMFLAG::Out] String* *s)
    	{
    		*s = S"Msg From Managed C++";
    	}
    };
  7. Build the project to create the ManagedFunction.dll assembly.
  8. Save, and then close the project.
back to the top

Call a Managed C++ Function from Visual C# .NET

  1. Start Visual Studio .NET
  2. On the File menu, point to New, and then click Project.
  3. Click Visual C# Projects under Project Types, and then click Console Application under Templates.
  4. Add a reference to the ManagedFunction.dll assembly that you created in the Create a Managed C++ DLL section of this article.
  5. Use the using statement on the namespaces so that you do not have to fully qualify declarations in those namespaces later in your code. As in the following example, you must use the using statement before any other declarations:
    using ManagedFunction;
  6. Declare a String variable in the .cpp file. You do not have to initialize a variable that is passed as an Out argument.
    String str;  // variable does not have to be initialized
  7. Call the Managed C++ function by using the Out parameter. To use an Out parameter, you must explicity pass the argument to the method as an Out argument. Paste the following code in the .cpp file:
    Class2.ProvideString(out str);
    System.Console.WriteLine(str);
  8. Build, and then run the project. Notice that the String object from the Managed C++ function appears in the console application.
back to the top

Modification Type:MajorLast Reviewed:6/16/2003
Keywords:kbHOWTOmaster kbManaged kbhowto KB818271 kbAudDeveloper kbAudITPRO