Python and CPP interface programming

Source: Internet
Author: User

(1) VC6 Build DLL Learning below

1. Generating a DLL using VC6.0
New Project "Win32 dynamic-link Library", enter the project name, select "A Simple DLL Project" and click "Finish".

The following is the auto-generated code for the CPP file:
#include "stdafx.h"
BOOL apientry DllMain (HANDLE hmodule,
DWORD Ul_reason_for_call,
LPVOID lpreserved
)
{
return TRUE;
}

To edit a CPP file:
Join in the next line of # include "StdAfx.h"
extern "C" __declspec (dllexport) int fun (int a, int b);
/*
This is the C format export function;
This kind of writing is generally used in C + + written DLL, refers to the rules of C export this function, otherwise the exported function will be very strange;
plus extern "C" means exporting functions in standard C format. If you remove only compatible C + +;
where int fun (int a, int b) This part of the code is the function prototype declaration we want to implement with the DLL
If you still want to join the other can continue to join extern "C" __declspec (dllexport) int fun1 (int a, int b);
*/

DllMain is the default entry function for a DLL, similar to the C language's main function, which does not need to be modified here, but is added after DllMain:
int fun (int a,int b)
{
return a+b;
}

This is the definition of the function we want to implement with the DLL, and the build will generate the DLL file we want in the debug directory.
2. Calling the DLL
Create a new Win32 Console application project to copy the DLL file you just generated to the project's root directory

Added in stdafx.h file: #include <windows.h>

To edit a CPP file:
#include "stdafx.h"
typedef int (*pfun) (int,int);
void Main ()
{
Hmodule hmodule =:: LoadLibrary ("Dlltest.dll");
Pfun Newfun = (pfun):: GetProcAddress (Hmodule, "fun");
int i = Newfun (n);
printf ("The result is%d\n", i);
:: FreeLibrary (hmodule);
}

Then, you run and you see the results.



Switch VC6.0 the function interfaces and global variables provided with DLL files
Function Interface:
First copy the generated DLL files (such as RegularDll.dll and RegularDll.lib) to the folder where the current project is located, there are two methods of calling:

1) Dynamic Method:
Use functions such as LoadLibrary and GetProcAddress, for example
typedef void (*lpfun) (void);
HINSTANCE hDLL;
hDLL = LoadLibrary ("RegularDll.dll");
if (Null==hdll) {
MessageBox ("Dll load failed!");
}
Lpfun Pshowdlg = (lpfun) GetProcAddress (hDLL, "Showdlg");
if (NULL = = Pshowdlg) {
MessageBox ("Load function \" showdlg\ "failed!");
}
Pshowdlg (); */

2) Static Declaration method:
In the project that creates the DLL, the function declaration and definition is _stdcall decorated with the example
void _stdcall Showdlg (void) {...}
In the file that calls the DLL's project, declare the libraries and functions in the file header, as in the following example
#pragma comment (lib, "RegularDll.lib")
void _stdcall Showdlg (void);
Direct Showdlg () on call is possible.
The above two methods in the VC6.0 debugging success, guaranteed to be available!

Some articles say such declarations can:
#pragma comment (lib, "RegularDll.lib")
void Showdlg (void);
But in the VC6.0 will not find the function Showdlg error, I do not know if I did not set it?

Global variables (access to global variables in DLL files)
In the project that created the DLL, this is declared in the header file:
Lib.h
#ifndef _lib_h
#define _lib_h
#ifdef Dll_file
extern int Dllglobalvar;
#else
extern int _declspec (dllimport) Dllglobalvar;
#endif

Define the Dll_file in the CPP file of the project that created the DLL, and then define the variable, as in the following example:
Lib.cpp
#define Dll_file
#include "Lib.h"
int Dllglobalvar; Define
BOOL apientry DllMain (HANDLE hmodule, DWORD ul_reason_for_call, LPVOID lpreserved)
{
Switch (ul_reason_for_call) {
Case Dll_process_attach:
Dllglobalvar = 100; Initialization when DLL attached
Break
......
}
int _stdcall Getglobalvar ()
{
return Dllglobalvar; Use the global variable
}

In the project that calls the DLL file, when you need to access the global variable, declare the following:
#include ". \\lib.h "
#pragma comment (lib, "DllTest.lib")
It can then be used and modified as a normal global value variable!
printf ("%d \ n", Dllglobalvar);
Dllglobalvar = 234;
printf ("%d \ n", Dllglobalvar);
Modifications affect the values used in this project, but the internal implementation principle is unclear: (

It is said that there is another way to access the global variable pointer form, but I have not tried, I hope the expert pointing!
If you have other questions, please contact the Mail! (Mailing address See announcement)

Python and CPP interface programming

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.