layty
layty

Reputation: 31

How to call a C# library function with ref and out parameter from python?

I use pythonnet not ironpython.

There is a function like this:

test(ref string p1,out string p2)

How can I call test in python 3.6?

import clr
import sys
import System
sys.path.append(r'd:\dll')  
clr.FindAssembly('communication.dll')  
from  communication import *  
dll=IEC62056()
dll.test(----------)

Upvotes: 3

Views: 1869

Answers (2)

willhyper
willhyper

Reputation: 106

you can feed ref as a regular argument, and feed out arbitrarily. pythonnet will return them positionally as tuple.

assume void test(ref string p1,out string p2) in c#, you get p1, p2 = test(p1, None) in python

Upvotes: 0

mdk
mdk

Reputation: 408

I can't test the code without communication.dll, so please check if the following code is working for you:

import clr
import sys
sys.path.append("D:\\dll") # path to dll
clr.AddReference("communication") # add reference to communication.dll
from  communication import test

ref_string_p1 = "----------"
out_string_p2 = test(ref_string_p1)
print(out_string_p2)

Upvotes: 1

Related Questions