Recent

Author Topic: ParamStr - maximum length  (Read 7216 times)

mtanner

  • Sr. Member
  • ****
  • Posts: 287
ParamStr - maximum length
« on: April 28, 2016, 06:54:19 pm »
I see from other questions that whether ParamStr is available is OD dependent, but confining the question to Windows (version unknown) and Linux, what is the maximum length string that can be passed?

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: ParamStr - maximum length
« Reply #1 on: April 28, 2016, 07:28:51 pm »
You can just test it.
Setup a program that expects 2 parameters, the frist a string, the second the length of the first parameter.
If the second parameter <> legnt of first parameter then set ExitCode to 1

Now write a program that executes the other program with consecutive larger first parameters (and correct second parameter of course).
(Use StringOfChar to make a string of arbitrary length).
If the other program exits with exitcode 1, then you hit the barrier.

Alternatively just see if you can google the results for Windows, Linus, OSX etc.

Bart

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: ParamStr - maximum length
« Reply #2 on: April 28, 2016, 09:34:24 pm »
I see from other questions that whether ParamStr is available is OD dependent, but confining the question to Windows (version unknown) and Linux, what is the maximum length string that can be passed?
http://www.freepascal.org/docs-html/rtl/system/paramstr.html

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: ParamStr - maximum length
« Reply #3 on: April 28, 2016, 11:43:11 pm »
Win7-64: tested with the concept described in my previous reply.
fpc 3.0.0 32-bit
I can supply a paramter of 32749 characters (+ a second of 5 characters).
Adding just 1 character more gives:
Code: [Select]
Exception:
Failed to execute  : 87
GetLastOSError = 87
De parameter is onjuist.  //probably "Invalid parameter"

B.t.w. repeatedly executing and icrementing the paramters supplied by 1 char at a time brought down my system almost completely.
I managed to shutdown (which took < 5 minutes).
Windows had do do a CHKDSK upon restart.

And before you ask, I did free the TProcess after each execute (prtected by try..finally).

Bart

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: ParamStr - maximum length
« Reply #4 on: April 29, 2016, 01:06:38 am »
Another big question: What is the encoding of ParamStr?




B.t.w. repeatedly executing and icrementing the paramters supplied by 1 char at a time brought down my system almost completely.
I managed to shutdown (which took < 5 minutes).
Windows had do do a CHKDSK upon restart.

And before you ask, I did free the TProcess after each execute (prtected by try..finally).

 :D :o :D

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: ParamStr - maximum length
« Reply #5 on: April 29, 2016, 03:23:51 am »
@mtanner:
what is the maximum length string that can be passed?

According to Windows' CreateProcess documentation, about lpCommandLine:
Quote
The maximum length of this string is 32,768 characters, including the Unicode terminating null character. If lpApplicationName is NULL, the module name portion of lpCommandLine is limited to MAX_PATH characters.

lpCommandLine points at a string the contains both the application and its parameters.

Based on that I guess Bart's test had an executable file name+path ~ 17 character length.



@BeniBela:
Another big question: What is the encoding of ParamStr?

I guess it is ANSI because FPC 3.0.0 uses GetCommandLineA, by default, which returns pchar.

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: ParamStr - maximum length
« Reply #6 on: April 29, 2016, 10:15:48 am »
I guess it is ANSI because FPC 3.0.0 uses GetCommandLineA, by default, which returns pchar.
Depends on the mode. If mode  delphiunicode is specified it is GetCommandlineW. Afaik that mode made it into 3.0.0.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

Bart

  • Hero Member
  • *****
  • Posts: 5290
    • Bart en Mariska's Webstek
Re: ParamStr - maximum length
« Reply #7 on: April 29, 2016, 12:16:59 pm »
Another big question: What is the encoding of ParamStr?

CP_OEM (which on (almost) all Windows is a single byte encoding)

Bart

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: ParamStr - maximum length
« Reply #8 on: April 29, 2016, 01:27:48 pm »
CP_OEM (which on (almost) all Windows is a single byte encoding)

Bart
Windows is a UTF16 based OS... Ever since NT4. It has a translation layer for legacy encodings.
The one byte encodings are based around CP_OEM to be as close as to what a user expects on his particular installed windows system. It is not lossless, though, under any and all circumstances, due to the nature of one byte encoded strings and its associated codepages.

So for single byte string systems it indeed CP_OEM, but in unicode mode paramstr returns UTF16. Which is the native windows format.
« Last Edit: April 29, 2016, 01:30:06 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: ParamStr - maximum length
« Reply #9 on: June 04, 2016, 01:40:32 am »
So GetCommandlineW is the way to go?

However,
Code: [Select]
{$mode objfpc}{$H+}

uses
  windows,FPCAdds,LazUTF8;
begin                 
  writeln('>string>',string(GetCommandLineW));
  writeln('>utf8string>',utf8string(GetCommandLineW));


with "string" it prints '?' for non-ascii characters.


Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: ParamStr - maximum length
« Reply #10 on: June 04, 2016, 08:49:36 am »
with "string" it prints '?' for non-ascii characters.
Then test this:
Code: Pascal  [Select][+][-]
  1. program Project9;
  2. {$ifdef fpc}{$mode delphiunicode}{$H+}{$endif}
  3. {$apptype console}
  4. uses
  5.   windows;
  6. begin
  7.   writeln('>string>',string(GetCommandLine));
  8.   writeln('>utf8string>',utf8string(GetCommandLine));
  9.   readln;
  10. end.
Note that you have to have a capable font selected in your terminal window. But that is basics so I expect you know how to select a proper font.
« Last Edit: June 04, 2016, 08:51:56 am by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: ParamStr - maximum length
« Reply #11 on: June 04, 2016, 11:43:52 am »
Another big question: What is the encoding of ParamStr?

In an unicode mode (Delphiunicode) it is unicodestring (utf16). Always.




BeniBela

  • Hero Member
  • *****
  • Posts: 906
    • homepage
Re: ParamStr - maximum length
« Reply #12 on: June 04, 2016, 12:31:35 pm »
Then test this:
Code: Pascal  [Select][+][-]
  1. program Project9;
  2. {$ifdef fpc}{$mode delphiunicode}{$H+}{$endif}
  3. {$apptype console}
  4. uses
  5.   windows;
  6. begin
  7.   writeln('>string>',string(GetCommandLine));
  8.   writeln('>utf8string>',utf8string(GetCommandLine));
  9.   readln;
  10. end.
Note that you have to have a capable font selected in your terminal window. But that is basics so I expect you know how to select a proper font.

But I want to use $mode objfpc

And Lazarus brings fpcadds

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11453
  • FPC developer.
Re: ParamStr - maximum length
« Reply #13 on: June 04, 2016, 01:10:36 pm »
Another big question: What is the encoding of ParamStr?

In an unicode mode (Delphiunicode) it is unicodestring (utf16). Always.

Hmm, but its implementation sucks :-)

Thaddy

  • Hero Member
  • *****
  • Posts: 14373
  • Sensorship about opinions does not belong here.
Re: ParamStr - maximum length
« Reply #14 on: June 04, 2016, 02:33:55 pm »
@Marco:
 why and where? Can you provide a small example as well?
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018