2015-10-16 93 views
7

我使用NuGet包UnmanagedExports建一个C#DLL(MyTestDll):加载C#DLL

path = "C:\\Temp\\Test" 
os.chdir(path) 
dll = ctypes.WinDLL("MyTestDll.dll") 
f = dll.Test 
f.restype = ctypes.c_char_p 
print f('qqq') 

[DllExport("Test", CallingConvention = CallingConvention.Cdecl)] 
public static string Test(string name) 
{ 
    return "hi " + name + "!"; 
} 

我通过ctypes的DLL导入使用它在Python

这只是一个幻想,它的作品。

然后,我增加了一个DLL(NoSenseDll):

namespace NoSenseDll 
{ 
    public class NoSenseClass 
    { 
     public static int Sum(int a, int b) 
     { 
      return a + b; 
     } 
    } 
} 

我开始使用这个NoSenseDll实现MyTestDll:

[DllExport("Test", CallingConvention = CallingConvention.Cdecl)] 
public static string Test(string name) 
{ 
    return NoSenseDll.NoSenseClass.Sum(4, 5).ToString(); 
} 

不幸的是,这是行不通的。蟒蛇说:

WindowsError: [Error -532462766] Windows Error 0xE043435 

我试图添加C:\\Temp\\Test到路径,但这并没有帮助。


我写了一个C++测试:

#include "stdafx.h" 
#include "windows.h" 
#include <iostream> 
#include <string> 
#include "WinBase.h" 

typedef char*(__stdcall *f_funci)(const char*); 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int t; 
    std::string s = "C:\\Temp\\Test\\MyTestDll.dll"; 
    HINSTANCE hGetProcIDDLL = LoadLibrary(std::wstring(s.begin(), s.end()).c_str()); 

    f_funci funci = (f_funci)GetProcAddress(hGetProcIDDLL, "Test"); 

    std::cout << "funci() returned " << funci(std::string("qqq").c_str()) << std::endl; 
    std::cin >> t; 
    return EXIT_SUCCESS; 
} 

它的工作原理,如果第二个DLL(NoSenseDll)是相同的文件夹C++可执行文件。如果我只是将NoSenseDll文件夹添加到PATH,它不起作用。

+0

*我试着添加'C:\\ Temp \\ Test'到路径,但是没有帮助。*你真的使用双反斜杠吗?也许这可能是问题所在。只是猜测。 – Palec

+0

我尽我所能编辑您的问答以便更容易理解,并且我将NoSen * c * eDll修正为NoSen * s * eDll。感谢您为此问答所做的努力! – Palec

回答

4

解决吃水:

  1. 复制NoSenseDll到Python的文件夹,在我的情况%HOMEPATH%\Anaconda
  2. 重新启动IPython/Spyder。

最终的解决方案:

static MyTestDllClass() // static constructor 
{ 
    AppDomain currentDomain = AppDomain.CurrentDomain; 
    currentDomain.AssemblyResolve += new ResolveEventHandler(LoadFromSameFolder); 
} 
static Assembly LoadFromSameFolder(object sender, ResolveEventArgs args) 
{ 
    string folderPath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
    string assemblyPath = Path.Combine(folderPath, new AssemblyName(args.Name).Name + ".dll"); 
    if (File.Exists(assemblyPath) == false) return null; 
    Assembly assembly = Assembly.LoadFrom(assemblyPath); 
    return assembly; 
} 

最后一点:

如果你不能使用,因为matplotlib或熊猫的IronPython的,
如果你不能使用python.net由于IPython或Spyder,
如果你不想使用COM Interop只是因为,
和你真实希望让C#和Python一起工作,使用上面的解决方案和C#反射。

+0

你能指点问题从pythonnet在ipython或spyder? – denfromufa

2

你也可以看看Costura.Fody。

这是一个构建任务,将您的依赖关系作为资源添加到您的程序集,甚至挂钩模块初始化程序以在运行时加载它们。

+0

你能举个例子吗?这与您的非托管出口有何不同? – denfromufa

+1

您仍然会使用我的软件包,但是在Costura中,您的依赖项将作为资源添加到您的程序集中。 Costura增加挂钩从那里装载它们。 –