tutalia
tutalia

Reputation: 285

How to keep custom text in Window (Win32)

We can set window text with:

BOOL WINAPI SetWindowText(
  __in      HWND hWnd,
  __in_opt  LPCTSTR lpString
);

and this windows text by another application

int WINAPI GetWindowText(
  __in   HWND hWnd,
  __out  LPTSTR lpString,
  __in   int nMaxCount
);

I don't want to change my forms caption but I want to keep a custom text in window and get this text by another instance of same program. How can I achieve this?

Upvotes: 2

Views: 375

Answers (3)

David Heffernan
David Heffernan

Reputation: 613322

Probably the easiest way to do this is to define a windows message in the WM_APP range and send it from one instance to the other. You'd have to do your own marshalling though with something like GlobalAlloc.

Upvotes: 1

Jon
Jon

Reputation: 437604

A simple way to do this is:

  1. Define your own WM_APP message, which instance A of your app will use to tell instance B's window "send me your private text".
  2. When instance B receives this message, it can use WM_COPYDATA to send a reply to instance A's window.
  3. Instance A receives the data from WM_COPYDATA and does what it wants with it.

This scheme allows you to send arbitrary data (as long as it doesn't include pointers) provided that the data is reasonably small (say a few KB tops). It's also easy to implement. What it does lack is security controls, so if you have to prevent the bad guys from retrieving this data from your application you will need to use something more heavy-duty.

Upvotes: 3

Vikram.exe
Vikram.exe

Reputation: 4585

I am not sure, but I think you will have to open the other process using openProcess and then use some function on the process handle to get the title text. Once you have the text, you can very well use that in your application. For getting the process if of the required process, you can use EnumProcess, here is a complete example: http://msdn.microsoft.com/en-us/library/ms686701%28v=vs.85%29.aspx

Upvotes: 1

Related Questions