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