Condividi tramite


Procedura: Specificare un parametro out

Questo esempio illustra come specificare che un parametro di funzione è un out parametro e come chiamare tale funzione da un programma C#.

Un out parametro viene specificato in C++ usando OutAttribute .

Esempio

La prima parte di questo esempio crea una DLL C++. Definisce un tipo che contiene una funzione con un out parametro .

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

Questo file di origine è un client C# che usa il componente C++ creato nell'esempio precedente.

// 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);
      System.Console.WriteLine(t);
   }
}
a string

Vedi anche

Uso delle funzionalità di interoperabilità C++ (PInvoke implicito)