Writing a string variable to the clipboard

I agree, right off the bat, that I am Pascal - inexperienced, to say the least. So any help I can get would be very appreciated. :)

As part of a larger program, I need to write a string variable to the clipboard. I created a new project in Lazarus (version 1.0.12), so I can try to figure out how to do this without the complications caused by the extra unnecessary code that I included below:

program varToClipboard; uses Clipbrd; var textToCopy:string; begin textToCopy := 'Test text from Pascal'; Clipboard.AsText := textToCopy; end. 

Using the above code along with the required LCLBase dependency, I get the following error in CMD when running the compiled EXE:

 An unhandled exception occurred at $000000000043D45E : EAccessViolation : Access violation $000000000043D45E CLIPBOARDREGISTERFORMAT, line 98 of ./include/lclintf.inc $000000000043C35B PREDEFINEDCLIPBOARDFORMAT, line 185 of lclintf.pas $0000000000415B0C TCLIPBOARD__SETASTEXT, line 452 of ./include/clipbrd.inc $0000000000401802 main, line 12 of varToClipboard.lpr 

According to the documentation , I seem to be doing everything right. Although, I found that the documentation ... is missing more than ever.

Also, what do I need to do so that I can run the compiled EXE (which will just generate and write the string to the clipboard) without the console window appearing?

+4
source share
1 answer

You did everything right. The problem here is that the buffer class cannot be used in any way for use in console applications. Your application could not be executed on the next line from the lclintf.inc file, where the WidgetSet object will be available. This fails because the WidgetSet nil variable when you are in the console application only because the console application does not need any widgets:

 function ClipboardRegisterFormat(const AMimeType: string): TClipboardFormat; begin Result := WidgetSet.ClipboardRegisterFormat(AMimeType); end; 

To get around this, you can add the Interfaces block to your uses section and add the LCL dependency project to it:

 program Project1; uses Clipbrd, Interfaces; begin Clipboard.AsText := 'Hello, I''ma text from clipboard!'; end. 

But according to your additional question, it seems that you want to make an application that simply copies certain text to the clipboard and ends. That the type of console application is not the right choice because the console window is displayed for a short time. For such an application, I would make an application without a formal window (note that I know this trick only from the Windows platform):

  • Create a new application through the File / New... menu, select Project / Application in the dialog dialog box tree and create a new project by clicking OK
  • Now remove from your only block (with form); go to the menu Project / Remove from Project and in the newly opened dialog box select unit1.pas and click OK
  • Now you have a childless (and shapeless) application, so it remains to write code to copy text to the clipboard; so now open the project source from the Project / View Project Source menu and, since the project source inserts such code (this is the shortest possible form):

 program Project1; uses Interfaces, Forms, Clipbrd; begin Application.Initialize; Clipboard.AsText := 'Hello, I''ma text from clipboard!'; end. 
+5
source

Source: https://habr.com/ru/post/1499480/


All Articles