Hope you're having a great day!
I have created an ExpSim.dll
file that I compiled from Visual Studio 2022 in a DLL project. Here is the main code:
#include "ExpressionSimplifier.h"
#include <Windows.h>
#include <Python.h>
exs::ExprSimplifier simplifier = exs::ExprSimplifier();
extern "C" __declspec(dllexport)
PyObject* _cdecl getSteps()
{
// some code
}
extern "C" __declspec(dllexport)
void _cdecl getSimplifiedExpressionSize(const char* expression)
{
// some code
}
extern "C" __declspec(dllexport)
const char* _cdecl getSimplifiedExpression(const char* expression)
{
// some code
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
Now in python, when I run the following code:
from ctypes import *
exp_sim = cdll.LoadLibrary("python/maths/exp_simplifier/ExpSim.dll")
exp_sim.getSteps.restype = c_char_p
exp_sim.simplifyExpression.argtypes = [c_char_p]
exp_sim.simplifyExpression.restype = py_object
result = exp_sim.simplifyExpression(b"x+y+x")
print(result)
It shows the following error at line 3:
Traceback (most recent call last):
File "e:\Python Stuff\Projects\Minisoft\python\maths\exp_simplifier\exp_simplifier.py", line 3, in <module>
exp_sim = cdll.LoadLibrary("python/maths/exp_simplifier/ExpSim.dll")
File "C:\Users\Saket\AppData\Local\Programs\Python\Python310\lib\ctypes\__init__.py", line 452, in LoadLibrary
return self._dlltype(name)
File "C:\Users\Saket\AppData\Local\Programs\Python\Python310\lib\ctypes\__init__.py", line 374, in __init__
self._handle = _dlopen(self._name, mode)
FileNotFoundError: Could not find module 'E:\Python Stuff\Projects\Minisoft\python\maths\exp_simplifier\ExpSim.dll' (or one of its dependencies). Try using the full path with constructor syntax.
Before I started using the Python API in my C++ code, the DLL seemed to work fine, but not after that. Firstly, it refused to compile my code due to it not being able to find python311_d.lib
, and after fixing that, python refuses to read my DLL.
I believe it's due to ExpSim.dll still having a dependency on python311_d.lib
, but I have no idea how to fix that in VS Code.
Any help would be appreciated. Have a good day!
My suggestion is to replace the following code with line 2 of your python code:
exp_sim = cdll.LoadLibrary("./python/maths/exp_simplifier/ExpSim.dll")
You can also see some explanations here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.