Python's DLL Generation method

Source: Internet
Author: User

This article is for Python to pick up the C + + DLL files for the Windows platform.

1. If you use the C language, the code is as follows and the file name is test.c.

__declspec (dllexport) int sum (int a,int b) {    return (A + b);}

If you use the C + + language, the code is as follows and the file name is Test_cpp.cpp.

#define DLLEXPORT extern "C" __declspec (DLLEXPORT) DLLEXPORT int sum (int a,int b) {    return a + b;}
Under the Windows platform, __declspec (dllexport) is required to be added.
2. The Declaration of the function can be placed in the Test.h header file (but not required):
Test.hint sum (int,int)

3. Compile the build DLL file.

In Visual Studio, the generated DLL file has 32bit and 64bit two, which needs to correspond to the Python version, otherwise the "Windowserror: [ERROR 193]%1 is not a valid Win32" error is reported as shown in.

My native Python is Python 2.7.9 (64bit), so I need to set the project property to 64-bit in Visual Studio. The setup steps are as follows, and the corresponding 64-bit DLLs are generated under the x64 directory.

Since some computers do not have C + + Run time installed, such as only 32bit Python installed, still cannot load 32-bit DLL, will report the following error, "Windowserror: [Error 126]"

Refer to http://stackoverflow.com/questions/10411709/windowserror-error-126-when-loading-a-dll-with-ctypes this link, You need to change the runtime to "MT" mode.

4.python call, the code is as follows:

From ctypes import Cdlldll = Cdll. LoadLibrary (' G:\keyphrase extraction\test\testforpython.dll ') print Dll.sum

The output results are as follows:

If there is only one DLL, how can you tell if it is a 32-bit or 64-bit DLL?

Using the DUMPBIN tool,

In Visual Studio, bring your own DUMPBIN tool, and then enter Dumpbin/headers TestForPython.dll

Machine post bit (x64), you can see that this DLL is 64 bits. If Machin is (x86), it is a 32-bit DLL.

Reference Links:

http://wolfprojects.altervista.org/dllforpyinc.php

Http://stackoverflow.com/questions/10411709/windowserror-error-126-when-loading-a-dll-with-ctypes

http://stackoverflow.com/questions/15374710/windowserror-error-193-1-is-not-a-valid-win32-application-in-python#

http://blog.csdn.net/jamestaosh/article/details/4237756

Python's DLL Generation method

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.