PInvoke

Top  Previous  Next

 

"PInvoke" or "P/Invoke" are the abbreviated notations for "Platform invoke" or "Platform invocation services". Generally that's the technology to access unmanaged code from managed code. With regard to Dellpi2C# only the use of the Windows API matters. Especially the big Winapi.Windows.pas nearly completely consists in calls of functions of the Windows API, but but there are other units of the VCL/RTL too, where such calls take place.

 

It's often not easy to find the correct signatures for such calls. This is indicated by the amount of tutorials and forum questions to this subject in the internet and there even is a complete website, where a lot of users have assembled and discussed the PInvoke signatures, that they found out.

 

http://www.pinvoke.net

 

 

In Delphi the calls of external API's is managed in a systematic way. It should be possible therefore to convert the Delphi signatures to C# PInvoke signatures. At the development of Delphi2C# much time was spent to this subject and there are many API calls that are converted correctly. But unfortunately there also remain a calls, where the signatures have to be post-processed manually. In such cases pinvoke.net is a valuable source. Often there also may be a direct pendant in C# for the needed API function.

 

 

For call of a Windows API function attributes have to be specified, at least the DllImport attribute. For parameters and return values further attributes can be added, which specify the manner, how the according types are passed. This treatment is called "marshalling". The most simple case is, that the data types have a common representation in both managed and unmanaged memory. An additional attribute is necessary, if arrays of a fixed size are passed.

 

The conversion becomes more difficult when pointers have to be passed, because C# hasn't pointers at all. In such cases Delphi2C# creates adapter-functions, which are called with the pointer simulating types. Adapter functions are created too, if a buffer of characters or a string has to be retrieved from the unmanaged code.

 

The most complicated case is, that an API function uses a function pointer as parameter.



This page belongs to the Delphi2C# Documentation

Delphi2C# home  Content