简体   繁体   English

在python中将int转换为int *

[英]Converting int to int* in python

We are using SWIG to test C++ API with Python. 我们正在使用SWIG使用Python测试C ++ API。 One of the method in C++ expects int * as int fnClicTestIntPtr(int *) ; C ++中的一种方法期望int *int fnClicTestIntPtr(int *) ; It just adds one at returns value. 它只是在返回值上加一个。 I am trying to call this method in Python and test the output. 我试图在Python中调用此方法并测试输出。 Can you please tell me how can i pass int* in Pyhton. 您能告诉我如何在Pyhton中传递int*吗?

My interface file looks like this: 我的界面文件如下所示:

   %module ClicTest
   %{
   #include "ClicTest.h"

   %}
   %include <windows.i>
   %include "ClicTest.h"

Header file: 头文件:

  #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 *);

I tried passing a number , also tried using ctypes.byref and ctypes.pointer methods. 我尝试传递一个数字,也尝试使用ctypes.byref和ctypes.pointer方法。 But nothing has been successfull.Like below 但是没有成功。

Py file: 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 *

We had to include “cpointer.i” interface in the interface file (connecting C++ to Python using SWIG). 我们必须在接口文件中包含“ cpointer.i”接口(使用SWIG将C ++连接到Python)。 Refer to http://www.swig.org/Doc2.0/Library.html#Library for more details 有关更多详细信息,请参阅http://www.swig.org/Doc2.0/Library.html#Library

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM