Recent

Author Topic: GetConsoleTitle with pchar as argument ?  (Read 896 times)

Nitorami

  • Sr. Member
  • ****
  • Posts: 451
GetConsoleTitle with pchar as argument ?
« on: March 10, 2023, 09:29:00 am »
GetConsoleTitle works with a shortstring as parameter but not with a pchar. Why ?

Code: Pascal  [Select][+][-]
  1. {$H-}
  2. uses windows;
  3.  
  4. var s: shortstring;
  5.     p: pchar;
  6. begin
  7.   writeln;
  8.   s :=  '##########################################################################';
  9.   writeln ('GetTitle returns ',GetConsoleTitle (@s[1],50));  //works
  10.   writeln (s+LineEnding);
  11.  
  12.   p :=  '##########################################################################';
  13.   writeln ('GetTitle returns ',GetConsoleTitle (p,50));      //doesn't work
  14.   writeln (p+LineEnding);
  15.   writeln ('GetTitle returns ',GetConsoleTitle (@p[0],50));  //doesn't either
  16.   writeln (p+LineEnding);
  17. end.
  18.  
  19.  

Output:
Running "c:\temp\getconsoletitle.exe "

GetTitle returns 24
Eingabeaufforderung - fp #################################################

GetTitle returns 0
##########################################################################

GetTitle returns 0
##########################################################################



Red_prig

  • Jr. Member
  • **
  • Posts: 99
Re: GetConsoleTitle with pchar as argument ?
« Reply #1 on: March 10, 2023, 10:08:28 am »
Because pchar is a reference to data, first allocate the memory and put the reference in p, then when the data is no longer needed, free it accordingly

Nitorami

  • Sr. Member
  • ****
  • Posts: 451
Re: GetConsoleTitle with pchar as argument ?
« Reply #2 on: March 10, 2023, 10:10:20 am »
Understood, but I initialised p with p := '####....', so it must have been allocated at a speciifc address ?

Red_prig

  • Jr. Member
  • **
  • Posts: 99
Re: GetConsoleTitle with pchar as argument ?
« Reply #3 on: March 10, 2023, 10:11:36 am »
'####....' is a constant and is write-protected

Nitorami

  • Sr. Member
  • ****
  • Posts: 451
Re: GetConsoleTitle with pchar as argument ?
« Reply #4 on: March 10, 2023, 10:31:23 am »
You mean the content of pchar is stored in a specific write-protected area of memory which the API function cannot write to ?

Red_prig

  • Jr. Member
  • **
  • Posts: 99
Re: GetConsoleTitle with pchar as argument ?
« Reply #5 on: March 10, 2023, 10:46:24 am »
correct

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4228
  • I like bugs.
Re: GetConsoleTitle with pchar as argument ?
« Reply #6 on: March 10, 2023, 01:44:53 pm »
Yes, memory must be allocated for PChar with StrAlloc(). Later it should be freed with StrDispose().
ShortString is a good type if the Title does not exceed 255 characters.
AnsiString can be used, but you must set its length first. After that it is freed automatically.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Nitorami

  • Sr. Member
  • ****
  • Posts: 451
Re: GetConsoleTitle with pchar as argument ?
« Reply #7 on: March 10, 2023, 02:57:21 pm »
Thank you, that enlightens me. Maybe you can help me a bit further. I try to copy text to the windows clipboard from within a Console. I got it working in principle:

  • First, get a window handle to the Console via GetConsoleTitle and FindWindow().
  • Using this handle, I can now OpenClipBoard() and EmptyClipBoard().
  • Then, use GlobalAlloc to reserve a memory region; it returns a handle, not a pointer. I can pass this handle to SetClipboardData() to copy the associated memory to the clipboard.
  • But to get something into this memory first, I need its address. I can get a pointer via GlobalLock (handle), and can now move() my own memory content (e.g. the string) to this memory.

This seems to work, but I wonder if I could avoid the extra memory allocation via Global Alloc. I would need a memory handle for my own Pascal structure but cannot see a way how to do this.[/list]

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4228
  • I like bugs.
Re: GetConsoleTitle with pchar as argument ?
« Reply #8 on: March 10, 2023, 03:44:13 pm »
I can only recommend the LCL unit Clipbrd and class TClipboard.
I don't use Windows myself and don't know much about WinAPI programming.
LCL pulls in a lot of code, thus in your console program you may want to avoid it.
If one GlobalAlloc is all you need for the task, it sounds like a reasonable addition.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

Nitorami

  • Sr. Member
  • ****
  • Posts: 451
Re: GetConsoleTitle with pchar as argument ?
« Reply #9 on: March 10, 2023, 03:56:47 pm »
Agreed it would be easier via Lazarus and the LCL. But I still use the textmode IDE because compliation speed is unsurpassed (unless the windows virusscanner intervenes). I often write small throw-away programs during my work, and the Strg-F9 button starts to wear out...

Can I use the LCL from within a Console program ? Rather not, I guess ?

JuhaManninen

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4228
  • I like bugs.
Re: GetConsoleTitle with pchar as argument ?
« Reply #10 on: March 11, 2023, 08:54:59 am »
Can I use the LCL from within a Console program ? Rather not, I guess ?
You can but it may be difficult without the Lazarus IDE.
LCL has a "NoGUI" widgetset which does not call any real widgetset. For example the LazBuild console program uses it.
The same LazBuild can compile and build projects made with Lazarus. You could modify such a project with a textmode IDE and then run LazBuild. Compilation would not be super-fast any more because LCL pulls in lots of code.
Mostly Lazarus trunk and FPC 3.2 on Manjaro Linux 64-bit.

 

TinyPortal © 2005-2018