尝试用 python ctypes 和 运行 中的函数打开用 c 编写的 dll,但它以 int 形式出现,而不是字符串
Trying to open a dll written in c with python ctypes and run the function in it, but it comes as int, not a string
这些是我的示例源代码:
C
#include <stdio.h>
#include <stdlib.h>
__declspec(dllexport)
char* sys_open(char* file_name)
{
char *file_path_var = (char *) malloc(100*sizeof(char));
FILE *wrt = fopen(file_name, "r");
fscanf(wrt, "%s", file_path_var);
fclose(wrt);
return file_path_var;
}
Test.txt
test
Python
from ctypes import *
libcdll = CDLL("c.dll")
taken_var = libcdll.sys_open("test.txt")
print("VAR: ", taken_var)
结果
VAR: 4561325
所以我只是得到一个随机数。我该怎么办?
我不是 C 开发人员,但 sys_open
return 不是指针吗?上次我检查指针是 HEX 中的 WORD 大小的内存地址,所以 python 看到 HEX 中的数值并将其转换为十进制可能有意义吗?也许您想从 C 函数中 return 是 &file_path_var
我找到了真货。
python 文件错误,必须是:
from ctypes import *
libcdll = CDLL("c.dll")
taken_var = libcdll.sys_open("test.txt")
print("VAR: ", c_char_p(taken_var).value)
这些是我的示例源代码:
C
#include <stdio.h>
#include <stdlib.h>
__declspec(dllexport)
char* sys_open(char* file_name)
{
char *file_path_var = (char *) malloc(100*sizeof(char));
FILE *wrt = fopen(file_name, "r");
fscanf(wrt, "%s", file_path_var);
fclose(wrt);
return file_path_var;
}
Test.txt
test
Python
from ctypes import *
libcdll = CDLL("c.dll")
taken_var = libcdll.sys_open("test.txt")
print("VAR: ", taken_var)
结果
VAR: 4561325
所以我只是得到一个随机数。我该怎么办?
我不是 C 开发人员,但 sys_open
return 不是指针吗?上次我检查指针是 HEX 中的 WORD 大小的内存地址,所以 python 看到 HEX 中的数值并将其转换为十进制可能有意义吗?也许您想从 C 函数中 return 是 &file_path_var
我找到了真货。
python 文件错误,必须是:
from ctypes import *
libcdll = CDLL("c.dll")
taken_var = libcdll.sys_open("test.txt")
print("VAR: ", c_char_p(taken_var).value)