WendyG
WendyG

Reputation: 587

C++/CLI and .NET out string paramter

In C#, I have this method (.net framework 2.0)

 public String Authenticate( String configUrl, out String tokenId)

I want to call it from managed c++ code I have

__authenticator->Authenticate(  gcnew System::String(hostUrl),gcnew System::String(temp));

but the tokenId is coming back as true.

I have seen some answers talk about using ^ % in the C# but that just doesn't compile.

Upvotes: 1

Views: 147

Answers (2)

xanatos
xanatos

Reputation: 111860

With

public String Authenticate(String configUrl, out String tokenId)

this

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    gcnew System::String(temp)
);

is, in C#, equivalent (considering the signature of Authenticate) to

__authenticator.Authenticate(
    new String(hostUrl),
    out new String(temp)
);

but in C# you can't do a out new Something, you can only out to variables, fields... So in C# you would need to do:

String temp2 = new String(temp);

__authenticator.Authenticate(
    new String(hostUrl),
    out temp2
);

and, considering that the parameter is in out you can:

String temp2;

__authenticator.Authenticate(
    new String(hostUrl),
    out temp2
);

Now, in C++/CLI you have

System::String^ temp2 = gcnew System::String(temp);

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    temp2
);

or, knowing that temp2 is out (note that the difference between ref and out is something checked only by the C# compiler, not by the C++/CLI compiler)

// agnostic of the out vs ref
System::String^ temp2 = nullptr;

// or knowing that temp2 will be used as out, so its value is irrelevant
// System::String^ temp2;

__authenticator->Authenticate(
    gcnew System::String(hostUrl),
    temp2
);

Upvotes: 3

WendyG
WendyG

Reputation: 587

Ok i got it, make the parameter I pass in a String^

CString hostUrl;
String^ temp ;
String^ error = __authenticator.get() == nullptr ? "failed to get token" : 
                 __authenticator->Authenticate(  gcnew System::String(hostUrl),temp);

Upvotes: 1

Related Questions