disallowing multiple IrrlichtDevice's being opened

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
ericaus
Posts: 14
Joined: Sat Sep 19, 2009 10:53 am
Location: Australia

disallowing multiple IrrlichtDevice's being opened

Post by ericaus »

Hello, just wondering. How do I stop more then 1 IrrlichtDevice from being opened?
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Re: disallowing multiple IrrlichtDevice's being opened

Post by randomMesh »

ericaus wrote:Hello, just wondering. How do I stop more then 1 IrrlichtDevice from being opened?
Easy. Just don't create multiple devices.
"Whoops..."
ericaus
Posts: 14
Joined: Sat Sep 19, 2009 10:53 am
Location: Australia

Re: disallowing multiple IrrlichtDevice's being opened

Post by ericaus »

randomMesh wrote:
ericaus wrote:Hello, just wondering. How do I stop more then 1 IrrlichtDevice from being opened?
Easy. Just don't create multiple devices.
that way would be easy but not so great.

I now done some testing with the source code and added:

Code: Select all

		HWnd = FindWindow(ClassName, NULL); // I'll change NULL to a window name that I'll have inside a project to avoid problems if I have a new project using IrrlichtDevice.

		if (HWnd != NULL)
		{
			hInstance = NULL;
			MessageBox(HWnd, "Oops, a device is already opened", "duplicate", MB_OK);
			//closeDevice(); // this doesn't work.
			//return 0; // this doesn't work.
			//HWnd = NULL; // this doesn't work.
			//PostQuitMessage(0); // this kind of works but would make problems when getting an IrrlichtDevice into fullscreen
		}
just below:

Code: Select all

	// get handle to exe file
	HINSTANCE hInstance = GetModuleHandle(0);

	// create the window if we need to and we do not use the null device
	if (!CreationParams.WindowId && CreationParams.DriverType != video::EDT_NULL)
	{
		const c8* ClassName = "CIrrDeviceWin32";

inside the CIrrDeviceWin32... What might be a good way to end it from there?
Last edited by ericaus on Sat Oct 03, 2009 4:31 am, edited 1 time in total.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Just wondering - is there any reason why you want to prevent that? I do occasionally work with 2 devices open at the same time and think it's not such a good idea to change something like that in the engine. Always try to minimize changes in your engine version, it will make updating so much easier even if you have a good patch management.

randomMesh gave a good hint there - just don't open more than one if you don't need that. You usually do that anyway just once in initializing, so it's not exactly something happens accidentally (usually).

Or (just guessing) do you maybe want to prevent your application getting started more than once? Then you better use a mutex (in your app - not in the engine).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
ericaus
Posts: 14
Joined: Sat Sep 19, 2009 10:53 am
Location: Australia

Post by ericaus »

CuteAlien wrote:Just wondering - is there any reason why you want to prevent that?
Oh, its just so that once I become professionalized in using Irrlicht, I could attempt creating MORPG's and disallow users from opening more than 1 application and playing on multiple accounts. :D
CuteAlien wrote:Then you better use a mutex (in your app - not in the engine).
hmm, mutex. :? I've never heard of it, though I'll do some research on that.
randomMesh
Posts: 1186
Joined: Fri Dec 29, 2006 12:04 am

Post by randomMesh »

You could create a shared memory segment and check if it is already present.
You can use boost::interprocess for this.
"Whoops..."
ericaus
Posts: 14
Joined: Sat Sep 19, 2009 10:53 am
Location: Australia

Post by ericaus »

:) The mutex solution works perfectly. After a bit of research, I added:

Code: Select all

	HANDLE handle = CreateMutex(NULL, false, "demo");

	if (GetLastError() == ERROR_ALREADY_EXISTS)
	{
		MessageBox(0, "Application is already running", "duplicate", MB_ICONEXCLAMATION | MB_OK);
		return 0;
	}
in the first couple of lines inside int main() before calling anything else inside a test project. :D
Nox
Posts: 304
Joined: Wed Jan 14, 2009 6:23 pm

Post by Nox »

ericaus wrote:Oh, its just so that once I become professionalized in using Irrlicht, I could attempt creating MORPG's and disallow users from opening more than 1 application and playing on multiple accounts.
And the user will open a vm, start a second app from the vm and laugh about the programmer who tried to avoid this.....
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post by Eigen »

Nox wrote: And the user will open a vm, start a second app from the vm and laugh about the programmer who tried to avoid this.....
You could create a temp file when your game starts and erase it when you close the game. On startup you check if the file exists. If it does, you won't be able to start. Of course, someone would try erase the file after starting one copy of game but the game would shut down abruptly after some time (you could check for file existence after every 30 seconds or so)

It may not be the most elegant solution, but I think it works.
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

@Eigen:
And the user will open a vm, start a second app from the vm and laugh about the programmer who tried to avoid this.....
The VM won't know anything about the file on the other machine.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
CuteAlien
Admin
Posts: 9734
Joined: Mon Mar 06, 2006 2:25 pm
Location: Tübingen, Germany
Contact:

Post by CuteAlien »

Even worse - the games crashes once, the file would not get deleted and the user couldn't start it any-more. Actually speaking out of experience here, as some applications occasionally try that thing (for other reasons - it's sometimes nice to protect users from accidentally opening same application twice).
IRC: #irrlicht on irc.libera.chat
Code snippet repository: https://github.com/mzeilfelder/irr-playground-micha
Free racer made with Irrlicht: http://www.irrgheist.com/hcraftsource.htm
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post by Eigen »

Oh, right. But then perhaps you can do an IP check on login and check if it's not logged in already? Different accounts, yes, but same IP, no? Or can you change your IP as well when running in VM?
Sylence
Posts: 725
Joined: Sat Mar 03, 2007 9:01 pm
Location: Germany
Contact:

Post by Sylence »

Eigen wrote:Or can you change your IP as well when running in VM?
Not the one the server sees, but you can use proxies.

There is no way that works 100%. You have to prevent the user from creating multiple accounts. Once the user has more than one account he can use them both.
Software documentation is like sex. If it's good you want more. If it's bad it's better than nothing.
Eigen
Competition winner
Posts: 375
Joined: Fri Jan 27, 2006 2:01 pm
Location: Estonia
Contact:

Post by Eigen »

Preventing a user from creating more than one account is pretty much impossible, I think. You can easily limit one account per IP but then you'd simply use another computer.

I think you have deal with it once the game is running. Monitoring interactions between users. (It could be made automatic on the server) If two users are constantly trading things, etc. then have the administrator have a look and warn both accounts, if needed. If that doesn't help, delete them. If you make it a rule, there should not be any misunderstanding.
Post Reply