LINUX 如何查看JPG文件
更新时间:2022-06-13 20:06
最满意答案
1. 先新建一个名为 hello.cpp 的 C++ 源文件: Cpp代码 #include#define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT int __stdcall hello() { printf("Hello world!\n"); return 0; } 2. 编译成 dll 文件: Cpp代码 cl /LD hello.cpp 注意, 这里的参数是 /LD, 而不是 /DL。 3. 编写一个名为 hello.py 的 python 文件: Python代码 # coding: utf-8 import os import ctypes CUR_PATH = os.path.dirname( __file__) if __name__ == '__main__': print 'starting...' dll = ctypes.WinDLL(os.path.join(CUR_PATH, 'hello.dll')) dll.hello() 4. 输出为: Python代码 starting... Hello world! 需要注意的地方: 1. C++ 的 dll 中的接口函数必须以 extern "C" __declspec(dllexport) 为前缀, C 的以 __declspec(dllexport) 为前缀。 否则会报错: Python代码 Traceback (most recent call last): File "hello.py", line 12, in dll.hello() File "C:\Python25\lib\ctypes\__init__.py", line 361, in __getattr__ func = self.__getitem__(name) File "C:\Python25\lib\ctypes\__init__.py", line 366, in __getitem__ func = self._FuncPtr((name_or_ordinal, self)) AttributeError: function 'hello' not found
其他回答
这个在python的帮助里就有。例子代码也完整。参考”extending and embedding the python interpreter“, 还有"python/c api reference manual". 后面还有一段说明。 the api is equally usable from c++, but for brevity it is generally referred to as the python/c api。 不过有两个建议: 最好用c语言,而不是用c++。这主要是保持兼容性,方便使用ctypes, c++略复杂些。 另外就是建议你使用cython,这样挺容易就生成python 的ext,所以不用再为c++编译python ext发愁。 如果想在c++里调用python也容易。初始化一个interpreter,就和python一样用。通过返回变量获得结果。 另外c++与python程序数据交换还可以采用共享内存,其中有一个是基于numpy的共享内存方案挺成熟的。
相关问答
更多-
如何用python写 webservice[2022-06-11]
python是一款应用非常广泛的脚本程序语言,谷歌公司的网页就是用python编写。python在生物信息、统计、网页制作、计算等多个领域都体现出了强大的功能。python和其他脚本语言如java、R、Perl 一样,都可以直接在命令行里运行脚本程序。工具/原料 python;CMD命令行;windows操作系统 方法/步骤 1、首先下载安装python,建议安装2.7版本以上,3.0版本以下,由于3.0版本以上不向下兼容,体验较差。 2、打开文本编辑器,推荐editplus,notepad等,将文件保存成 ... -
创建 可以被python调用的 dll[2023-06-07]
如果经过stdcall声明的方法,如果不是用def文件声明的导出函数或者extern “C” 声明的话,编译器会对函数名进行修改;在编译加上extern C;这样就OK了; 另外可以在PYTHON代码里试试这样: ctypes.cdll.LoadLibrary("dllpath") -
如何用 C++ 为 Python 写 dll[2023-05-08]
1. 先新建一个名为 hello.cpp 的 C++ 源文件: Cpp代码 #include #define DLLEXPORT extern "C" __declspec(dllexport) DLLEXPORT int __stdcall hello() { printf("Hello world!\n"); return 0; } 2. 编译成 dll 文件: Cpp代码 cl /LD hello.cpp 注意, 这里的参数是 /LD, 而不是 /DL。 3. 编写一个名为 hello.py 的 py ... -
python的ctypes可以调用c++编译生成的dll文件中的类函数吗?[2023-07-17]
可以的调用,python中一般有两种方法调用DLL中的函数: 1、直接使用函数名,函数名可以用dependency walker等工具查看。(这个工具在vc或者vs的工具包中) [python] view plaincopy import ctypes dll = CTYPES.CDLL("test.dll") res = test(3, 4) 2、使用Ordinal,Ordinal可以用dependency walker等工具查看。 [python] view plaincopy import ctype ... -
用C++写的一个dll文件,里面有很多类,想用Python调用类方法,怎么做[2022-05-04]
dll文件,有两种调用标准约定。 你使用Python调用dll文件,先了解一下Python使用的dll调用标准。 -
如何用pycharm写python[2022-04-07]
点击Settings按钮>选择Project Interpreter; 可以在右侧切换Python版本; 在此之前必须将Python的路径加入系统环境变量 -
听起来就像您安装了不兼容的Python版本,或者使用错误的设置编译了DLL。 DLL和Python解释器都必须是32或64位。 Sounds like you have an incompatible version of Python installed or the DLL was compiled using the wrong settings. The DLL and the Python interpreter both have to be either 32 or 64 Bit.
-
我如何链接(C ++)与重命名的Python .lib和.dll?(How can I link (C++) with renamed Python .lib and .dll?)[2023-10-18]
MSVC通过编译指示支持此功能: #pragma comment(lib, "python25.lib"); 更多信息在MSDN 。 查看Python.h文件并修改链接的名称,如果这是你想要的。 MSVC supports this feature through pragmas: #pragma comment(lib, "python25.lib"); More info in MSDN. Look into Python.h file and modify the name of the link ... -
查看C ++ DLL的成员(View the members of a C++ DLL)[2022-07-14]
获取: http : //www.dependencywalker.com/ ,使用depends.exe打开DLL,然后在“视图”菜单中激活“Undecorate C ++ Functions”。 我主要使用它来查找依赖项,但它也暴露了DLL的入口点。 这不是万无一失的,因为暴露类的DLL不必导出其方法。 例如,纯虚方法布局足够统一,您可以将实例公开为可能是工厂函数的接口指针。 但它可能会解决您的问题。 无论如何,您需要一个依赖性walker的副本。 :) Get this: http://www.dep ... -
从Python调用的C ++ dll(C++ dll called from Python)[2022-02-25]
鉴于问题中提供的信息,解决方案是: import ctypes lib = ctypes.CDLL(r'C:\Users\toto\FIProtocol.dll') CommOpen = getattr(lib, "?CommOpen@CFIPcmd@@QAEJEJ@Z") CommOpen.argtypes = [c_byte, c_long] CommOpen.restype = c_long 现在它准备打电话: l = CommOpen(5 ,115200) 一些说明: 使用CDLL而不是Win ...