• Advertisement

Archived

This topic is now archived and is closed to further replies.

only one instance

This topic is 5093 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

i have a winamp styled mp3 player(in vb), but i don''t know how to make it so it would be present in only one instance? please help if U can thanx

Share this post


Link to post
Share on other sites
Advertisement
Perhaps this stuff from msdn could help:
If you need to detect whether another instance already exists, create a uniquely named mutex using the CreateMutex function. CreateMutex will succeed even if the mutex already exists, but the function will return ERROR_ALREADY_EXISTS. This indicates that another instance of your application exists, because it created the mutex first.

Share this post


Link to post
Share on other sites
thank U
there is only one problem and probably i''m the one to blame
or maybe i''m just dum enough
but i would apreciate it if u would tell me what''s a "mutex"

Share this post


Link to post
Share on other sites
Thankx, but with this i can only tell if my program is running in multiple instances or not
until now: every time i started an mp3 file it started another instance of my player. now there is only one instance running but how can i add an mp3 file to my playlist if my program is allready running

Share this post


Link to post
Share on other sites
When your program runs, and sees that there is already an instance of it, maybe you could send a message to the window of the original instance, with information about what mp3 to play, and then close the new instance.


int main() { return *((int*)0); }

Share this post


Link to post
Share on other sites
Declare Function CreateMutex Lib "kernel32" Alias "CreateMutexA" (lpMutexAttributes As SECURITY_ATTRIBUTES, ByVal bInitialOwner As Long, ByVal lpName As String) As Long

this is the function U''ve been talking about
but i have no idea what the SECURITY_ATTRIBUTES are, it has "binherithandle","nlength" and "lpsecuritydescriptor"

i have no idea what they are or what values they should take so the function could work corectly

any ideas??? ?)

Share this post


Link to post
Share on other sites

#define WIN32_LEAN_AND_MEAN
#include <windows.h>

#include <iostream>
#include <sstream>
#include <stdexcept>

using namespace std;

// Auto Mutex Class

class mutex
{
public:
mutex(const char* name);
~mutex();

struct already_exists : public std::runtime_error
{
already_exists() : runtime_error("Mutex already exists") {}
};
private:
HANDLE m_mutex;
};

mutex::mutex(const char* name)
: m_mutex(NULL)
{
m_mutex = CreateMutex(NULL,true,name);
if(m_mutex == NULL)
{
std::stringstream ss;
ss << "Could not create mutex: " << name;
throw std::runtime_error(ss.str());
}
else if(GetLastError() == ERROR_ALREADY_EXISTS)
throw already_exists();
}

mutex::~mutex()
{
if(m_mutex != NULL)
ReleaseMutex(m_mutex);
}

int main(int argc,char* argv[])
{
try
{
mutex m("6f8f2a98-2d17-492d-b883-0ff4f9cf07fe");
// Continue program execution

}
catch(mutex::already_exists&)
{
// Silently ignore and shut down

}
catch(std::exception& e)
{
cerr << e.what() << endl;
}
return 0;
}



daerid | Legends | Garage Games | Spirit | Hapy | Boost | Python | Google
"Doomed to crumble, unless we grow, and strengthen our communication" - Maynard James Keenan, Tool

Share this post


Link to post
Share on other sites
thats cool, but i don''t know much of C, i''ve been trying to do this in Visual Basic

thankx anyway

Share this post


Link to post
Share on other sites
And if you find that PrevInstance is true, you can use FindWindow and SendMessage to add the file to the previous instance. See this thread for more info (and maybe run a search on PrevInstance in that forum for even more info).

shmoove

Share this post


Link to post
Share on other sites
thankx
that was a simple one(now that's workin')

well i'm still having problems with the sending of the data to the existing window :
m_hwnd=FindWindow(vbnullstring,"myPLayer")
{"myPLayer" being the name of my form}
returns 0 and i don't know why?
my gues is that this the reson why the setforegroundwindow or setactivewindow does not work
any ideas?

thankx everyone


[edited by - bladesnet on March 15, 2004 12:42:40 PM]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I know nothing of vb but...
is the ''L'' really capital in ''myPLayer''??

Share this post


Link to post
Share on other sites
so the problem is that the app is running fine but now you can't add files to the playlist while it's running? it seems that you are loading your playlist with a seperate program, is that correct? this sounds like bad design or a broken app to me. why not just modify your app so that there is an "add to playlist" button that you "click", like in winamp or any other mp3 player?

-me

[edited by - Palidine on March 17, 2004 1:11:14 PM]

Share this post


Link to post
Share on other sites
maybe sending the data wouldn''t be a problem if i could find the preveous instance of my program.
all it does is detect that another instance is running and shuts it self down...i would need the
FindWindow(vbnullstring,"myPLayer") to return the adress of my previous instance so i would know where to send the data...



Share this post


Link to post
Share on other sites

  • Advertisement