I'm trying to create a method using mono where a string is passed by reference,
here is the test code I have:
C++:
static bool p_TestMethod(int num, MonoString ** response) {
auto b = mono_string_new(mono_domain_get(), "Test repsonse");
response = &b;
return true;
}
//...
mono_add_internal_call("SharpCode.TestClass::Testmethod", p_TestMethod);
C#:
[MethodImpl(MethodImplOptions.InternalCall)]
public static extern bool Testmethod(int num, out string response);
public bool RunTheTest()
{
string x;
Testmethod(0, out x);
Console.WriteLine("Test: {0}", x);
return true;
}
But no response is printed (only Test: )
How do I properly pass a string by reference using Mono?
Fixed by doing it like this:
*response = mono_string_new(mono_domain_get(), "Test repsonse");
as suggested by delnan
Related
I would like to convert (but i think it's not possible) a string into a bool, in this way:
string a = "b.Contains('!')";
private void print(string condition)
{
string b = "Have a nice day!";
/* Some kind of conversion here for condition parameter */
/* like bool.Parse(condition) or Bool.Convert(condition) */
if(condition)
{
Console.Write("String contains !-character.");
}
}
Mind that the bool-string has to be passed to the function as a parameter.
Is there a way to achieve this?
There is no built in way to parse your string to a expression.
But of your goal is to sent the expression to an other function you could do this
using System;
public class Program
{
public static void Main()
{
print(x=>x.Contains("!"));
}
private static void print(Func<string,bool> condition)
{
string b = "Have a nice day!";
/* Some kind of conversion here for condition parameter */
/* like bool.Parse(condition) or Bool.Convert(condition) */
if(condition.Invoke(b))
{
Console.Write("String contains !-character.");
}
}
}
if you would like a non build in way you could look at : Is there a tool for parsing a string to create a C# func?
I think you need to use an auxiliar bool variable, like this example..
bool aux = false;
private bool print(string condition)
{
string b = "Have a nice day!";
if(b.Contains(condition))
aux = true;
return aux;
}
Or the same example without auxiliar.
private bool print(string condition)
{
string b = "Have a nice day!";
return b.Contains(condition);
}
Then call the method print to check if it is true or false and write the message you want
if(print("!"))
Console.WriteLine("String contains !-character.");
I've a managed C# test DLL and I want to call its functions from my unmanaged C++ code.
The problem is that I can't find an example that shows how to call arbitrary functions from my c++ code, I could only get the ICLRRuntimeHost::ExecuteInDefaultAppDomain working, but this function does use a predefined method signature.
Question
I'm able to execuete the ShowMsg function from my C# DLL but I can't find out how to call methods with other signatures than int(int). Can someone please help me with that?
C++ Code
// interface definitions of functions from c# dll
typedef int (*TestCallbackPtr)(std::wstring info, int value1, double value2);
typedef int (*TestFunctionPtr)(std::wstring string1, int int1, double double1, TestCallbackPtr callback);
// path of my c# dll
auto pathDll = L"..."
void RunTest()
{
ICLRMetaHost* clrMetaHost = NULL;
ICLRRuntimeInfo* clrRuntimeInfo = NULL;
ICLRRuntimeHost* clrRuntimeHost = NULL;
// loading the data above is excluded for simplicity!
// ...
// -------------
// Test 1: run ShowMsg => WORKING
// -------------
DWORD pReturnValue;
HRESULT result = clrRuntimeHost->ExecuteInDefaultAppDomain(pathDll, L"Test.Managed", L"ShowMsg", L"Hello from C++", &pReturnValue);
// -------------
// Test 2: run TestFunction
// -------------
TestCallbackPtr callback = [](std::wstring info, int value1, double value2) {
return 21;
};
TestFunctionPtr function = NULL;
// how to continue? how can I execute the function here and assign my local function pointer so that following will work:
// (*function)(L"Test", 45, 1.23, callback);
}
C# Code
namespace Test
{
public class Managed
{
public static int ShowMsg(string msg)
{
MessageBox.Show(msg);
return 0;
}
public delegate int TestCallback([MarshalAs(UnmanagedType.LPWStr)] string info, int value1, double value2);
public static int TestFunction(
[MarshalAs(UnmanagedType.LPWStr)] string string1,
int int1,
double double1,
TestCallback callback)
{
string info = $"string1 = {string1} | int1 = {int1} | double1 = {double1}";
int returnValue = callback(info, 5, 10.34);
return returnValue;
}
}
}
I implemented a code that read the value of a variable in a PLC. the value could be of whatever type (bool, string, int16, int32 and so on).
Actually I implemented this code using cast.
But now I saw that cast makes box/unboxing and that's a bad idea for a clean memory usage.
So I'm trying to make the same behavior but with a generic method type which is linked at run time.
So here is my code with the commentaries.
That works with all struct types(bool, int32) but not with class type (String), could you tell my what I missed so far.
namespace Template_String
{
class Program
{
public int num1 = 33;
public static string PLCstrvar = "PLCvarName";
static void Main(string[] args)
{
Console.WriteLine((Int32)Readwrite.READ(PLCstrvar));//working with cast
Console.WriteLine(Readwrite.READImp2<Int32>(PLCstrvar));//Working with generics
Console.WriteLine(Readwrite.READImp2<String>(PLCstrvar));//not working
}
}
public static class Readwrite
{
//the actual code
public static object READ(String PLCVariableName, Boolean WorkingDATA = false)
{//my code return an type object which is not really good regarding box / unbox implementation
//I'd like to do something like
String returnstr = FINDSYMBOL(PLCVariableName, WorkingDATA);
if (returnstr != "")
{
return _plcClient.ReadSymbol(returnstr);
}
return "";
}
//the projected code
public static T READImp2<T>(String PLCVariableName, Boolean WorkingDATA = false) where T : new()
{//The new code uses generics but that doesn't work with String as they are Class and not struct as Boolean are
//I'd like to do something like
dynamic returnstr = new T();//resolved in runtime
returnstr = FINDSYMBOL(PLCVariableName, WorkingDATA);
if (returnstr != "")
{
return(T)_plcClient.ReadSymbol(PLCVariableName);
}
return (T)false;//not good
}
public static String FINDSYMBOL(String strvalueToreach, Boolean WorkingDATA = false)
{
String StrValueToReach = "";
if (!WorkingDATA)
{
for (int inc = 2; inc < 10; inc++)
{
StrValueToReach = strvalueToreach;
if (_plcClient.SymbolExists(StrValueToReach))
{
return StrValueToReach;
}
else
{
return "null";
}
}
}
return "null";
}
}
/EDIT That is a black box developped by XY developpers far away from me. This code is only used to make the code running without their code !!/
public static class _plcClient //PLC dynamic link !!dummy code implemented elsewhere!!
{
/* this code return if the symbole exist and its value in Int32, Int16, String, Boolean.... type.*/
private static string nullable="";
public static bool SymbolExists(string value) { return true;}
public static object ReadSymbol(String value) { return nullable = "toto"; }
}
}
(Unfortunately, SWIG's documentation is very difficult to parse and online examples seem rare. So I come here.)
Suppose a C++ function uses this typical return style for a class type:
void func(Class& out);
Using SWIG, this function should be wrapped in C# like this:
Class func();
From what I've found, I can use a typemap to accomplish this.
Pretending that Class is actually int, I've attempted the following based on examples I've found:
%include <typemaps.i>
%{
void func(int& pOut);
%}
%apply int &OUTPUT { int &pOut }
void func(int& pOut);
Many examples (leaning toward Python, though) suggest that this should create a function with no parameters that outputs an int.
However, I've used the following commandline:
swig.exe -namespace Test -o .\Test.cxx -c++ -module Test -csharp -outdir . test.i
This output the following Test.cs:
namespace Test {
using System;
using System.Runtime.InteropServices;
public class Test {
public static void func(out int pOut) {
TestPINVOKE.func(out pOut);
}
}
}
How can I achieve the function signature I want, and how do I transfer this to an object type?
Looks like I've found a way to do this specifically in C#, although it should be extendable to other languages.
Consider this SWIG interface, where I've added additional arguments for dramatic effect:
%include <typemaps.i>
%{
class MyClass{};
void func(MyClass& pOut, int x);
MyClass* func2(int x);
%}
%typemap(ctype, out="void *") void func ""
%typemap(imtype, out="global::System.IntPtr") void func ""
%typemap(cstype, out="MyClass") void func ""
%typemap(in, numinputs=0, noblock=1) MyClass &pOut
{
$1 = new MyClass();
}
%typemap(argout, noblock=1) MyClass &pOut
{
$result = $1;
}
%typemap(csout, excode=SWIGEXCODE) void func
{
IntPtr cPtr = $imcall;$excode
MyClass ret = (cPtr != IntPtr.Zero) ? null : new MyClass(cPtr, $owner);
return ret;
}
class MyClass{};
void func(MyClass& pOut, int x);
MyClass* func2(int x);
I've included func2 with the proper signature as well.
The first 3 %typemaps change the return type of the C++ wrapper function, C# interop method, and the C# wrapper method respectively.
The %typemap(in) removes the extraneous output parameter and adds code to use a new object in its place. This also, miraculously, leaves other arguments intact.
The %typemap(argout) uses the output parameter value as the newly created return value.
The %typemap(csout) rewrites the C# wrapper method code to utilize the return value of the interop method just like in the normal case.
Here are the example outputs proving it works like a charm:
Test.cxx
SWIGEXPORT void * SWIGSTDCALL CSharp_func(int jarg2) {
void * jresult ;
MyClass *arg1 = 0 ;
int arg2 ;
arg1 = new MyClass();
arg2 = (int)jarg2;
func(*arg1,arg2);
jresult = arg1;
return jresult;
}
SWIGEXPORT void * SWIGSTDCALL CSharp_func2(int jarg1) {
void * jresult ;
int arg1 ;
MyClass *result = 0 ;
arg1 = (int)jarg1;
result = (MyClass *)func2(arg1);
jresult = (void *)result;
return jresult;
}
TestPINVOKE.cs
[DllImport("Test", EntryPoint="CSharp_func")]
public static extern global::System.IntPtr func(int jarg2);
[DllImport("Test", EntryPoint="CSharp_func2")]
public static extern IntPtr func2(int jarg1);
Test.cs
public class Test {
public static MyClass func(int x) {
IntPtr cPtr = TestPINVOKE.func(x);
MyClass ret = (cPtr != IntPtr.Zero) ? null : new MyClass(cPtr, false);
return ret;
}
public static MyClass func2(int x) {
IntPtr cPtr = TestPINVOKE.func2(x);
MyClass ret = (cPtr == IntPtr.Zero) ? null : new MyClass(cPtr, false);
return ret;
}
}
The C#-specific %typemaps would need to be replaced with other language-specific ones to use with other languages, but alas I found no language-agnostic way to do it.
To make this work easily with multiple types and functions, a macro could be defined.
I've trying to achieve something like this:
class App {
static void Main(string[] args) {
System.Console.WriteLine(new Test("abc")); //output: 'abc'
System.Console.ReadLine();
}
}
I can do this passing by an variable:
class Test {
public static string str;
public Test (string input) { str = input; }
public override string ToString() {
return str;
}
}
works fine.
But, my desire is do something as:
class Test {
public static string input;
public Test (out input) { }
public override string ToString() {
return input;
}
}
System.Console.WriteLine(new Test("abc test")); //abc test
Don't works.
How I do this?
Thanks,advanced.
You can't. The variable approach is exactly the correct way, although the variable shouldn't be declared static, and shouldn't be a public field.
class Test {
public string Input {get;set;}
public Test (string input) { Input = input; }
public override string ToString() {
return Input;
}
}
I have an impression that you're not entirely understand what out keyword means. Essentially when you're writing something like void MyMethod(out string var) it means you want to return some value from method, not pass it into method.
For example there's bool Int32.TryParse(string s, out int result). It parses string s, returns if parse was successful and places parsed number to result. Thus, to correctly use out you should have real variable at the calling place. So you can't write Int32.Parse("10", 0) because this method can't assign result of 10 to 0. It needs real variable, like that:
int result;
bool success = Int32.TryParse("10", out result);
So, your desire is somewhat else - it is not in line with language designer's intentions for out :)