37,718
社区成员
发帖
与我相关
我的任务
分享
// dll.h
extern "C" {
struct DataHandle;
DataHandle* create_data_handle();
void destroy_data_handle(DataHandle* handle);
const char* output_string(DataHandle*, int, int*);
}
// dll.cpp
#include <stdlib.h>
#include "dll.h"
#include <string.h>
#include <string>
#include <sstream>
struct DataHandle {
std::string m_str_data;
};
DataHandle* create_data_handle() {
return new DataHandle;
}
void destroy_data_handle(DataHandle* handle) {
if (handle != NULL) {
delete handle;
}
}
const char* output_string(DataHandle* handle, int line_num, int* outlen) {
std::stringstream ss;
for (int i = 0; i < line_num; ++i) {
ss << "This is line " << i << "\n";
}
handle->m_str_data = ss.str();
*outlen = handle->m_str_data.size();
return handle->m_str_data.c_str();
}
from ctypes import *
import os
import pdb
class OutInt(Structure):
_fields_ = [('v', c_int)]
out_len = OutInt()
line_num = 50000
dll = cdll.LoadLibrary(os.getcwd()+'/dll.so')
handle = dll.create_data_handle()
data = dll.output_string(handle, line_num, byref(out_len))
print "out len : ", out_len.v
addr = create_string_buffer(out_len.v*10)
pdb.set_trace()
memmove(addr, data, out_len.v)
for i in addr.value.strip().split('\n'):
print i