C. Compile the dynamic link library PHPDLL new-project-win32DynamicLinkLibrary-& gt; a simple dll project is only marked before the function: extern ?? "C "?? _ Declspec (dllexport) function can be written by C dynamic link library PHP DLL
Create-project-win32 Dynamic Link Library-> a simple dll project
Only note: extern? ? "C "? ? _ Declspec (dllexport) functions can be called by other programs through dll
extern "C" _declspec(dllexport) int add(int i1,int i2){return i1+i2;}
(1) Static link declaration:
#pragma comment(lib, "dll1.lib")extern "C" _declspec(dllexport) int add(int i1,int i2);
? Then you can call it like calling a common function. If there are many functions in the dll, The. h file is generally developed by the dll developer, and the declaration of the function is written and called by others, as long as the include function is used.
(2) dynamic link:
// There are different call conventions such as _ cdecl _ stdcall, that is, the pressure stack sequence of parameters. you do not need to worry about them for the moment, you only need to ensure that the call is the same as the call Convention in the dll.
Declare the function pointer typedef int (_ cdecl *? FunctionAdd) (int, int );
# Include "stdafx. h "typedef int (_ cdecl * FunctionAdd) (int, int); HMODULE hModule; // declare handle FunctionAdd add; // declare pointer hModule = LoadLibrary (" dll1.dll "); // when debugging, the hModule is 0x10000000 If (NULL = hModule) {// error .} add = (FunctionAdd) GetProcAddress (hModule, "add"); If (NULL = add) {// error} int r = add (1, 1); FreeLibrary (hModule ); // release the handle
The difference between static link and dynamic link: When the program starts, the static link checks whether the dll exists. if the dll does not exist, an error is reported and the program cannot be started; dynamic Links check whether a dll exists only when it runs, and the programmer can determine the logic when the dll does not exist.
Distinguish static and dynamic databases. Static call dll and Dynamic Call dll.
?
3. plug-in mechanism.
Encapsulate the repetitive code frequently used in your programming into a dll. load your new dll when the program starts to provide new functions, such as PHP ext dll.
?
Manually compile php dll extensions in windows)
Prerequisites: the php extension skeleton is OK.
1. php source code package and binary package in windows, install Visual C ++, and add the absolute path of Microsoft Visual Studio \ Common \ MSDev98 \ Bin to the windows environment variable
2. decompress the source code package to d: \ php_src.
3. enter the d: \ php_src \ ext source package directory, copy the skeleton folder, and rename it the name of the extension to be developed. In this example, the name is "linvo"
4. Upload The php5ts. lib file in the dev directory of the binary package to the new linvo directory.
5. rename php_skeleton.h to php_linvo.h and skeleton. c is linvo. c, skeleton. dsp is linvo. DSPS: edit php_linvo.h and linvo in the linvo directory. c. linvo. the dsp files replace all extnames with linvo and extname with LINVO. (Case sensitive)
6. edit the php_linvo.h File (header file)
Write PHP_FUNCTION (hello); declare a hello function in PHP_FUNCTION (confirm_linvo_compiled );
7. edit the linvo. c File (main file)
In PHP_FE (confirm_linvo_compiled ,??? NULL) written below
PHP_FE (hello ,??? NULL)
This is the function entry. the Function subject should be written below.
Find the PHP_FUNCTION (confirm_linvo_compiled) function, which is a test function and writes a new function after the function.
PHP_FUNCTION(hello){ char *arg = NULL; int arg_len, len; char *strg; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &arg, &arg_len) == FAILURE) { return; } len = spprintf(&strg, 0, "Hello,%s", arg); RETURN_STRINGL(strg, len, 0);}
?
Compile:
8. run the cmd command line to enter the d: \ php_src \ ext \ linvo directory.
9. enter msdev linvo. dsp/MAKE "linvo-Win32 Release_TS"
10. if there is no error, a Release_TS folder will be generated under the php_src directory, which contains the compiled php_linvo.dll extension.
?
Usage:
11. export it to the php extension directory ext in the runtime environment
12. edit php. ini to add extension = php_linvo.dll and restart apache.
13. execute the following statement in the PHP file:
Echo hello ('linvo ');
Output
Hello, Linvo
14. echo phpinfo (); can be used to view the extended information.
?
Open the project file through VC6 to compile the DLL, php_stream_transport.h returns an error
D: \ php_src \ main \ streams \ php_stream_transport.h. you must add the typedef int socklen_t to the file to compile