gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 20:57:42 24 4
gpt4 key购买 nike

这是 this question 的反转/补码.

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

我尝试了四种 C# + Python 变体 - 没有一个成功。任何人都可以完成这项工作吗?

C#

    /* no ref, convert to list */
[DllExport("combineWords1", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.AnsiBStr)]
public static string CombineWords1(object obj)
{
var l = (List<string>) obj;
return string.Join(",", l.ToArray());
}

/* no ref, convert to array */
[DllExport("combineWords2", CallingConvention = CallingConvention.Cdecl)]
[return: MarshalAs(UnmanagedType.AnsiBStr)]
public static string CombineWords2(object obj)
{
var l = (string[]) obj;
return string.Join(",", l);
}

/* ref, convert to list */
[DllExport("combineWords3", CallingConvention = CallingConvention.Cdecl)]
public static void CombineWords3(ref object obj)
{
var l = (List<string>)obj;
obj = string.Join(",", l.ToArray());
}

/* ref, convert to array */
[DllExport("combineWords4", CallingConvention = CallingConvention.Cdecl)]
public static void CombineWords4(ref object obj)
{
var l = (string[]) 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

最佳答案

您可以像这样声明 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);
}

Python 方面是这样的:

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 声明。 Ansi 已成为过去。

关于c# - 可以让 Python 向 C# 发送一个可变长度的字符串数组吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48732331/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com