How to: Specify an out Parameter
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here. ArchiveDisclaimer

How to: Specify an out Parameter

This sample shows how to specify that a function parameter is an out parameter and how to call that function from a C# program.

An out parameter is specified in Visual C++ with OutAttribute .

The first part of this sample is a Visual C++ DLL with a type that contains a function with an out parameter.

// cpp_out_param.cpp
// compile with: /LD /clr:safe
using namespace System;
public value struct TestStruct {
   static void Test([Runtime::InteropServices::Out] String^ %s) {
      s = "a string";

This is a C# client that consumes the Visual C++ component created in the previous example.

// cpp_out_param_2.cs
// compile with: /reference:cpp_out_param.dll
using System;
class TestClass {
   public static void Main() {
      String t;
      TestStruct.Test(out t);
a string
© 2016 Microsoft