Maybe you just do not know how to debug step by step?
I do step by step debugging.
I tell, you this does not show the evaluated values of funtions. I can do this in Delphi no problem. But in Lazarus is typical "identifier not found". That's why I don't see result of the length function.
Maybe you just do not know that there is a ExtractFileName() method that can be freely used?
I looked for basename(). But anyway why to extract information which is already known from the variable?
filePath := ExtractFilePath(ParamStr(i));
It's always hard decision for me because I prefer low level programming.
So even with the function ExtractFilename ... this doesn't solve the current problem what I am trying to learn. Using ExtractFilename can be fast solution, which skips the need to learn how to use length function and watch expression during debugging. You can provide alternative solution, but I need to learn that little point anyway to be more indepentent on help of others.
I am almost finished with param parsing so I will show you the code a little bit later. Now waiting for response and have to have a break.
* * *
Note:
I see I need to parse arguments in different way
procedure TForm1.ParseParams;
var
i: Integer;
Params: array of string;
begin
if ParamCount <= 0 then Exit;
SetLength(Params, ParamCount);
// copy parameters to dynamic artray
for i := 1 to ParamCount do
Params[i - 1] := ParamStr(i);
* * *
And I need to change the way I process arguments.
I will use ChatGPT advice.
You are absolutely correct. In FreePascal and its Run-Time Library (RTL), there is a function that allows you to remove the first element from an array similar to array_shift in PHP. This function is called Delete and is located in the SysUtils unit.
Here's an example of how you can use it:
uses
SysUtils;
procedure TForm1.ParseParams;
var
i: Integer;
Params: array of string;
begin
if ParamCount <= 0 then Exit;
SetLength(Params, ParamCount);
for i := 1 to ParamCount do
Params[i - 1] := ParamStr(i);
// Removing the first element from the array
if Length(Params) > 0 then
Delete(Params, 0, 1);
// Displaying the remaining elements in the array
for i := 0 to High(Params) do
Memo1.Lines.Add('Param ' + IntToStr(i) + ': ' + Params[i]);
end;
Yet I would like to call the program repetitively like so:
program name "My 1st Gallery" ... argument list of images... &
program name "My 2nd Gallery" ... next images &
program save "My Saturday Galeries" &
program exit &
// Program exited.
// Galeries saved. Let's read them to view the save images:
program read "My Saturday Galeries" &
program exit &
To add the icons and information to the existing instance of the program running in memory. Because I don't know how technically to solve that I asked of chatGPT, so you please tell me if this code it suggested is not just ton of ballshit:
uses
SysUtils, SyncObjs, Windows;
type
TSharedData = record
// You can add data here that you want to share between instances
end;
var
Mutex: TMutex;
SharedData: ^TSharedData;
Created: Boolean;
procedure InitializeSharedMemory;
begin
Mutex := TMutex.Create(nil, False, 'MySharedMemoryMutex');
Mutex.Acquire;
Created := False;
SharedData := MapViewOfFile(
CreateFileMapping(INVALID_HANDLE_VALUE, nil, PAGE_READWRITE, 0, SizeOf(TSharedData), 'MySharedMemory'),
FILE_MAP_ALL_ACCESS, 0, 0, SizeOf(TSharedData)
);
if SharedData = nil then
begin
Writeln('Could not create or access shared memory.');
Exit;
end;
if GetLastError = ERROR_ALREADY_EXISTS then
Created := True
else
FillChar(SharedData^, SizeOf(TSharedData), 0);
Mutex.Release;
end;
procedure SendDataToExistingInstance;
begin
Mutex.Acquire;
// Here you can update data in the shared memory
// or send a message to the existing instance
Mutex.Release;
end;
begin
InitializeSharedMemory;
if Created then
begin
// This is a new instance, perform normal operations
end
else
begin
// This is an already existing instance, perform communication
SendDataToExistingInstance;
end;
// Release the shared memory and mutex
UnmapViewOfFile(SharedData);
Mutex.Free;
end.
* * *
I plan to write ini file to save settings. I think I will include this as a separate unit. See attachment.