Special Cases

There are cases where a StringBuilder and even the Marshal class will be unable to correctly handle the data that needs to be passed to an unmanaged function. An example of such a case is when the string you need to pass, or receive, contains multiple strings separated by null characters. Since the default marshaler will consider the first null to be the end of the string, the data will be truncated (this also applies to the StringToHGlobalXXX and PtrToStringXXX methods). In this situation TBytes can be used (using the PlatformStringOf and PlatformBytesOf functions in Borland.Delphi. System to convert the byte array to/from a string). Note that these utility functions do not add or remove terminating null characters.

When working with COM interfaces, the UnmanagedType enumeration (used by the MarshalAsAttribute class) has a special value, LPStruct. This is only valid in combination with a System.Guid class, causing the marshaler to convert the parameter into a Win32 GUID structure. The function CoCreateInstance that is declared in Delphi 7 as:

function CoCreateInstance([MarshalAs(UnmanagedType.LPStruct)] clsid: TCLSID;

  • MarshalAs(UnmanagedType.IUnknown)] unkOuter: TObject; dwClsContext: Longint;
  • MarshalAs(UnmanagedType.LPStruct)] iid: TIID; [MarshalAs(UnmanagedType.Interface)] out pv ): HResult;

This is currently the only documented use for UnmanagedType.LPStruct.

Project Management Made Easy

Project Management Made Easy

What you need to know about… Project Management Made Easy! Project management consists of more than just a large building project and can encompass small projects as well. No matter what the size of your project, you need to have some sort of project management. How you manage your project has everything to do with its outcome.

Get My Free Ebook

Post a comment