Can one have Python send a variable-length string array to C#?
See the question and my original answer on StackOverflowYou can declare the C# side like this:
[DllExport("combinewords", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.LPWStr)] // matches python's c_wchar_p
public static string CombineWords(object obj) // object matches python's VARIANT
{
var array = (object[])obj; // if obj is an array, it will always be an array of object
return string.Join(", ", array);
}
And the python side like this:
import ctypes
from ctypes import *
from comtypes.automation import VARIANT
dll = ctypes.cdll.LoadLibrary("exported") # the dll's name
dll.combinewords.argtypes = [VARIANT] # python VARIANT = C# object
dll.combinewords.restype = ctypes.c_wchar_p
# create a VARIANT from a python array
v = VARIANT(["hello", "world"])
print(dll.combinewords(v))
Note: I have used unicode declaration wich is better. Ansi is a thing of the past.