Timothy Vismor wrote: > On 11/17/06, Havoc Pennington <[EMAIL PROTECTED]> wrote: >> >> >> >> Timothy Vismor wrote: >> > >> > A common way to guarantee a unique instance of a Windows application >> > is to use a well known mutex name. A simple example is: >> > >> >> Thanks, that looks pretty good. We'd need to solve one more problem >> also, which is how to get the address of the existing process... any >> typical pattern for that? ;-) >> >> Havoc > > > Never needed to do that specific operation, but it seems that a simple > solution could be to use named shared memory. When a process grabs > the DBus mutex, it could write relevant address information to a well > known block of named shared memory. When you release the > mutex, you also release the shared memory block. If a process > fails to grab the mutex, it could read the required information from > shared memory. > > DURING STARTUP, AFTER YOU CREATE THE MUTEX > > SECURITY_ATTRIBUTES sa; > sa.nLength = sizeof( sa ); > sa.lpSecurityDescriptor = m_pMemSD; > sa.bInheritHandle = TRUE; > > // Create shared virual memory in the page file. > m_hSharedMem = CreateFileMapping( (HANDLE)0xFFFFFFFF, &sa, > PAGE_READWRITE, > ZERO, nBytes, "DBusAddressInfo" > ); > if( m_hSharedMem == NULL ) > { > // Unable to allocate shared memory. > return( FALSE ); > } > > // Map shared memory into process address space. > if( m_hSharedMem != NULL ) > { > m_pSharedMem = MapViewOfFile( m_hSharedMem, FILE_MAP_WRITE, ZERO, > ZERO, ZERO ); > if( m_pSharedMem == NULL ) > { > // Unable to map shared memory. > ::CloseHandle( m_hSharedMem ); > m_hSharedMem = NULL; > return( FALSE ); > } > } > // Write process information to shared memory. > ... > > DURING SHUTDOWN > > if( m_pSharedMem != NULL ) > { > ::UnmapViewOfFile( m_pSharedMem ); > m_pSharedMem = NULL; > } > if( m_hNetDevMem != NULL ) > { > ::CloseHandle( m_hSharedMem ); > m_hSharedMem = NULL; > } > > > IF CAN'T GET MUTEX > > // Connect to the memory mapped file (actually the paging file). > m_hSharedMem = OpenFileMapping(FILE_MAP_READ, FALSE, "DBusAddressInfo"); > > if( m_hSharedMem == NULL ) > { > // Unable to link to shared memory. > // Process is no longer running. > return( FALSE ); > } > > // Map shared memory into our address space. > m_pSharedMem = MapViewOfFile( m_hSharedMem, FILE_MAP_READ,ZERO, ZERO, > ZERO ); > if( m_pSharedMem == NULL ) > { > // Unable to map shared memory. > CloseHandle( m_hSharedMem ); > m_hSharedMem = NULL; > return( FALSE ); > } > // Read process info from shared mem > ....
Great! I'll give it a try. Peter