2017-09-19 1 views
0

내 목표는 파이썬 스크립트 ctypes 라이브러리에서 C# DLL 파일 기능에 액세스하는 것입니다. 내 C# 코드는 다음과 같습니다파이썬 코드에서 C# DLL 기능 액세스

import ctypes 
    a=ctypes.cdll.LoadLibrary('File Location') 
    a.MyFunctionName("a") 

하지만 오류 AttributeError 무엇입니까 :

using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Text; 
    using System.Threading.Tasks; 
    using RGiesecke.DllExport; 
    using System.Runtime.InteropServices; 

    namespace ConsoleApp1 
    { 
     [ComVisible(true)] 
     public class Program 
     { 
    [DllExport("MyFunctionName", CallingConvention = CallingConvention.Cdecl)] 
    [return: MarshalAs(UnmanagedType.LPWStr)] 
    public static string MyFunctionName([MarshalAs(UnmanagedType.LPWStr)] string iString) 
    { 
     return "hello world i'm " + iString; 
    } 

    [DllExport("Add",CallingConvention = CallingConvention.Cdecl)] 
    public static int Add(int a, int b) 
    { 
     return a + b; 
    } 
    [DllExport(CallingConvention = CallingConvention.Cdecl)] 
    static public int Subtract(int a, int b) 
    { 
     return a - b; 
    } 
    [DllExport(CallingConvention = CallingConvention.Cdecl)] 
    static public int Multiply(int a, int b) 
    { 
     return a * b; 
    } 
    [DllExport(CallingConvention = CallingConvention.Cdecl)] 
    static public int Divide(int a, int b) 
    { 
     return a/b; 
    } 

    static void Main(string[] args) 
    { 
     //Console.Write(Add(2,3)); 
    } 
} 

}

및 파이썬 코드는

어떻게 할 수

를 찾을 수없는 기능을 'MyFunctionName' DLL 파일에 포함 된 함수에 액세스 할 수 없으므로 오류를 해결 하시겠습니까?

답변

0

아마도 도움이 될 것입니다. RGiesecke.DllExport 설명서에서 : - 플랫폼 대상을 x86, ia64 또는 x64로 설정해야합니다. AnyCPU 어셈블리는 함수을 내보낼 수 없습니다. - 내보내기 이름의 기본값은 메서드 이름과 stdcall에 대한 호출 규칙입니다. 이것이 모두 원하는 것이라면, 매개 변수없이 [DllExport]를 사용할 수 있습니다. - 내보내기를 제네릭 형식으로 두거나 일반 메서드를 내보낼 수 없습니다. (CLR은 사용할 매개 변수를 알지 못합니다.)

+0

솔루션 플랫폼을 x64로 앞에서 설정했지만 여전히 오류가 발생합니다. –