在python中将int转换为int*

Converting int to int* in python

本文关键字:int 转换 中将 python      更新时间:2023-10-16

我们正在使用SWIG与Python一起测试c++ API。c++中的一种方法期望int *int fnClicTestIntPtr(int *);它只是在返回值上加1。我试图在Python中调用这个方法并测试输出。你能告诉我如何在python中通过int*吗?

我的接口文件是这样的:

   %module ClicTest
   %{
   #include "ClicTest.h"
   %}
   %include <windows.i>
   %include "ClicTest.h"

头文件:

  #ifdef CLICTEST_EXPORTS
  #define CLICTEST_API __declspec(dllexport)
  #else
  #define CLICTEST_API __declspec(dllimport)
  #endif
 // This class is exported from the ClicTest.dll
 class CLICTEST_API CClicTest {
 public:
CClicTest(void);
// TODO: add your methods here.
  };
 extern CLICTEST_API int nClicTest;
 CLICTEST_API int fnClicTest(void);
 CLICTEST_API int fnClicTestInt(int);
 CLICTEST_API char* fnClicTestChar(char *);
 CLICTEST_API int fnClicTestIntPtr(int *);
 CLICTEST_API int fnClicTestVoidPtr(void *);
 CLICTEST_API char* fnClicTestCharPtr(char *);
 CLICTEST_API long fnClicTestLongPtr(long *);

我试着传递一个数字,也试着使用ctypes。Byref和ctypes。指针的方法。但一切都没有成功。像下面的

Py文件:

 print(ClicTest.fnClicTestLongPtr(ctypes.pointer(num)))
 print(ClicTest.fnClicTestLongPtr(num))
 print(ClicTest.fnClicTestLongPtr(ctypes.byref(num)))

Error : TypeError: in method 'fnClicTestIntPtr', argument 1 of type 'int *

我们必须包含" cpointer "。接口文件中的i "接口(使用SWIG将c++连接到Python)。详细信息请参阅http://www.swig.org/Doc2.0/Library.html#Library