Pass arguments by reference with ref keyword to .NET DLL in C++ app

This article provides an introduction to cross-technology invocation of both static and instance methods which contains argument of type ref. Parameter modifier ref in C# (.NET) technology is used in a method signature to pass an argument by reference. It is described in details in this article.

Javonet allows you to reference and use modules or packages written in (Java/Kotlin/Groovy/Clojure, C#/VB.NET, Ruby, Perl, Python, JavaScript/TypeScript) like they were created in your technology. If have not yet created your first project check Javonet overview and quick start guides for your technology.

With Javonet you can interact with methods with ref parameter modifier from .NET DLL like they were available in C++ but invocation must be performed through Javonet SDK API. Javonet allows you to pass any C++ value type as argument to method from .NET DLL. For reference type arguments (instances of other classes) you can create such instance with Javonet and pass the Invocation Context variable referencing that object as argument of static method invocation.

Custom .NET DLL with ref C++ argument

With Javonet it is possible to reference any custom .NET DLL and interact with its methods declared on types defined within that module almost the same as with any other C++ library.

Snippet below represents the sample code from .NET DLL which contains methods with ref parameter modifier.

public static void RefSampleMethod(ref int x)
{
    x = x * 2;
}

public static void RefSampleMethod2(ref int x, ref double y, ref string z)
{
    x = x * 2;
    y = y / 2;
    z = "Done";
}

It is possible to invoke the declared methods from .NET DLL using following C++ code:

I code in:
// use Activate only once in your app
Javonet::Activate("your-license-key");

// create called runtime context
auto calledRuntime = Javonet::InMemory()->Netcore();

// set up variables
auto libraryPath = resourcesDirectory + "/TestClass.dll";
auto className = "TestClass.TestClass";

// load custom library
calledRuntime->LoadLibrary(libraryPath);

// get type from the runtime
auto calledRuntimeType = calledRuntime->GetType(className)->Execute();

// create values for ref

// first way - pass only value
auto refValue1 = calledRuntime->AsRef(10)->Execute();

// second way - pass value and type
// ref variable should have specific type to be able to invoke methods on it
// this way enables to cast value to specific type needed by called method
auto intType = calledRuntime->GetType("System.Int32")->Execute();
auto refValue2 = calledRuntime->AsRef({ 20.0, intType })->Execute();

// invoke type's static method with ref values
calledRuntimeType->InvokeStaticMethod("RefSampleMethod", refValue1)->Execute();
calledRuntimeType->InvokeStaticMethod("RefSampleMethod", refValue2)->Execute();

// get ref values
auto result1 = std::any_cast<int>(refValue1->GetRefValue()->Execute()->GetValue());
auto result2 = std::any_cast<int>(refValue2->GetRefValue()->Execute()->GetValue());

// write result to console
std::cout << result1 << std::endl;
std::cout << result2 << std::endl;

This snippet uses in memory runtime bridging to load the .NET DLL and next retrieves reference to specific type.

Next, two reference type arguments are created. An argument that is passed to a ref parameter must be initialized before it's passed to a method. Two ways of initialization are presented in create values for ref section of the snippet.
Fist argument (refValue1) is initialized using asRef() method with default type of passed value (int). This type matches the type of parameter in RefSampleMethod(ref int x) method.
Second argument (refValue2) is initialized using asRef() method with specifying the exact type which should be use. "System.Int32" is an alias for int.

Both values can be passed to RefSampleMethod(ref int x) method using invokeStaticMethod(...).execute() invocation.

Each reference type argument is get as regular C++ value using getRefValue() and can be used for further processing.

Custom C++ with ref keyword

It is possible to invoke the declared method which contains multiple ref arguments from .NET DLL using following C++ code:

I code in:
// use Activate only once in your app
Javonet::Activate("your-license-key");

// create called runtime context
auto calledRuntime = Javonet::InMemory()->Netcore();

// set up variables
auto libraryPath = resourcesDirectory + "/TestClass.dll";
auto className = "TestClass.TestClass";

// load custom library
calledRuntime->LoadLibrary(libraryPath);

// get type from the runtime
auto calledRuntimeType = calledRuntime->GetType(className)->Execute();
auto doubleType = calledRuntime->GetType("System.Double")->Execute();

// create values for ref
// ref variable should have specific type to be able to invoke methods on it
// This way enables to cast value to specific type needed by called method
auto refToInt = calledRuntime->AsRef(10)->Execute();
auto refToDouble = calledRuntime->AsRef({ 5, doubleType })->Execute();
auto refToString = calledRuntime->AsRef("Before execution")->Execute();

// invoke type's static method with ref values
calledRuntimeType->InvokeStaticMethod("RefSampleMethod2", { refToInt, refToDouble, refToString })->Execute();

// get ref values
auto result1 = std::any_cast<int>(refToInt->GetRefValue()->Execute()->GetValue());
auto result2 = std::any_cast<double>(refToDouble->GetRefValue()->Execute()->GetValue());
auto result3 = std::any_cast<std::string>(refToString->GetRefValue()->Execute()->GetValue());

// write result to console
std::cout << result1 << std::endl;
std::cout << result2 << std::endl;
std::cout << result3 << std::endl;

This snippet uses in memory runtime bridging to load the .NET DLL and next retrieves reference to specific type.

Next, three reference type arguments are created. An argument that is passed to a ref parameter must be initialized before it's passed to a method. Two ways of initialization are presented in create values for ref section of the snippet.
Fist argument (refToInt) is initialized using asRef() method with default type of passed value (int). This type matches the type of first parameter in RefSampleMethod2 method.
Second value (refToDouble) is initialized using asRef() method with specifying the exact type which should be used. "System.Double" is an alias for double.
Third argument (refToString) is initialized using asRef() method with default type of passed value (string).

All three arguments are processed in RefSampleMethod2 and changed. Their values are obtained using getRefValue() method.

The same operation can be performed remotely by just changing the new Runtime Context invocation from in memory to tcp that will create and interact with your .NET DLL objects on any remote node, container or service that hosts Javonet Code Gateway. This way you can preserve the same logic in your application and instantly switch between monolithic and microservices architecture without the need to implement the integration layer based on web services or other remote invocation methods.

Read more about use cases and software architecture scenarios where Javonet runtime bridging technology can support your development process.