* * *

Author Topic: Using the clipboard in a windows console application  (Read 507 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

 

GetMem

  • Hero Member
  • *****
  • Posts: 1990
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

  • Sr. Member
  • ****
  • Posts: 268
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.

 

Recent

Get Lazarus at SourceForge.net. Fast, secure and Free Open Source software downloads Open Hub project report for Lazarus