Special warning about wb_call_function() This function provides access to the Windows API and should be used with great care. This function can easily generate a runtime exception or freeze the PHP application if you omit or mismatch a parameter, or if you add an extra parameter to the array. Worse, if you are using Windows 98 or Me, it is not unlikely to crash the whole system when calling this function. For this reason Windows 98 or Windows Me are not recommended for developing WinBinder applications. Always be careful and double-check your source code before running it when using this function. |
int wb_call_function (int address [, array args])
Calls the DLL function pointed by address. args is an optional array of parameters that must match those of the function being called. Returns an integer that may be a valid value or a pointer to one object, according to the library function called.
NOTE: Function arguments are limited to a maximum of 20.
You must perform the following steps to use a foreign function with WinBinder:
The pack() function should be used to pass a structure to a foreign function. Similarly, the unpack() function can retrieve structure fields as an associative array. Some common Windows types and their corresponding format characters for pack() / unpack() are listed in the table below.
Windows |
ANSI C |
pack() |
bytes |
---|---|---|---|
BYTE |
unsigned char |
|
1 |
DWORD |
unsigned long |
|
4 |
HANDLE¹ |
void * |
|
4 |
LONG |
long |
|
4 |
LPARAM |
long |
|
4 |
LPTSTR, LPCTSTR |
unsigned short * |
|
4 |
LRESULT |
long |
|
4 |
UINT |
unsigned int |
|
4 |
WORD |
unsigned short |
|
2 |
WPARAM |
unsigned int |
|
4 |
Up to three constants are needed for each structure. The best way is to define them in advance to improve code readability. For example, the Windows API documentation defines a MEMORYSTATUS structure that is passed to the GlobalMemoryStatus() function to retrieve information about the system memory. The definition is as follows:
typedef struct _MEMORYSTATUS {
DWORD
dwLength; // sizeof(MEMORYSTATUS)
DWORD dwMemoryLoad; //
percent of memory in use
DWORD dwTotalPhys;
// bytes of physical memory
DWORD
dwAvailPhys; // free physical memory bytes
DWORD dwTotalPageFile; // bytes of paging
file
DWORD dwAvailPageFile; // free
bytes of paging file
DWORD dwTotalVirtual;
// user bytes of address space
DWORD
dwAvailVirtual; // free user bytes
} MEMORYSTATUS;
Because the structure consists of eight DWORDs, we can create a constant to format the structure like this:
define("MEMORYSTATUS_RAW", "VVVVVVVV");
or
define("MEMORYSTATUS_RAW", "V8");
This is enough to pass the structure to the function. To retrieve the structure, however, we need a different structure so that unpack() function can return an associative array with the field names:
define("MEMORYSTATUS", "Vlen/Vmemload/Vphys/Vavailphys/Vpagefile/Vavailpagefile/Vvirtual/Vavailvirtual")
Finally, because we must also pass the size of the structure, we define another constant:
define("MEMORYSTATUS_SIZE", 8
* 4);
The example below shows how to create a new PHP function that calls a Windows function.
|
The mini-program below shows how to call a Windows function in a single PHP line.
|
wb_get_function_address
wb_load_library
Low-level functions