MSI: make sure that dialogs are only created and destroyed in a single thread

Mike McCormack mike at codeweavers.com
Tue Apr 26 05:18:07 CDT 2005


ChangeLog:
* make sure that dialogs are only created and destroyed in a single thread
-------------- next part --------------
? dlls/msi/version.res
Index: dlls/msi/action.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/action.c,v
retrieving revision 1.109
diff -u -p -r1.109 action.c
--- dlls/msi/action.c	20 Apr 2005 15:18:43 -0000	1.109
+++ dlls/msi/action.c	26 Apr 2005 10:08:38 -0000
@@ -631,7 +631,7 @@ static void ui_progress(MSIPACKAGE *pack
     MSI_ProcessMessage(package, INSTALLMESSAGE_PROGRESS, row);
     msiobj_release(&row->hdr);
 
-    msi_dialog_check_messages(package->dialog, NULL);
+    msi_dialog_check_messages(NULL);
 }
 
 static void ui_actiondata(MSIPACKAGE *package, LPCWSTR action, MSIRECORD * record)
@@ -1421,7 +1421,7 @@ UINT ACTION_PerformUIAction(MSIPACKAGE *
     if (!handled)
         handled = ACTION_HandleDialogBox(package, action, &rc);
 
-    msi_dialog_check_messages( package->dialog, NULL );
+    msi_dialog_check_messages( NULL );
 
     if (!handled)
     {
@@ -5421,7 +5421,7 @@ static UINT ACTION_SelfRegModules(MSIPAC
                   c_colon, &si, &info);
 
         if (brc)
-            msi_dialog_check_messages(package->dialog, info.hProcess);
+            msi_dialog_check_messages(info.hProcess);
  
         HeapFree(GetProcessHeap(),0,filename);
         msiobj_release(&row->hdr);
Index: dlls/msi/custom.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/custom.c,v
retrieving revision 1.11
diff -u -p -r1.11 custom.c
--- dlls/msi/custom.c	11 Apr 2005 16:10:33 -0000	1.11
+++ dlls/msi/custom.c	26 Apr 2005 10:08:38 -0000
@@ -372,9 +372,9 @@ static UINT process_handle(MSIPACKAGE* p
         /* synchronous */
         TRACE("Synchronous Execution of action %s\n",debugstr_w(Name));
         if (ProcessHandle)
-            msi_dialog_check_messages(package->dialog, ProcessHandle);
+            msi_dialog_check_messages(ProcessHandle);
         else
-            msi_dialog_check_messages(package->dialog, ThreadHandle);
+            msi_dialog_check_messages(ThreadHandle);
 
         if (!(type & 0x40))
         {
@@ -782,8 +782,7 @@ void ACTION_FinishCustomActions(MSIPACKA
         {
             TRACE("Waiting on action %s\n",
                debugstr_w(package->RunningAction[i].name));
-            msi_dialog_check_messages(package->dialog,
-                                      package->RunningAction[i].handle);
+            msi_dialog_check_messages(package->RunningAction[i].handle);
         }
 
         HeapFree(GetProcessHeap(),0,package->RunningAction[i].name);
Index: dlls/msi/dialog.c
===================================================================
RCS file: /home/wine/wine/dlls/msi/dialog.c,v
retrieving revision 1.12
diff -u -p -r1.12 dialog.c
--- dlls/msi/dialog.c	11 Apr 2005 12:47:20 -0000	1.12
+++ dlls/msi/dialog.c	26 Apr 2005 10:08:38 -0000
@@ -106,6 +106,24 @@ static UINT msi_dialog_radiogroup_handle
 static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
 
 
+/* dialog sequencing */
+
+#define MSI_UI_THREAD_CREATE  1
+#define MSI_UI_THREAD_DESTROY 2
+
+typedef struct 
+{
+    int action;
+    msi_dialog *dialog;
+    HANDLE event;
+    UINT error;
+} msi_thread_message;
+
+static DWORD uiThreadId;
+static msi_thread_message* msi_next_msg;
+static HANDLE msi_next_dialog_event;
+static HANDLE msi_dialog_mutex;
+
 INT msi_dialog_scale_unit( msi_dialog *dialog, INT val )
 {
     return (dialog->scale * val + 5) / 10;
@@ -725,41 +743,51 @@ static INT msi_dialog_get_sans_serif_hei
     return height;
 }
 
-static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
+/* fetch the associated record from the Dialog table */
+static MSIRECORD *msi_get_dialog_record( msi_dialog *dialog )
 {
     static const WCHAR query[] = {
         'S','E','L','E','C','T',' ','*',' ',
         'F','R','O','M',' ','D','i','a','l','o','g',' ',
         'W','H','E','R','E',' ',
            '`','D','i','a','l','o','g','`',' ','=',' ','\'','%','s','\'',0};
-    static const WCHAR df[] = {
-        'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
-    msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
     MSIPACKAGE *package = dialog->package;
     MSIQUERY *view = NULL;
     MSIRECORD *rec = NULL;
-    DWORD width, height;
-    LPCWSTR text;
-    LPWSTR title = NULL;
     UINT r;
 
-    TRACE("%p %p\n", dialog, package);
-
-    dialog->hwnd = hwnd;
-    SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
+    TRACE("%p %s\n", dialog, debugstr_w(dialog->name) );
 
-    /* fetch the associated record from the Dialog table */
     r = MSI_OpenQuery( package->db, &view, query, dialog->name );
     if( r != ERROR_SUCCESS )
     {
         ERR("query failed for dialog %s\n", debugstr_w(dialog->name));
-        return -1;
+        return NULL;
     }
     MSI_ViewExecute( view, NULL );
     MSI_ViewFetch( view, &rec );
     MSI_ViewClose( view );
     msiobj_release( &view->hdr );
 
+    return rec;
+}
+
+static LRESULT msi_dialog_oncreate( HWND hwnd, LPCREATESTRUCTW cs )
+{
+    static const WCHAR df[] = {
+        'D','e','f','a','u','l','t','U','I','F','o','n','t',0 };
+    msi_dialog *dialog = (msi_dialog*) cs->lpCreateParams;
+    MSIRECORD *rec = NULL;
+    DWORD width, height;
+    LPCWSTR text;
+    LPWSTR title = NULL;
+
+    TRACE("%p %p\n", dialog, dialog->package);
+
+    dialog->hwnd = hwnd;
+    SetWindowLongPtrW( hwnd, GWLP_USERDATA, (LONG_PTR) dialog );
+
+    rec = msi_get_dialog_record( dialog );
     if( !rec )
     {
         TRACE("No record found for dialog %s\n", debugstr_w(dialog->name));
@@ -1004,7 +1032,8 @@ static LRESULT WINAPI MSIDialog_WndProc(
 {
     msi_dialog *dialog = (LPVOID) GetWindowLongPtrW( hwnd, GWLP_USERDATA );
 
-    TRACE(" 0x%04x\n", msg);
+    TRACE("0x%04x\n", msg);
+
     switch (msg)
     {
     case WM_CREATE:
@@ -1020,13 +1049,25 @@ static LRESULT WINAPI MSIDialog_WndProc(
     return DefWindowProcW(hwnd, msg, wParam, lParam);
 }
 
+static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
+{
+    WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
+
+    TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
+
+    if (msg == WM_COMMAND) /* Forward notifications to dialog */
+        SendMessageW(GetParent(hWnd), msg, wParam, lParam);
+
+    return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
+}
+
 /* functions that interface to other modules within MSI */
 
 msi_dialog *msi_dialog_create( MSIPACKAGE* package, LPCWSTR szDialogName,
                                 msi_dialog_event_handler event_handler )
 {
+    MSIRECORD *rec = NULL;
     msi_dialog *dialog;
-    HWND hwnd;
 
     TRACE("%p %s\n", package, debugstr_w(szDialogName));
 
@@ -1036,73 +1077,182 @@ msi_dialog *msi_dialog_create( MSIPACKAG
     if( !dialog )
         return NULL;
     strcpyW( dialog->name, szDialogName );
+    msiobj_addref( &package->hdr );
     dialog->package = package;
     dialog->event_handler = event_handler;
+    dialog->finished = 0;
 
-    /* create the dialog window, don't show it yet */
-    hwnd = CreateWindowW( szMsiDialogClass, szDialogName, WS_OVERLAPPEDWINDOW,
-                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
-                     NULL, NULL, NULL, dialog );
-    if( !hwnd )
+    /* verify that the dialog exists */
+    rec = msi_get_dialog_record( dialog );
+    if( !rec )
     {
-        ERR("Failed to create dialog %s\n", debugstr_w( szDialogName ));
-        msi_dialog_destroy( dialog );
+        HeapFree( GetProcessHeap(), 0, dialog );
         return NULL;
     }
+    dialog->attributes = MSI_RecordGetInteger( rec, 6 );
+    msiobj_release( &rec->hdr );
 
     return dialog;
 }
 
+static void msi_process_pending_messages(void)
+{
+    MSG msg;
+
+    while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
+    {
+        TranslateMessage( &msg );
+        DispatchMessageW( &msg );
+    }
+}
+
 void msi_dialog_end_dialog( msi_dialog *dialog )
 {
+    TRACE("%p\n", dialog);
     dialog->finished = 1;
+    PostMessageW(dialog->hwnd, WM_NULL, 0, 0);
 }
 
-UINT msi_dialog_run_message_loop( msi_dialog *dialog )
+static void msi_handle_ui_message( void )
 {
-    MSG msg;
+    msi_thread_message *ui_msg;
+    UINT r;
+
+    if( !msi_next_msg )
+        return;
+
+    ui_msg = msi_next_msg;
+    msi_next_msg = NULL;
 
-    if( dialog->attributes & msidbDialogAttributesVisible )
+    TRACE("dialog %s action %d thread\n",
+          debugstr_w(ui_msg->dialog->name), ui_msg->action );
+
+    ResetEvent( msi_next_dialog_event );
+    switch( ui_msg->action )
     {
-        ShowWindow( dialog->hwnd, SW_SHOW );
-        UpdateWindow( dialog->hwnd );
+    case MSI_UI_THREAD_CREATE:
+        r = msi_dialog_run_message_loop( ui_msg->dialog );
+        break;
+    case MSI_UI_THREAD_DESTROY:
+        msi_dialog_destroy( ui_msg->dialog );
+        r = ERROR_SUCCESS;
+        break;
+    default:
+        ERR("bad action %d\n", ui_msg->action);
+        r = ERROR_FUNCTION_FAILED;
     }
+    ui_msg->error = r;
+    SetEvent( ui_msg->event );
+}
 
-    if( dialog->attributes & msidbDialogAttributesModal )
+void msi_dialog_check_messages( HANDLE handle )
+{
+    /* in threads other than the UI thread, block */
+    if( uiThreadId != GetCurrentThreadId() )
     {
-        while( !dialog->finished && GetMessageW( &msg, 0, 0, 0 ) )
-        {
-            TranslateMessage( &msg );
-            DispatchMessageW( &msg );
-        }
+        if( handle )
+            WaitForSingleObject( handle, INFINITE );
+        return;
     }
-    else
-        return ERROR_IO_PENDING;
 
-    return ERROR_SUCCESS;
+    /* there's two choices for the UI thread */
+    while (1)
+    {
+        HANDLE h[2];
+        DWORD r = 0;
+
+        WaitForSingleObject( msi_dialog_mutex, INFINITE );
+
+        msi_process_pending_messages();
+
+        /* check if there's any new dialogs */
+        msi_handle_ui_message();
+
+        ReleaseMutex( msi_dialog_mutex );
+
+        if( !handle )
+            break;
+
+        /*
+         * block here until somebody creates a new dialog or
+         * the handle we're waiting on becomes ready
+         */
+        h[0] = handle; 
+        h[1] = msi_next_dialog_event; 
+        r = MsgWaitForMultipleObjects( 2, h, 0, INFINITE, QS_ALLEVENTS );
+        if( r == WAIT_OBJECT_0 )
+            break;
+    }
 }
 
-void msi_dialog_check_messages( msi_dialog *dialog, HANDLE handle )
+static UINT msi_dialog_call_ui_thread( msi_thread_message *ui_msg )
 {
-    MSG msg;
-    DWORD r;
+    UINT r = ERROR_FUNCTION_FAILED;
+
+    ui_msg->event = CreateEventW( NULL, 0, 0, NULL );
+    WaitForSingleObject( msi_dialog_mutex, INFINITE );
+    if( !msi_next_msg )
+    {
+        msi_next_msg = ui_msg;
+        SetEvent( msi_next_dialog_event );
+        r = ERROR_SUCCESS;
+    }
+    ReleaseMutex( msi_dialog_mutex );
+    WaitForSingleObject( ui_msg->event, INFINITE );
+    CloseHandle( ui_msg->event );
+    return r;
+}
+
+UINT msi_dialog_run_message_loop( msi_dialog *dialog )
+{
+    HWND hwnd;
+
+    if( !(dialog->attributes & msidbDialogAttributesVisible) )
+        return ERROR_SUCCESS;
+
+    if( uiThreadId != GetCurrentThreadId() )
+    {
+        msi_thread_message ui_msg;
+
+        ui_msg.action = MSI_UI_THREAD_CREATE;
+        ui_msg.dialog = dialog;
+        ui_msg.error = ERROR_FUNCTION_FAILED;
+
+        msi_dialog_call_ui_thread( &ui_msg );
+
+        return ui_msg.error;
+    }
+
+    /* create the dialog window, don't show it yet */
+    hwnd = CreateWindowW( szMsiDialogClass, dialog->name, WS_OVERLAPPEDWINDOW,
+                     CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
+                     NULL, NULL, NULL, dialog );
+    if( !hwnd )
+    {
+        ERR("Failed to create dialog %s\n", debugstr_w( dialog->name ));
+        return ERROR_FUNCTION_FAILED;
+    }
+
+    ShowWindow( hwnd, SW_SHOW );
+    UpdateWindow( hwnd );
 
-    do
+    if( dialog->attributes & msidbDialogAttributesModal )
     {
-        while( PeekMessageW( &msg, 0, 0, 0, PM_REMOVE ) )
+        while( !dialog->finished )
         {
-            TranslateMessage( &msg );
-            DispatchMessageW( &msg );
+            MsgWaitForMultipleObjects( 0, NULL, 0, INFINITE, QS_ALLEVENTS );
+            msi_process_pending_messages();
         }
-        if( !handle )
-            break;
-        r = MsgWaitForMultipleObjects( 1, &handle, 0, INFINITE, QS_ALLEVENTS );
     }
-    while( WAIT_OBJECT_0 != r );
+    else
+        return ERROR_IO_PENDING;
+
+    return ERROR_SUCCESS;
 }
 
 void msi_dialog_do_preview( msi_dialog *dialog )
 {
+    TRACE("\n");
     dialog->attributes |= msidbDialogAttributesVisible;
     dialog->attributes &= ~msidbDialogAttributesModal;
     msi_dialog_run_message_loop( dialog );
@@ -1110,6 +1260,19 @@ void msi_dialog_do_preview( msi_dialog *
 
 void msi_dialog_destroy( msi_dialog *dialog )
 {
+    if( uiThreadId != GetCurrentThreadId() )
+    {
+        msi_thread_message ui_msg;
+
+        ui_msg.action = MSI_UI_THREAD_DESTROY;
+        ui_msg.dialog = dialog;
+        ui_msg.error = ERROR_FUNCTION_FAILED;
+
+        msi_dialog_call_ui_thread( &ui_msg );
+
+        return;
+    }
+
     if( dialog->hwnd )
         ShowWindow( dialog->hwnd, SW_HIDE );
     
@@ -1142,7 +1305,7 @@ void msi_dialog_destroy( msi_dialog *dia
     HeapFree( GetProcessHeap(), 0, dialog );
 }
 
-void msi_dialog_register_class( void )
+BOOL msi_dialog_register_class( void )
 {
     WNDCLASSW cls;
 
@@ -1155,22 +1318,28 @@ void msi_dialog_register_class( void )
     cls.lpszMenuName  = NULL;
     cls.lpszClassName = szMsiDialogClass;
 
-    RegisterClassW( &cls );
-}
+    if( !RegisterClassW( &cls ) )
+        return FALSE;
 
-void msi_dialog_unregister_class( void )
-{
-    UnregisterClassW( szMsiDialogClass, NULL );
-}
+    uiThreadId = GetCurrentThreadId();
 
-static LRESULT WINAPI MSIRadioGroup_WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
-{
-    WNDPROC oldproc = (WNDPROC) GetPropW(hWnd, szButtonData);
+    msi_dialog_mutex = CreateMutexW( NULL, 0, NULL );
+    if( !msi_dialog_mutex )
+        return FALSE;
 
-    TRACE("hWnd %p msg %04x wParam 0x%08x lParam 0x%08lx\n", hWnd, msg, wParam, lParam);
+    msi_next_dialog_event = CreateEventW( NULL, 0, 0, NULL );
+    if( !msi_next_dialog_event )
+        return FALSE;
 
-    if (msg == WM_COMMAND) /* Forward notifications to dialog */
-        SendMessageW(GetParent(hWnd), msg, wParam, lParam);
+    return TRUE;
+}
 
-    return CallWindowProcW(oldproc, hWnd, msg, wParam, lParam);
+void msi_dialog_unregister_class( void )
+{
+    UnregisterClassW( szMsiDialogClass, NULL );
+    CloseHandle( msi_next_dialog_event );
+    CloseHandle( msi_dialog_mutex );
+    msi_next_dialog_event = 0;
+    msi_dialog_mutex = 0;
+    uiThreadId = 0;
 }
Index: dlls/msi/msipriv.h
===================================================================
RCS file: /home/wine/wine/dlls/msi/msipriv.h,v
retrieving revision 1.53
diff -u -p -r1.53 msipriv.h
--- dlls/msi/msipriv.h	20 Apr 2005 12:50:05 -0000	1.53
+++ dlls/msi/msipriv.h	26 Apr 2005 10:08:38 -0000
@@ -368,10 +368,10 @@ typedef VOID (*msi_dialog_event_handler)
 extern msi_dialog *msi_dialog_create( MSIPACKAGE*, LPCWSTR, msi_dialog_event_handler );
 extern UINT msi_dialog_run_message_loop( msi_dialog* );
 extern void msi_dialog_end_dialog( msi_dialog* );
-extern void msi_dialog_check_messages( msi_dialog*, HANDLE );
+extern void msi_dialog_check_messages( HANDLE );
 extern void msi_dialog_do_preview( msi_dialog* );
 extern void msi_dialog_destroy( msi_dialog* );
-extern void msi_dialog_register_class( void );
+extern BOOL msi_dialog_register_class( void );
 extern void msi_dialog_unregister_class( void );
 
 /* UI globals */


More information about the wine-patches mailing list