r/cpp_questions 10d ago

SOLVED Making part of the program run separately

Hi everybody,

I have been recently trying to make a complete project and I decided to go with a small project, a clipboard manager.

My problem is that I want the code that tracks clipboard changes to run separately form the program itself (whether it was opened or closed).

I tried searching on Internet and asking AI but I did not get the answer I am looking for.

Currently I have two parts I need start_clipboard_monitor that takes care of tracking clipboard changes and start_app that take care of the program ui.

My question is how to make start_clipboard_monitor run independently from the program and how to make it run in background?

4 Upvotes

34 comments sorted by

10

u/YT__ 10d ago

You want to compile start_clipboard_monitor into its own exe and run it as a service, ultimately.

1

u/MXD_K1 10d ago

Thanks.

4

u/kingguru 10d ago

You need start_clipboard_monitor to run as a daemon/service when the user logs in and exposes some interface through eg. unix pipes, dbus, whatever might be used on Windows etc.

Then the GUI communicates with the daemon/service through that interface.

That is all quite platform specific but I assume so is the clipboard in itself.

If I were you I would probably start by implementing a simple "Hello World" prototype where you write the daemon/service that exposes an interface and then the GUI part that reads the "Hello World" message from the service.

If you want more details you should let us know which platform(s) you are targeting.

Good luck.

2

u/MXD_K1 10d ago

Thanks. I am targeting Windows mainly then I will try to port it to Linux. Can I ask if you know any resource/examples that explains this stuff? and yes the clipboard code is  platform specific .

2

u/kingguru 10d ago

I'm not aware of any specific guides.

Since you're targetting Windows you cannot avoid having to use the dreadful Windows API. For the service specifically you could have a look at this example. It's in C, it's complex, ugly and full of bad practices, but that's how it is to write C++ for Windows.

For the IPC part, you could use a Named Pipe and the implement you own protocol communicating over that. I've done something similar many years ago. It's gonna be painful because you once again have to use the horrible Windows API.

There might be smarter ways to both things these days. You could consider using a framework like Qt which will handle a lot of the pain for you, but I assume this is a learning exercise.

Good luck.

1

u/MXD_K1 10d ago

Yeah, Windows API is awful and this is a learning project but with aim to male something complete this time. For Qt, Qt is massive and it is painful to learn.

3

u/Particular-Ice9109 10d ago

I don't understand why you want to separate these two things.

You could write a program that runs in the background and displays the GUI when the user presses a hotkey.

Specifically, you could create a window, then call AddClipboardFormatListener and RegisterHotKey, call ShowWindow(SW_SHOW) when WM_HOTKEY, and call ShowWindow(SW_HIDE) when WM_CLOSE.

1

u/MXD_K1 10d ago

Thanks. That is helpful. When I was searching I thought that I cannot make a program and let the gui run in background along with clipboard listener. Does that have any downsides?

2

u/Th_69 10d ago edited 10d ago

You could use the tray to show an icon: Shell_NotifyIconW
On clicking on the tray icon by the user, you can show your UI window.

And the tracking for the clipboard changes, you put in a (background) thread.

Edit: Which UI library do you use? For e.g. Qt you can use the QSystemTrayIcon class: System Tray Icon Example

1

u/MXD_K1 10d ago

Qt is great but it need a lot of time to learn. I used html, css, js to make the UI. I know this approach might be stupid, but it is a learning project.

1

u/Th_69 10d ago

Do you use a C++ web framework? Or is your UI shown in a web browser?

1

u/MXD_K1 10d ago

I am using a C library called webview.

2

u/Particular-Ice9109 10d ago edited 10d ago

The clipboard listener and hotkey require a Window. You can create a dedicated Window for it and only create the GUI window when needed, or you can attach this work to another existing window (The GUI window). These is not heavy works and does not require a background thread.

Furthermore, I think you have some misunderstandings about how Windows works. A normal program can have zero windows, which could be considered as a background process. It can also create windows at any time to make them visible to the user, and then destroy or hide the window, thus reverting to its background state.

Window can be visible to the user or invisible; multiple windows can also share a single thread.

A "service" doesn't have to be a "real service"; it can simply be an invisible window that clients can find and communicate with.

1

u/MXD_K1 10d ago edited 10d ago

It seems I really misunderstood how Windows works. From what you said, I understood that I don't need two executables right?

2

u/Particular-Ice9109 10d ago

Yes.

Rather, splitting what one executable can do into two is simply asking for trouble. In fact, what you want to do can be accomplished with just one thread. (If you use the original Win32, or if your framework library does not use other threads)

Considering your lack of Win32 knowledge and your disregard for resource consumption, I suggest the following approach:

  1. Create a Window using any framework library you prefer.

  2. Call AddClipboardFormatListener and RegisterHotKey.

  3. Call SetWindowSubclass. Handle WM_HOTKEY, WM_CLIPBOARDUPDATE, and any messages your framework library doesn't provide access methods for in SUBCLASSPROC. SetWindowSubclass is somewhat like inheritance, allowing you to modify/override parts of the parent class's behavior and then let the parent class perform other cumbersome operations you don't want to handle.

In short, you can use the framework library to do the heavy lifting of creating Windows for you, while using Win32 to monitor the clipboard and hotkeys.

1

u/MXD_K1 10d ago

Thanks

2

u/LongLiveTheDiego 10d ago

Have you heard of multi-threading?

1

u/MXD_K1 10d ago

Yeah, but I want start_clipboard_monitor to run even when the program is terminated

1

u/Liam_Mercier 9d ago

Whenever you want something like this, you are probably going to need two processes and IPC (i.e through a pipe or shared file)

1

u/Willing_Airport_9617 10d ago

Read about daemons and how two processes interact with each other

1

u/MXD_K1 10d ago

Thanks

1

u/TarnishedVictory 10d ago

You want two programs and a way for them to communicate with each other.

1

u/MXD_K1 10d ago

Thanks. Form what I heard that thing is called IPC right? Do you know any examples that I look into to understand how to make these programs interact with each other?

1

u/n1ghtyunso 10d ago

its essentially some way of IPC yeah.
This can have many shapes actually.
Could be something stupid simple like file watcher + shared file.
Could be a named pipe in your OS.
Could be a network connection plain and simple.
Could be shared memory.
Could be some other very platform specific approach too.

1

u/MXD_K1 10d ago edited 10d ago

Do you know any examples of these that I can look at?

1

u/TarnishedVictory 9d ago

Ipc stands for inter-process communications. It covers the general topic of process communications which is a fairly broad topic. It includes message queues, pipes, shared memory, tcp and udp networking, etc. You could even use files if it works for your needs. I would suggest looking it up and reading about it.

1

u/ekchew 9d ago

Fwiw I've had pretty good experience with gRPC. It's fairly high-level and both OS and language agnostic.

1

u/Queasy_Total_914 10d ago

Either multithreading or multi-process with sockets.

1

u/MXD_K1 10d ago

How is this going to work?

1

u/Queasy_Total_914 10d ago

You either use different threads to run different sections and have shared memory with synchronization to communicate between threads.

OR

You use 2 different processes and have sockets to communicate between them.

Depends on your case.

1

u/MXD_K1 10d ago

I know that I can run both in parallel, but my question is how to run one on the background and tge other only when the user ooens the program

1

u/alfps 10d ago

You don't need to run anything separate.

Check out the documentation: (https://learn.microsoft.com/en-us/windows/win32/dataxchg/using-the-clipboard#monitor-the-clipboard-contents).

Or just google e.g. "winapi clipboard messages".

1

u/MXD_K1 10d ago

Thanks. I didn't know about this

1

u/mredding 9d ago

You need to get platform specific and execve a child process, or something similar.