Forum > Windows

Using the clipboard in a windows console application

(1/1)

tobibo1:
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:
http://wiki.freepascal.org/Size_Matters
Basically you should switch to release mode.

ASerge:

--- Quote from: tobibo1 on February 23, 2017, 10:18:38 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?

--- End quote ---
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; uses Windows; function AddDataToClip(Format: UINT; const Data; Size: PtrUInt): Boolean;var  HMem: THandle;  P: Pointer;begin  Result := False;  HMem := GlobalAlloc(GMEM_MOVEABLE, Size);  if HMem <> 0 then  begin    P := GlobalLock(HMem);    if P <> nil then    begin      Move(Data, P^, Size);      GlobalUnlock(HMem);      Result := SetClipboardData(Format, HMem) <> 0;    end;  end;end; function PutTextToClipboard(const S: string): Boolean;var  W: UnicodeString;begin  Result := False;  if OpenClipboard(0) then  try    if EmptyClipboard then    begin      W := UTF8Decode(S);      Result := AddDataToClip(CF_UNICODETEXT, Pointer(W)^, SizeOf(UnicodeChar) * Length(W));    end;  finally    CloseClipboard;  end;end; begin  PutTextToClipboard('10' + #9 +'15' + sLineBreak + '12');end.

ad1mt:

--- Quote from: ASerge on February 26, 2017, 09:20:14 pm ---
--- Quote from: tobibo1 on February 23, 2017, 10:18:38 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?

--- End quote ---
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  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program Project1; uses Windows; function AddDataToClip(Format: UINT; const Data; Size: PtrUInt): Boolean;var  HMem: THandle;  P: Pointer;begin  Result := False;  HMem := GlobalAlloc(GMEM_MOVEABLE, Size);  if HMem <> 0 then  begin    P := GlobalLock(HMem);    if P <> nil then    begin      Move(Data, P^, Size);      GlobalUnlock(HMem);      Result := SetClipboardData(Format, HMem) <> 0;    end;  end;end; function PutTextToClipboard(const S: string): Boolean;var  W: UnicodeString;begin  Result := False;  if OpenClipboard(0) then  try    if EmptyClipboard then    begin      W := UTF8Decode(S);      Result := AddDataToClip(CF_UNICODETEXT, Pointer(W)^, SizeOf(UnicodeChar) * Length(W));    end;  finally    CloseClipboard;  end;end; begin  PutTextToClipboard('10' + #9 +'15' + sLineBreak + '12');end.
--- End quote ---

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

ASerge:

--- Quote from: ad1mt on October 18, 2023, 07:45:23 pm ---Please can you add example code to get text from the clipboard?

--- End quote ---
Once in 6 years they exchanged letters :)

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---{$APPTYPE CONSOLE} uses Windows; var  HClip: THandle;  P: PUnicodeChar;  U: UnicodeString = '';begin  if OpenClipboard(0) then  begin    HClip := GetClipboardData(CF_UNICODETEXT);    if HClip <> 0 then    begin      P := GlobalLock(HClip);      U := P;      GlobalUnlock(HClip);    end;    CloseClipboard;  end;  Writeln(U);  Readln;end.

Navigation

[0] Message Index

Go to full version