Question

I have an application that's compiled using Delphi 2006, and I want to launch an another application compiled in XE2 and pass it a parameter. I am using ShellExecute to launch the 2nd app from D2006, and the 4th parameter in ShellExecute expects PANSIChar (as the parameter passed to the launched application).

My XE2 app is not reading the parameter correctly, presumably due to the change to UNICODE strings.

Is there a way I can launch my XE2 application from my D2006 application and pass it the string as a parameter?

Was it helpful?

Solution

Your Delphi 2006 program is calling the ANSI version of ShellExecute, namely ShellExecuteA. That receives ANSI parameters.

When those arguments arrive in your Delphi XE2 program, they will be retrieved via GetCommandLine. And XE2 programs will call the Unicode version, GetCommandLineW.

But behind the scenes, Windows will have converted from ANSI to Unicode for you.

This sort of context sensitive conversion happens all the time in Windows. For example, you call SendMessageA, for WM_SETTEXT, passing a PAnsiChar. But the window is a Unicode window and so receives a PWideChar. The system has to be this way. Anything else would be anarchy.

The source of your problem is not that one program uses ANSI, and the other Unicode. Your problem is elsewhere.

Where exactly, it's impossible to tell with this information. One obvious possibility is that your argument contains spaces. Those spaces are interpreted as argument separators by the recipient, the XE2 program. Wrap your arguments in quote marks. Like this:

ShellExecute(..., '"argument with spaces"', ...);

Another possibility is that you are perhaps casting the argument to PAnsiChar when you receive it. If so, don't. Just read ParamStr(1) which is a Unicode string. Converted from ANSI for you by Windows.

I'm clearly guessing a little at the end here, but there's frankly not enough information to diagnose the fault definitively. But I can be sure that ANSI text is transparently converted to Unicode in your scenario.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top