可以让Python向C#发送可变长度的字符串数组吗?

可以让Python向C#发送可变长度的字符串数组吗?,c#,python,dll,ctypes,unmanagedexports,C#,Python,Dll,Ctypes,Unmanagedexports,这是的相反/补充 我正在使用and,并希望将一个长度可变的字符串列表从Python传递到C#,然后返回一个字符串 我尝试了四种C#+Python变体——没有一种成功。有人能完成这项工作吗 C# /*无参考,转换为列表*/ [DllExport(“combineWords1”,CallingConvention=CallingConvention.Cdecl)] [返回:MarshalAs(UnmanagedType.AnsiBStr)] 公共静态字符串组合ORDS1(对象obj) { var l

这是的相反/补充

我正在使用and,并希望将一个长度可变的字符串列表从Python传递到C#,然后返回一个字符串

我尝试了四种C#+Python变体——没有一种成功。有人能完成这项工作吗

C#

/*无参考,转换为列表*/
[DllExport(“combineWords1”,CallingConvention=CallingConvention.Cdecl)]
[返回:MarshalAs(UnmanagedType.AnsiBStr)]
公共静态字符串组合ORDS1(对象obj)
{
var l=(列表)obj;
返回字符串.Join(“,”,l.ToArray());
}
/*无参考,转换为数组*/
[DllExport(“combineWords2”,CallingConvention=CallingConvention.Cdecl)]
[返回:MarshalAs(UnmanagedType.AnsiBStr)]
公共静态字符串组合ORDS2(对象obj)
{
var l=(字符串[])obj;
返回字符串。Join(“,”,l);
}
/*ref,转换为列表*/
[DllExport(“combineWords3”,CallingConvention=CallingConvention.Cdecl)]
公共静态无效组合WORDS3(参考对象obj)
{
var l=(列表)obj;
obj=string.Join(“,”,l.ToArray());
}
/*ref,转换为数组*/
[DllExport(“combineWords4”,CallingConvention=CallingConvention.Cdecl)]
公共静态无效组合ORDS4(参考对象obj)
{
var l=(字符串[])obj;
obj=string.Join(“,”,l.ToArray());
}
Python

import ctypes
from comtypes.automation import VARIANT

dll = ctypes.cdll.LoadLibrary("<...>")
l = ["Hello", "world"]

# 1 
dll.combineWords1.argtypes = [ctypes.POINTER(VARIANT)]
dll.combineWords1.restype = ctypes.c_char_p
obj = VARIANT(l)
dll.combineWords1(obj)
#WindowsError: [Error -532462766] Windows Error 0xE0434352

# 2
dll.combineWords2.argtypes = [ctypes.POINTER(VARIANT)]
dll.combineWords2.restype = ctypes.c_char_p
obj = VARIANT(l)
dll.combineWords2(obj)
# WindowsError: [Error -532462766] Windows Error 0xE0434352

# 3
dll.combineWords3.argtypes = [ctypes.POINTER(VARIANT)]
obj = VARIANT(l)
dll.combineWords3(obj)
#WindowsError: [Error -532462766] Windows Error 0xE0434352

# 4
dll.combineWords4.argtypes = [ctypes.POINTER(VARIANT)]
obj = VARIANT(l)
dll.combineWords4(obj)
#WindowsError: [Error -532462766] Windows Error 0xE0434352      
导入ctypes
从comtypes.automation导入变量
dll=ctypes.cdll.LoadLibrary(“”)
l=[“你好”,“世界”]
# 1 
dll.combineWords1.argtypes=[ctypes.POINTER(变量)]
dll.combineWords1.restype=ctypes.c\u char\p
obj=变量(l)
dll.combineWords1(obj)
#WindowsError:[错误-532462766]Windows错误0xE0434352
# 2
dll.combineWords2.argtypes=[ctypes.POINTER(变量)]
dll.combineWords2.restype=ctypes.c\u char\p
obj=变量(l)
dll.combineWords2(obj)
#WindowsError:[错误-532462766]Windows错误0xE0434352
# 3
dll.combineWords3.argtypes=[ctypes.POINTER(变量)]
obj=变量(l)
dll.combineWords3(obj)
#WindowsError:[错误-532462766]Windows错误0xE0434352
# 4
dll.combineWords4.argtypes=[ctypes.POINTER(变量)]
obj=变量(l)
dll.combineWords4(obj)
#WindowsError:[错误-532462766]Windows错误0xE0434352
您可以这样声明C端:

[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);
}
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))  
python方面是这样的:

[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);
}
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))  
注意:我使用的是unicode声明,wich更好。Ansi已经成为过去。

你可以这样声明C端:

[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);
}
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))  
python方面是这样的:

[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);
}
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))  
注意:我使用的是unicode声明,wich更好。Ansi已经成为过去