using platform invoke with delphi 2005 this topic describes the basic techniques of using unmanaged apis from delphi 2005. some of the common mistakes and pitfalls are pointed out, and a quick reference for translating delphi data types is provided. this topic does not attempt to explain the basics of platform invoke or marshaling data. please refer to the links at the end of this topic for more information on platform invoke and marshaling. understanding attributes and how they are used is also highly recommended before reading this document. the win32 api is used for several examples. for further details on the api functions mentioned, please see the windows platform sdk documentation. the following topics are discussed in this section:
calling unmanaged functions when calling unmanaged functions, a managed declaration of the function must be created that represents the unmanaged types. in many cases functions take pointers to data that can be of variable types. one example of such a function is the win32 api function systemparametersinfo that is declared as follows: bool systemparametersinfo(
uint uiaction, // system parameter to retrieve or set
uint uiparam, // depends on action to be taken
pvoid pvparam, // depends on action to be taken
uint fwinini // user profile update option
);
depending on the value of uiaction, pvparam can be one of dozens of different structures or simple data types. since there is no way to represent this with one single managed declaration, multiple overloaded versions of the function must be declared (see ), where each overload covers one specific case. the parameter pvparam can also be given the generic declaration
unmanaged data type
managed data type
input parameter
output parameter
pointer to string (pchar)
string
stringbuilder
untyped parameter/buffer
tbytes
tbytes
pointer to structure (prect)
const trect
var trect
pointer to simple type (pbyte)
const byte
var byte
pointer to array (pinteger)
array of integer
array of integer
pointer to pointer type (^pinteger)