Python calls VC + + dynamic link library (DLL) _python

Source: Internet
Author: User
1. The exported function of VC + + DLL is defined as the export function of standard C:
Copy Code code as follows:

#ifdef Lrdlltest_exports
#define LRDLLTEST_API __declspec (dllexport)
#else
#define LRDLLTEST_API __declspec (dllimport)
#endif

extern "C" Lrdlltest_api int Sum (int a, int b);
extern "C" Lrdlltest_api void GetString (char* pchar);

A + b
LRDLLTEST_API int Sum (int a, int b)
{
return a + B;
}

Get a string
Lrdlltest_api void GetString (char* pchar)
{
strcpy (Pchar, "Hello DLL");
}


2. The python call is as follows:
Copy Code code as follows:

From ctypes Import *

Filename= "LRDllTest.dll"
Func=cdll. LoadLibrary (FileName)
str = Create_string_buffer (20)
n = func. Sum (2, 3)
Func. GetString (str)

Print n
Print Str.raw

Some of the parameter types in the C language are as detailed as: http://www.python.org/doc/2.5/lib/node454.html

3. Output Result:
5
Hello DLL
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.