The following piece of code doesn’t make an application to run. Can some one please point out what could be the problem?
procedure TTestForm1.DR_DBA_StartAppButtonClick(Sender: TObject);
var
StartInfo: TStartupInfo;
ProcInfo: TProcessInformation;
filename : String;
sa : TSecurityAttributes;
sd : TSecurityDescriptor;
begin
InitializeSecurityDescriptor( @sd, SECURITY_DESCRIPTOR_REVISION );
SetSecurityDescriptorDacl( @sd, true, nil, false);
sa.nLength := sizeof( sa );
sa.lpSecurityDescriptor := @sd;
sa.bInheritHandle := true;
// start app process
ZeroMemory(@StartInfo, SizeOf(TStartupInfo));
StartInfo.cb := SizeOf(TStartupInfo);
if not
CreateProcess (nil, PChar(SF_AppPathBox.Text), @sa, @sa, False,
PROCESS_VM_WRITE or PROCESS_VM_OPERATION, nil, nil, StartInfo, ProcInfo) then
begin
showmessage ('Cannot Start App');
exit;
end;
end;
This code ran perfectly well for an older alpha build with different UI but now after the implementation of a new UI design, it doesn’t.
The GetLastError function returns an error code of 2, the system cannot find the file specified.
The path defined is correct as it is extracted from the installdir registry entry made by the application. I have also tried manually including it but to no avail.
'X:\App\PB 0.93\PB.exe'
I am using DxScene v4.42 for UI design and the path is extracted from TVxTextBox. Putting in constant path works but not from the text box although both are system strings. I have compared both strings with each other through CompareStr which resulted in a complete match.
I am working on Windows 7 64/32bit and Windows XP SP2/3 32bit.
Findings
DxScene components inherently use unicode strings which are incompatible with createprocess procedure.
So I stored the required path strings first in normal strings and then passed them as parameter to create process which worked.
It appears that you have a mismatch between Unicode and Ansi text. You are using UI controls that use Unicode, but are compiling in an Ansi Delphi. So you are passing an array of 16 bit characters to a function that expects 8 bit encoded text. Which explains the error code that is reported.
It’s possible that the code worked for the original developers because they used a modern Unicode Delphi. If that is the case then your best course of action is to compile the code with the version of Delphi in which it was originally compiled.
If you must use an Ansi Delphi then you can fix your code by passing
to CreateProcess. That will have the effect of converting from Unicode to Ansi.
You could alternatively call the Unicode version of CreateProcess like this: