Microsoft KB Archive/818271

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

Article ID: 818271

Article Last Modified on 6/16/2003

-

APPLIES TO


 * Microsoft .NET Framework 1.1
 * Microsoft .NET Framework 1.0
 * Microsoft Visual C++ .NET 2002 Standard Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition

-





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

System.Runtime.InteropServices

IN THIS TASK
 * SUMMARY
 * Requirements
 * Create a Managed C++ DLL
 * Call a Managed C++ Function from Visual C# .NET



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
 Start Visual Studio .NET. On the File menu, point to New, and then click Project. Click Visual C++ Projects under Project Types, and then click Managed C++ Class Library under Templates. Name the project ManagedFunction .  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;   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.   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&quot;Msg From Managed C++&quot;; } }; </li> Build the project to create the ManagedFunction.dll assembly.</li> Save, and then close the project.</li></ol>

back to the top

Call a Managed C++ Function from Visual C# .NET
<ol> Start Visual Studio .NET</li> On the File menu, point to New, and then click Project.</li> Click Visual C# Projects under Project Types, and then click Console Application under Templates.</li> Add a reference to the ManagedFunction.dll assembly that you created in the Create a Managed C++ DLL section of this article.</li>  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; </li>  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 </li>  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); </li> Build, and then run the project. Notice that the String object from the Managed C++ function appears in the console application.</li></ol>

back to the top

Keywords: kbhowto kbhowtomaster kbmanaged KB818271

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.