Import C# function that returns List<string> to Python


My goal is to get a list of strings in Python (2.7) from C# function, that returns a List of strings. Please look at the code below.

C# code is using UnmanagedExports package. The function I want to import:

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;

I tried to import it in Python:

import os
import ctypes

MyDLL = 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

The error I get:

File "C:/.../", line .., in <module>
    res = get_inheritance_source(c_path)
WindowsError: [Error -532462766] Windows Error 0xE0434352

I am not sure any of this code is correct. Please help me to bring it all together. Thank you.

asked on Stack Overflow Oct 10, 2018 by okila

1 Answer


PyDotNet helped me to solve this problem.

Python code I use now:

import os
import dotnet

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

import ConsoleApp3
from ConsoleApp3 import Program
path = "c:\\dev\\py\\proj"
res = Program.GetInheritanceSource(path)
answered on Stack Overflow Oct 11, 2018 by okila

User contributions licensed under CC BY-SA 3.0