2017-09-19 21 views
0

私の目的は、PythonスクリプトライブラリをctypesはからC#のdllファイル機能にアクセスすることです。 私のC#コードは次のとおりです。C#のdllファイル機能へのアクセスは

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)); 
    } 
} 

}

とPythonのコードです:

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

しかし、私はエラーはAttributeErrorを取得しています:関数 'MyFunctionName' どのように私ができる

が見つかりません私はDLLファイルに含まれている関数にアクセスできないため、エラーを解決しますか?

答えて

0

多分これは役立ちます。 RGiesecke.DllExportのドキュメントから: - あなたは、x86、IA64またはx64のいずれかにお使いのプラットフォームの目標を設定する必要があります。 AnyCPUアセンブリは機能をエクスポートすることはできません。 - エクスポート名の既定値は、メソッド名とstdcallの呼び出し規則です。それがあなたの望むものなら、[DllExport]をパラメータなしで使うことができます。 - エクスポートをジェネリック型にしたり、ジェネリックメソッドをエクスポートすることはできません。 (CLRは使用するタイプパラメータを知りません)

+0

ソリューションプラットフォームをx64に以前に設定しましたが、引き続きエラーが発生しています。 –

関連する問題