Looking at the declaration of ShellExecute
in the .net implementation of ShellAPI
, it is clear what to do. Stop casting to PChar
and write your code like this:
ShellExecute(0, 'open', Path, '', '', SW_SHOW);
I had not appreciated this until now, but it seems that the Windows API calls you make from Delphi.net use the same DllImport
attribute as other .net languages. I guess that makes sense and these are just normal p/invoke calls as would be found in C# interop code.
Interestingly, you report that attempts to pass nil
to one of these string parameters leads to a compiler error. That means that there's no easy way to pass the null pointer to an API function that expects a C string. What you would have to do is to use an overloaded external declaration that received a Pointer
for the parameter which you want to pass nil
to.
As an aside, the Embarcadero developers made a mistake in their DllImport
declaration. They set SetLastError = True
which is incorrect for ShellExecute
which does not set the thread last error value.