Recent

Author Topic: Start the problem of Chinese garbled characters in delphi program with parameter  (Read 1745 times)

jianwt

  • Full Member
  • ***
  • Posts: 164
Please ask a question: I need to start a delphi program with parameters in a lazarUS program, but passing parameters containing Chinese characters from lazarUS to a delphi program (for example, 程序123), Chinese characters will be garbled. How to code so that the parameters passed between two programs are not garbled? Thank you!!

Code: Pascal  [Select][+][-]
  1.  
  2. 1.{Project code in  lazarUS}
  3.  
  4. {Please ask a question: I need to start a delphi program with parameters in a lazarUS program, but passing parameters containing Chinese characters from lazarUS to a delphi program (for example, 程序123), Chinese characters will be garbled. How to code so that the parameters passed between two programs are not garbled? Thank you!!}
  5.                
  6. procedure Tfrm_xw.BitBtn2Click(Sender: TObject);
  7. var
  8.   sFileName, sParam, sFilePath: string;
  9. begin
  10.    {$ifdef windows}
  11.   sFileName := ExtractFilePath(Application.ExeName) + '\sys123.exe';
  12.   sFilePath := 'ExtractFilePath(Application.ExeName)';
  13.   sParam := 'HosfeiDSf3se51d  程序123  {BE5EB793-6D96-44F8-B194-0BC3111DAF51} ';
  14.   ShellExecute(0, 'Open', PChar(sFileName), PChar(sParam),PChar(sFilePath), SW_SHOW);  
  15.     {$endif}
  16. end;    
  17.  
  18. 2.{sys123.exe code in delphi  }
  19. program E1;
  20.  
  21. uses
  22.   Forms,Dialogs,SysUtils,
  23.   EndM1 in 'EndM1.pas' {Form1};
  24.  
  25. {$R *.res}
  26.  
  27. begin
  28.   Application.Initialize;
  29.   Application.CreateForm(TForm1, Form1);
  30.   if ParamStr(2)<>'程序123' then
  31.   begin
  32.     ShowMessage('Missing parameter:'+ParamStr(2));
  33.     Application.Terminate;
  34.     Exit;
  35.   end;
  36.   Application.Run;
  37. end.                                  
  38.  
  39.  
               

wp

  • Hero Member
  • *****
  • Posts: 13328
Use the function ParamstrUTF8 (in LazUTF8) rather than ParamStr:

Code: Pascal  [Select][+][-]
  1.   if ParamStrUTF8(2)<>'程序123' then
  2.   begin
  3.     ShowMessage('Missing parameter:'+ParamStrUTF8(2));
  4.     Application.Terminate;
  5.     Exit;
  6.   end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
- Test who garbles it, the Lazarus program or the delphi program by temporary replacing the delphi program with a lazarus one.

- Optionally in the manifest screen of the lazarus application set the utf8 tick.  This will limit to windows 10, but even if that is a problem it could give you some pointers which side the problem is on

jianwt

  • Full Member
  • ***
  • Posts: 164
Thank you very much for your help!

jianwt

  • Full Member
  • ***
  • Posts: 164
@wp  marcov

Thank you for your help! But I couldn't get this thing right, maybe I didn't understand what I was trying to say with Google Translate. I want to use lazarUS to run a delphi compiled EXE program with run parameters (sys123.exe),
delphi under the code is as follows(delphi does not have ParamStrUTF8):
Code: Pascal  [Select][+][-]
  1. program E1;
  2.  
  3. uses
  4.   Forms,Dialogs,SysUtils,
  5.   EndM1 in 'EndM1.pas' {Form1};
  6.  
  7. {$R *.res}
  8.  
  9. begin
  10.   Application.Initialize;
  11.   Application.CreateForm(TForm1, Form1);
  12.   if ParamStr(2)<>'程序123' then
  13.   begin
  14.     ShowMessage('Missing parameter:'+ParamStr(2));
  15.     Application.Terminate;
  16.     Exit;
  17.   end;
  18.   Application.Run;
  19. end.    
  20.  
         
////////////////////////////
Click the button under lazarUS to run the "sys123.exe" external program with the following code:
Code: Pascal  [Select][+][-]
  1. procedure Tfrm_xw.BitBtn2Click(Sender: TObject);
  2. var
  3.   sFileName, sParam, sFilePath: string;
  4. begin
  5.    {$ifdef windows}
  6.   sFileName := ExtractFilePath(Application.ExeName) + '\sys123.exe';
  7.   sFilePath := 'ExtractFilePath(Application.ExeName)';
  8.   sParam := 'HosfeiDSf3se51d  程序123  {BE5EB793-6D96-44F8-B194-0BC3111DAF51} ';
  9.   ShellExecute(0, 'Open', PChar(sFileName), PChar(sParam),PChar(sFilePath), SW_SHOW);  
  10.     {$endif}
  11. end;    
  12.  

But will the compiled characters in sys123.exe obtained under lazarUS be garbled?
I just want to realize that the Chinese parameters passed between the two are not garbled, thank you!!
« Last Edit: July 19, 2023, 03:28:36 am by jianwt »

wp

  • Hero Member
  • *****
  • Posts: 13328
When you want to pass strings to Windows which contain characters outside your codepage you must use the "W" variants of the system functions and provide strings as WideStrings or UnicodeStrings:

Code: Pascal  [Select][+][-]
  1. uses
  2.   Windows;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. var
  6.   sFileName, sParam, sFilePath: UnicodeString;
  7. begin
  8.   sFileName := UTF8Decode(ExtractFilePath(Application.ExeName) + 'sys123.exe');
  9.   sFilePath := UTF8Decode(ExtractFilePath(Application.ExeName));
  10.   sParam := UTF8Decode('HosfeiDSf3se51d  程序123  {BE5EB793-6D96-44F8-B194-0BC3111DAF51} ');
  11.   ShellExecuteW(0, 'Open', PWideChar(sFileName), PWideChar(sParam), PWideChar(sFilePath), SW_SHOW);
  12. end;

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12593
  • FPC developer.
I recommended temporarily replacing the delphi program for a Lazarus program that supports unicode.

That way you know you can fix it in the calling program, and it is not the called program's problem.

jianwt

  • Full Member
  • ***
  • Posts: 164
@WP

Achieved your way, thanks again!!

 

TinyPortal © 2005-2018