Ok Hey :)

I did a quick search of the forums, but im still not sure about this.

Basicly I want a program in C++ that when its first run works, then the second time it'll close because its already been run. I dont want anything external though like registry, internet or files. So i searched self-modifying code and found its done through ASM :)

I tried reading a few tuts on ASM and im not 100% in it. So I was wondering if you could help me.

My plan is that the program will close if a variable = 0. So I want the program to modify a variable thats usually 1 to start as 0.

int main()
{
int blablahvar = 1;
}

to then run as

int main()
{
int blablahvar = 0;
}

Could you please give me an example and explain? Much appreciated.
Posted on 2007-05-20 21:54:47 by Flynch
It's much 'cleaner' to create a global mutex object.
Posted on 2007-05-21 00:11:45 by ti_mo_n
... how :P
Posted on 2007-05-21 00:22:54 by Flynch
You could just use the FindWindow api, its lame, but its simple, and it does what you want.

HWND FindWindow(

    LPCTSTR  lpClassName, // address of class name
    LPCTSTR  lpWindowName // address of window name
  );


Basically, having first Registered your Application Class with RegisterClass(Ex), but not having yet Created the Application Window, you need to search for a Window which matches your Application ClassName (which you registered with RegisterClass). If the Window exists, then a previous Instance is already running, so we can commit suicide via ExitProcess.. if the Window is not found, we Create it, and continue as usual.
Posted on 2007-05-21 06:29:40 by Homer
As ti_mo_n said you could create a mutex object with CreateMutex,

HANDLE CreateMutex(
    LPSECURITY_ATTRIBUTES lpMutexAttributes, // pointer to security attributes
    BOOL bInitialOwner,                                     // flag for initial ownership
    LPCTSTR lpName                                   // pointer to mutex-object name 
  );

And check if it returns ERROR_ALREADY_EXISTS with GetLastError, if it does then you can close it with ExitProcess.
Posted on 2007-05-21 09:54:25 by lone_samurai5