Click here to Skip to main content
15,895,011 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralSolution Pin
Tausifahamad Tamboli5-Dec-10 17:45
Tausifahamad Tamboli5-Dec-10 17:45 
GeneralProblem with CreateMutex() and CloseHandle() Pin
ashishbhatt19-Dec-07 18:18
ashishbhatt19-Dec-07 18:18 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Sarath C19-Dec-07 19:18
Sarath C19-Dec-07 19:18 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
ashishbhatt19-Dec-07 19:32
ashishbhatt19-Dec-07 19:32 
QuestionRe: Problem with CreateMutex() and CloseHandle() Pin
Kiran Pinjala19-Dec-07 22:31
Kiran Pinjala19-Dec-07 22:31 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Sarath C19-Dec-07 23:03
Sarath C19-Dec-07 23:03 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Kiran Pinjala19-Dec-07 23:11
Kiran Pinjala19-Dec-07 23:11 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Sarath C19-Dec-07 19:41
Sarath C19-Dec-07 19:41 
The following seems to be the problem with the code LPCWSTR(mutexname).


ashishbhatt wrote:
m_mutex = CreateMutex(NULL,FALSE,LPCWSTR(mutexname));



What's the type of mutexname. It's like type casting ASCII string into widestring which may cause unexpected behavior.

Seems this will do. Just try it

LPCTSTR mutexname = _T("MyMutex");
m_mutex = CreateMutex(NULL,FALSE,mutexname);

The following code worked fine for me.
<br />
class RTMPInstanceHandler<br />
{<br />
    HANDLE m_mutex;<br />
public:<br />
    RTMPInstanceHandler():m_mutex(NULL)<br />
    {<br />
    }<br />
<br />
    ~RTMPInstanceHandler()<br />
    {<br />
        // make sure mutex is cleared as we exit<br />
        Release();<br />
    }<br />
<br />
    BOOL Init()<br />
    {<br />
        // Create the named mutex<br />
        m_mutex = CreateMutex(NULL,FALSE,_T("mutexname"));<br />
        if(m_mutex == NULL)<br />
        {<br />
            return FALSE;<br />
        }<br />
<br />
        // Check that the mutex didn't already exist<br />
        if (GetLastError() == ERROR_ALREADY_EXISTS)<br />
        {<br />
            return FALSE;<br />
        }<br />
        return TRUE;<br />
    }<br />
<br />
    //<br />
    // allow mutex to be explicitely cleared<br />
    //<br />
    DWORD Release()<br />
    {<br />
        try<br />
        {<br />
            CloseHandle(m_mutex);<br />
        }<br />
        catch (...)<br />
        {<br />
            //<br />
            // Release() should be called once from the user code,<br />
            // and a second time by the object's destructor.<br />
            // the second call will cause an exception.<br />
            // we can just ignore it.<br />
            //<br />
        }<br />
<br />
        return GetLastError();<br />
    }<br />
};<br />
<br />
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])<br />
{<br />
    int nRetCode = 0;<br />
<br />
<br />
    RTMPInstanceHandler inst;<br />
    if ( !inst.Init())<br />
    {<br />
        cout<<"instance already exists";<br />
        return nRetCode;<br />
    }<br />
<br />
    cin >> nRetCode;<br />
    return nRetCode;<br />
}<br />

-Sarath.
"Great hopes make everything great possible" - Benjamin Franklin

GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
ashishbhatt19-Dec-07 20:56
ashishbhatt19-Dec-07 20:56 
AnswerRe: Problem with CreateMutex() and CloseHandle() Pin
Member 75496020-Dec-07 3:37
Member 75496020-Dec-07 3:37 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
ashishbhatt24-Dec-07 18:18
ashishbhatt24-Dec-07 18:18 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
Member 75496026-Dec-07 4:51
Member 75496026-Dec-07 4:51 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
DevelopmentNoob16-Dec-09 19:56
DevelopmentNoob16-Dec-09 19:56 
GeneralRe: Problem with CreateMutex() and CloseHandle() Pin
NehaMishra286846-Dec-10 19:31
NehaMishra286846-Dec-10 19:31 
GeneralDrag Drop Pin
john563219-Dec-07 18:01
john563219-Dec-07 18:01 
GeneralRe: Drag Drop Pin
Hamid_RT19-Dec-07 18:17
Hamid_RT19-Dec-07 18:17 
GeneralHelp Needed for getting record from database Pin
Y_Kaushik19-Dec-07 17:53
Y_Kaushik19-Dec-07 17:53 
GeneralRe: Help Needed for getting record from database Pin
Roger Broomfield19-Dec-07 18:57
Roger Broomfield19-Dec-07 18:57 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 19:22
Y_Kaushik19-Dec-07 19:22 
GeneralRe: Help Needed for getting record from database Pin
Roger Broomfield19-Dec-07 19:41
Roger Broomfield19-Dec-07 19:41 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 19:53
Y_Kaushik19-Dec-07 19:53 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 20:01
Y_Kaushik19-Dec-07 20:01 
GeneralRe: Help Needed for getting record from database Pin
Y_Kaushik19-Dec-07 20:36
Y_Kaushik19-Dec-07 20:36 
QuestionNeed help with restricting window movement to the screen... Pin
demxine19-Dec-07 16:35
demxine19-Dec-07 16:35 
GeneralRe: Need help with restricting window movement to the screen... Pin
Maxwell Chen19-Dec-07 16:45
Maxwell Chen19-Dec-07 16:45 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.