Zac*_*chR 4 c++ python string dll ctypes
我正在尝试使用 python 中的 ctypes 从 dll 调用 C++ 函数。我当前的问题是该函数似乎返回一个大整数,无论是正数还是负数,而不是我期望的 char 指针。如果我将该 int 转换为 c_char_p 并对其调用 .value,它每次都会杀死我的内核。我已经查看了整个站点和文档,但无法弄清楚这一点。我在这个网站上看到的很多东西甚至会为我抛出错误,比如将字符串传递给 ctypes 对象和函数,而它们应该是字节对象或类似的东西。下面是我的 C++ 代码,它变成了一个 dll,以及我用来从 dll 调用函数的 python 代码。请如果有人可以帮助我,那就太棒了。SaySomething 是有问题的函数。谢谢。
#pragma once
#ifdef TESTLIBRARY_EXPORTS
#define TESTLIBRARY_API __declspec(dllexport)
#else
#define TESTLIBRARY_API __declspec(dllexport)
#endif
#include <windows.h>
#include <cstring>
#ifdef __cplusplus
extern "C"
{
#endif
TESTLIBRARY_API char* SaySomething(const char* phrase);
#ifdef __cplusplus
};
#endif
Run Code Online (Sandbox Code Playgroud)
#include "stdafx.h"
#include "TestLibrary.h"
#include <iostream>
TESTLIBRARY_API char* SaySomething(const char* phrase)
{
char* p = new char [100];
p = "string something";
return p;
}
Run Code Online (Sandbox Code Playgroud)
import ctypes
dbl = ctypes.c_double
pChar = ctypes.c_char_p
pVoid = ctypes.c_void_p
libName = (r"D:\Documents\Coding Stuff\Visual Studio 2017\Projects\TestLibrary"
r"Solution\x64\Debug\TestLibrary.dll")
x = ctypes.windll.LoadLibrary(libName)
x.SaySomething.argtypes = [pChar]
x.SaySomething.restypes = pChar
phrase = b"Hi"
phrase = pChar(phrase)
res = x.SaySomething(phrase)
Run Code Online (Sandbox Code Playgroud)
虽然您可以制作一个 API 来完成您想要做的事情,但您目前会遇到内存泄漏。更好的解决方案是让 Python 为结果分配和管理内存。
我还修复了dllimport
注释中提到的并TESTLIBRARY_EXPORTS
在 .cpp 文件中定义的问题,以便该函数可以从 DLL 中导出。 restype
也被修复了。
测试库.h
#pragma once
#ifdef TESTLIBRARY_EXPORTS
#define TESTLIBRARY_API __declspec(dllexport)
#else
#define TESTLIBRARY_API __declspec(dllimport)
#endif
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#ifdef __cplusplus
extern "C" {
#endif
TESTLIBRARY_API char* SaySomething(const char* phrase, char* result, size_t resultMaxLength);
#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)
测试库.cpp
#define TESTLIBRARY_EXPORTS
#include "TestLibrary.h"
#include <stdio.h>
TESTLIBRARY_API char* SaySomething(const char* phrase, char* result, size_t resultMaxLength)
{
_snprintf_s(result,resultMaxLength,_TRUNCATE,"Decorated <%s>",phrase);
return result;
}
Run Code Online (Sandbox Code Playgroud)
tester2.py
import ctypes
libName = (r"TestLibrary.dll")
x = ctypes.CDLL(libName)
x.SaySomething.argtypes = [ctypes.c_char_p,ctypes.c_char_p,ctypes.c_size_t]
x.SaySomething.restype = ctypes.c_char_p
phrase = b"Hi"
result = ctypes.create_string_buffer(100)
res = x.SaySomething(phrase,result,ctypes.sizeof(result))
print(res)
print(result.value)
Run Code Online (Sandbox Code Playgroud)
输出
b'Decorated <Hi>'
b'Decorated <Hi>'
Run Code Online (Sandbox Code Playgroud)
result
当没有更多引用时,Python 将自动释放缓冲区。
归档时间: |
|
查看次数: |
3925 次 |
最近记录: |