Click here to Skip to main content
15,886,059 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Normally I work with c#. but i don't know c++. I'm trying to learn. Is there a C++ equivalent of this code I asked you for?
and what I want to do is: check if an app is open, if it is open give a message error and and if it is closed give a continue command

What I have tried:

C#
Process[] EngineApp = Process.GetProcessesByName("xengine");
if (EngineApp.Length == 0)
{
    RunApplication();
}
else
{
    MessageBox.Show("You cannot open the application more than once.", "Connection error",
    MessageBoxButtons.OK, MessageBoxIcon.Error);
    Application.Exit();
}
Posted
Updated 14-Nov-21 9:51am
v2

 
Share this answer
 
note: i haven't touched c++ for years.

An app that can only have a single instance at any one time is called a singleton.

C++ singleton example: [^]

Another example: [^]

note: you might wat to review the discussion here about C++ singleton: [^]
 
Share this answer
 
v3
Comments
Richard MacCutchan 14-Nov-21 10:53am    
But you can still start more than one instance of the actual application.
BillWoodruff 14-Nov-21 10:58am    
hi, we are talking about making an app behave like a singleton class here.

"What is a Singleton class in C++?
Singleton in C++

"Singleton is a creational design pattern, which ensures that only one object of its kind exists and provides a single point of access to it for any other code. ... You can't just use a class that depends on Singleton in some other context. You'll have to carry the Singleton class as well."

https://refactoring.guru/design-patterns/singleton/cpp/example
Richard MacCutchan 14-Nov-21 11:22am    
No he is asking how to ensure only one execution of a specific application is running at any one time. And the only way to do that is to check the running application list, or use a mutex when the application starts.

You can run any number of copies of an application, even if it contains a singleton class within it.
BillWoodruff 14-Nov-21 19:58pm    
i treasure these moments when i can see we are, temporarily, in different universes :)

"No he is asking how to ensure only one execution of a specific application is running at any one time."

uhhh ... that is what I said.

"You can run any number of copies of an application, even if it contains a singleton class within it."

The OP is talking about an application, not a class: i assume the OP is/has written the App, and the task is to enforce singleton behavior on one machine. Use of Mutex is one standard way to create a Singleton in a context where thread safety is critical.

It's been over 15 years since i touched C++, and i am not aware of current issues in creating Win apps.
 
Share this answer
 
Comments
xXRunnerNice RN 15-Nov-21 8:05am    
mmmm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900