为了账号安全,请及时绑定邮箱和手机立即绑定

将返回 List<string> 的 C# 函数导入 Python

将返回 List<string> 的 C# 函数导入 Python

德玛西亚99 2021-07-05 10:58:11
我的目标是从 C# 函数中获取 Python (2.7) 中的字符串列表,它返回一个字符串列表。请看下面的代码。C# 代码正在使用UnmanagedExports包。我要导入的功能:using System;using System.Collections.Generic;using RGiesecke.DllExport;using System.Runtime.InteropServices;namespace GetInheritanceLib{public class Program{    [DllExport("GetInheritanceSource", CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]     [return: MarshalAs(UnmanagedType.LPArray, ArraySubType = UnmanagedType.BStr, SizeParamIndex = 1)]    public static List<string> GetInheritanceSource([MarshalAs(UnmanagedType.LPWStr)] string path)    {        List<string> inheritedFrom = new List<string>(); //some correct result of unknown length;        return inheritedFrom;    }}}我尝试在 Python 中导入它:import osimport ctypesMyDLL = os.getcwd() + "\ConsoleApp3.dll"my_dll_loaded = ctypes.cdll.LoadLibrary(MyDLL)get_inheritance_source = my_dll_loaded.GetInheritanceSource path = "c:\\dev\\py\\proj"c_path = ctypes.c_char_p(path)get_inheritance_source.restype = ctypes.POINTER(ctypes.POINTER(ctypes.c_char_p)) # is it a list of strings?get_inheritance_source.argtypes = [ctypes.c_char_p] # is it a ctring?res = get_inheritance_source(c_path) # I expect this is a list of strings, but我得到的错误:File "C:/.../c_sharp.py", line .., in <module>    res = get_inheritance_source(c_path)WindowsError: [Error -532462766] Windows Error 0xE0434352我不确定这些代码是否正确。请帮我把这一切放在一起。谢谢你。
查看完整描述

1 回答

?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

我现在使用的 Python 代码:


import os

import dotnet


dotnet.add_assemblies(os.getcwd()) # a folder with DLLs

dotnet.load_assembly('ConsoleApp3')


import ConsoleApp3

from ConsoleApp3 import Program

path = "c:\\dev\\py\\proj"

res = Program.GetInheritanceSource(path)


查看完整回答
反对 回复 2021-07-13
  • 1 回答
  • 0 关注
  • 210 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号