m19983529326
2020-04-14 19:48
采纳率: 100%
浏览 646

用C++写了一个类,封装成dLL,python调用报错,如何解决?

我是一个学c++的学生, 写了个dll给python调用,报错win1114
class.h

#ifndef __CLASS_H__
#define __CLASS_H__
class a
{
    public:
        int fac(int z);
        void setx(int z);
    private:
         int x;
};
#endif

class.cpp

#include "class.h"
#include <windows.h>
#include  <cstdio>
int a::fac(int z)
{
    if(z == 1)
    return 1;
    return z*fac(z-1);
}
void a::setx(int z)
{
    x = z;
}

interface.h

#ifndef _DLL_H_
#define _DLL_H_
#define DLLIMPORT  __declspec(dllexport) 
extern"C"
{
    #include "class.h"
}
extern "C"
{
    DLLIMPORT int fac(a* A,int z);
    DLLIMPORT void setx(a* A,int z);
    DLLIMPORT a* makea();   
}
#endif

interface.cpp

#include"interface.h"
int fac(a* A,int z)
{
    return A->fac(z);
} 
void setx(a* A,int z)
{
     A->setx(z);
}
a* makea()
{
    return new a();
}

然后用python调用
test.py

from ctypes import *
c_lib = CDLL('E:/dlltest/Project3.dll')

报错

D:\python\python.exe C:/Users/mcc2018/PycharmProjects/untitled1/test.py
Traceback (most recent call last):
  File "C:/Users/mcc2018/PycharmProjects/untitled1/test.py", line 2, in <module>
    c_lib = CDLL('E:/dlltest/Project3.dll')
  File "ctypes\__init__.py", line 369, in __init__
OSError: [WinError 1114] 动态链接库(DLL)初始化例程失败。

Process finished with exit code 1

请各位前辈大工程师们帮帮我

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

相关推荐 更多相似问题