然后用python生成dll,c调试,VS生成dll后,用LoadLibraryA会报错1411

问题描述 投票:0回答:0

dll和调用文件都是release版本

操作系统:win11 IDE:visual studio 2022 畅达环境:python=3.6 cython=0.29.33 命令生成c文件 cython demo.pyx -3

演示.pyx

cdef public int add( int str1, int str2):
    return int(str1) + int(str2)

生成dll文件代码

#include <Windows.h>
#include <Python.h>
#include "demo.h"

extern "C" _declspec(dllexport)  int  p_add(int a, int b);

int p_add(int a, int b)
{
    int p = add(a, b);
    return p;
}
    


BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        Py_Initialize();
        PyInit_demo();
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        Py_Finalize();
        break;
    }
    return TRUE;
}


回调文件代码

#include <iostream>
#include <Windows.h>

typedef int(*fun)(int, int);

int main()
{   
    fun funName = NULL;
    HMODULE hMoudle =LoadLibraryA("D:\\stduioProject\\python_gen_dll\\x64\\Release\\python_gen_dll.dll");
    {
        FreeLibrary(hMoudle);
        std::cout << GetLastError();  // error code 1411
        return -1;
    }
    funName = (fun)GetProcAddress(hMoudle, "p_add");
    if (funName == NULL)
    {
        printf("error");
        FreeLibrary(hMoudle);
        return -10;
    }
    int f = funName(2, 3);
    std::cout << f << std::endl;
    FreeLibrary(hMoudle);
}

我期待返回答案5,但我不知道该怎么做。

我把 python3.8 降到 3.6 测试,还是有同样的问题

python-3.x visual-c++ dll
© www.soinside.com 2019 - 2024. All rights reserved.