Recent

Author Topic: Using the clipboard in a windows console application  (Read 7491 times)

tobibo1

  • Newbie
  • Posts: 1
Using the clipboard in a windows console application
« on: February 23, 2017, 10:18:38 pm »
Hello,


I use Lazarus 1.6 64bit with FPC 3.0.0 on Win7home 64bit.
On Lazarus I created a console-applicaton-project and added the following source:
program test_clipboard;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  Clipbrd, interfaces;

begin
  Clipboard.AsText := '10' + #9 +'15' + sLineBreak + '12';
end.

Additionally I added the dependency to LCL and LCLBase. Otherwise the units Clipbrd  and interfaces will not be found.
So far so good. The Program is compiled and work with no errors. But the exe-file has a size of 18MB.
Is there a way to compile the code directly from the console to reduce the size of the application?

Thanks
Tobias

 

balazsszekely

  • Guest
Re: Using the clipboard in a windows console application
« Reply #1 on: February 23, 2017, 10:21:51 pm »
http://wiki.freepascal.org/Size_Matters
Basically you should switch to release mode.

ASerge

  • Hero Member
  • *****
  • Posts: 2196
Re: Using the clipboard in a windows console application
« Reply #2 on: February 26, 2017, 09:20:14 pm »
But the exe-file has a size of 18MB.
Is there a way to compile the code directly from the console to reduce the size of the application?
There is no difference to compile from the console or from Lazarus.
If you follow the guidance URL from @GetMem, then size will be about 2.4 mb.
But you can write directly to WinAPI. For example, the size of this program approximately 45 KB:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses Windows;
  4.  
  5. function AddDataToClip(Format: UINT; const Data; Size: PtrUInt): Boolean;
  6. var
  7.   HMem: THandle;
  8.   P: Pointer;
  9. begin
  10.   Result := False;
  11.   HMem := GlobalAlloc(GMEM_MOVEABLE, Size);
  12.   if HMem <> 0 then
  13.   begin
  14.     P := GlobalLock(HMem);
  15.     if P <> nil then
  16.     begin
  17.       Move(Data, P^, Size);
  18.       GlobalUnlock(HMem);
  19.       Result := SetClipboardData(Format, HMem) <> 0;
  20.     end;
  21.   end;
  22. end;
  23.  
  24. function PutTextToClipboard(const S: string): Boolean;
  25. var
  26.   W: UnicodeString;
  27. begin
  28.   Result := False;
  29.   if OpenClipboard(0) then
  30.   try
  31.     if EmptyClipboard then
  32.     begin
  33.       W := UTF8Decode(S);
  34.       Result := AddDataToClip(CF_UNICODETEXT, Pointer(W)^, SizeOf(UnicodeChar) * Length(W));
  35.     end;
  36.   finally
  37.     CloseClipboard;
  38.   end;
  39. end;
  40.  
  41. begin
  42.   PutTextToClipboard('10' + #9 +'15' + sLineBreak + '12');
  43. end.

ad1mt

  • Full Member
  • ***
  • Posts: 172
    • Mark Taylor's Home Page
Re: Using the clipboard in a windows console application
« Reply #3 on: October 18, 2023, 07:45:23 pm »
But the exe-file has a size of 18MB.
Is there a way to compile the code directly from the console to reduce the size of the application?
There is no difference to compile from the console or from Lazarus.
If you follow the guidance URL from @GetMem, then size will be about 2.4 mb.
But you can write directly to WinAPI. For example, the size of this program approximately 45 KB:
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. uses Windows;
  4.  
  5. function AddDataToClip(Format: UINT; const Data; Size: PtrUInt): Boolean;
  6. var
  7.   HMem: THandle;
  8.   P: Pointer;
  9. begin
  10.   Result := False;
  11.   HMem := GlobalAlloc(GMEM_MOVEABLE, Size);
  12.   if HMem <> 0 then
  13.   begin
  14.     P := GlobalLock(HMem);
  15.     if P <> nil then
  16.     begin
  17.       Move(Data, P^, Size);
  18.       GlobalUnlock(HMem);
  19.       Result := SetClipboardData(Format, HMem) <> 0;
  20.     end;
  21.   end;
  22. end;
  23.  
  24. function PutTextToClipboard(const S: string): Boolean;
  25. var
  26.   W: UnicodeString;
  27. begin
  28.   Result := False;
  29.   if OpenClipboard(0) then
  30.   try
  31.     if EmptyClipboard then
  32.     begin
  33.       W := UTF8Decode(S);
  34.       Result := AddDataToClip(CF_UNICODETEXT, Pointer(W)^, SizeOf(UnicodeChar) * Length(W));
  35.     end;
  36.   finally
  37.     CloseClipboard;
  38.   end;
  39. end;
  40.  
  41. begin
  42.   PutTextToClipboard('10' + #9 +'15' + sLineBreak + '12');
  43. end.

Hi ASerge,
Please can you add example code to get text from the clipboard?
Thanks.

ASerge

  • Hero Member
  • *****
  • Posts: 2196
Re: Using the clipboard in a windows console application
« Reply #4 on: October 18, 2023, 08:03:38 pm »
Please can you add example code to get text from the clipboard?
Once in 6 years they exchanged letters :)
Code: Pascal  [Select][+][-]
  1. {$APPTYPE CONSOLE}
  2.  
  3. uses Windows;
  4.  
  5. var
  6.   HClip: THandle;
  7.   P: PUnicodeChar;
  8.   U: UnicodeString = '';
  9. begin
  10.   if OpenClipboard(0) then
  11.   begin
  12.     HClip := GetClipboardData(CF_UNICODETEXT);
  13.     if HClip <> 0 then
  14.     begin
  15.       P := GlobalLock(HClip);
  16.       U := P;
  17.       GlobalUnlock(HClip);
  18.     end;
  19.     CloseClipboard;
  20.   end;
  21.   Writeln(U);
  22.   Readln;
  23. end.

 

TinyPortal © 2005-2018