Thread: Dialogbox from within a DLL

  1. #1
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59

    Post Dialogbox from within a DLL

    I have been tasked with trying to create a text editor completely within a dll. Basically, they want to pass a user directory name to a dll and it will launch a dialogbox with an edit control inside it.

    I have to use "pure" C to create the dll.

    I have written the entire project and it works fine as a stand alone executable but I am unable to understand how to launch a dialog inside a dll.

    I learn best by example, so a sample of how I could spawn a dialog box from within the dll would be of great help.

    Thanks for any help.

    (By the way, they are explicit about not using .Net or C++)
    Last edited by JustMax; 01-31-2009 at 07:50 PM. Reason: added some specifics

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you're saying .net and .dll, why not ask Microsoft? It's not as though .dll is magic somehow; code doesn't change.

  3. #3
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Microsoft doesn't have any example that I have been able to find. It is my understanding that the code shouldn't be much different for a dll but I have been unable to get a dialog or a window to launch from within my dll. I can get message boxes to work just fine.

    Again, I cannot use .Net for this project and Microsoft pushes it and/or C++ in all of the examples I have found.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by JustMax View Post
    Microsoft doesn't have any example that I have been able to find.
    See there's this thing, it's called the internet, and the way it works is that certain words are specially marked with a "link" -- you click on them, and a new page is opened that (usually) is related to the words you clicked on. These words are often marked by the browser in a certain way -- a different color, such as red, perhaps.

    With that in mind, reread the past reply.

  5. #5
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Thank you for your attempt to belittle me. I am only asking for help. If you are above that, I am sure there are plenty of other postings.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by JustMax View Post
    Thank you for your attempt to belittle me. I am only asking for help. If you are above that, I am sure there are plenty of other postings.
    Well, if you don't want a link to an example making a dialog box without using C++ or .net, then what do you want?

  7. #7
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    Tabstop,
    I guess that, if I were smart enough to 'mouse over' I would have seen it was a link. I am sorry for my public showing of my ignorance.

    Thank you for the link. I will try it but I am not sure that the code to create and launch the dialog is my problem. Since I am realitively new at dlls, I am not sure I am creating the right initialization in the dllMain (or if that is even where I begin to call the editor's window.

    Thanks for the input and please accept my appology.

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Not a problem -- I had a choice between snark and a straightforward "what was wrong with the link" and I did not choose wisely the first time.

    I have to admit, I have not made that many dlls, but I have used even fewer dllMain functions. It gets called when the program runs LoadLibrary, so it shouldn't really have all your code in it or anything. You would have the functions that do all the work, and they get called by the main program as needed, just like any other API functions. (Well, I think they have to call GetProcAddress first.)

    (Edit: I guess my point is, just write a function or functions that pops up a dialog box and does whatever it's supposed to do with it -- and test it just like a plain old source file. Then when making a .dll, your function or functions won't change much if at all, but the calling program will have to do a bit more work.)
    Last edited by tabstop; 01-31-2009 at 09:39 PM.

  9. #9
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    I think I could make this work if the bosses would allow it. What they want is, in their code, to call a dll which then does all the displaying and editing of text files (they also need open and save dialogs), closes and returns control back to the main program.

    I have written it in a C application and it all works but they want it in a dll for some reason. They do not want to add anything but a "Launch Editor" button to their code.

  10. #10
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Lots of reading and examples on DLL's here: http://msdn.microsoft.com/en-us/libr...89(VS.85).aspx

    I would first create a DLL project with a simple function like "void LaunchEditor()". Have it just do a MessageBox() at first. Then create a project that uses/tests the DLL by calling LaunchEditor(). Once you have those nuts and bolts in place, you can start filling in the DLL with the real code you've already written.

    gg

  11. #11
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59

    Question

    I have done what MS says and still nothing. If I paste the code to call the dialog into my main program, I can get it to launch but the not from the dll.

    Any ideas?
    Last edited by JustMax; 02-01-2009 at 02:11 AM.

  12. #12
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    If I had any hair left, I would pull it out!!

    I get an exception in the dll in the "DisplayMyMessage" portion I call if with this:
    Code:
    //=========================
            hwndGoto = CreateDialog(NULL, 
                                    MAKEINTRESOURCE(DLG_GOTO), 
                                    hWnd, 
                                    (DLGPROC)GoToProc); 
            ShowWindow(hwndGoto, SW_SHOW); 
    //==========================
    
    
    LRESULT DisplayMyMessage(HINSTANCE hinst, HWND hwndOwner, LPSTR lpszMessage)
    {
        HGLOBAL hgbl;
        LPDLGTEMPLATE lpdt;
        LPDLGITEMTEMPLATE lpdit;
        LPWORD lpw;
        LPWSTR lpwsz;
        LRESULT ret;
        int nchar;
    	HInst = hinst;
    	hWnd = hwndOwner;
        hgbl = GlobalAlloc(GMEM_ZEROINIT, 1024);
        if (!hgbl)
            return -1;
     
        lpdt = (LPDLGTEMPLATE)GlobalLock(hgbl);
     
        // Define a dialog box.
     
        lpdt->style = WS_POPUP | WS_BORDER | WS_SYSMENU | DS_MODALFRAME | WS_CAPTION;
        lpdt->cdit = 3;         // Number of controls
        lpdt->x  = 10;  lpdt->y  = 10;
        lpdt->cx = 100; lpdt->cy = 100;
    
        lpw = (LPWORD)(lpdt + 1);
        *lpw++ = 0;             // No menu
        *lpw++ = 0;             // Predefined dialog box class (by default)
    
        lpwsz = (LPWSTR)lpw;
        nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "My Dialog", -1, lpwsz, 50);
        lpw += nchar;
    
        //-----------------------
        // Define an OK button.
        //-----------------------
        lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->x  = 10; lpdit->y  = 70;
        lpdit->cx = 80; lpdit->cy = 20;
        lpdit->id = IDOK;       // OK button identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_DEFPUSHBUTTON;
    
        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080;        // Button class
    
        lpwsz = (LPWSTR)lpw;
        nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "OK", -1, lpwsz, 50);
        lpw += nchar;
        lpw = lpwAlign(lpw);    // Align creation data on DWORD boundary
        *lpw++ = 0;             // No creation data
    
        //-----------------------
        // Define a Help button.
        //-----------------------
        lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->x  = 55; lpdit->y  = 10;
        lpdit->cx = 40; lpdit->cy = 20;
        lpdit->id = ID_HELP;    // Help button identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | BS_PUSHBUTTON;
    
        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0080;        // Button class atom
    
        lpwsz = (LPWSTR)lpw;
        nchar = 1 + MultiByteToWideChar(CP_ACP, 0, "Help", -1, lpwsz, 50);
        lpw += nchar;
        lpw = lpwAlign(lpw);    // Align creation data on DWORD boundary
        *lpw++ = 0;             // No creation data
    
        //-----------------------
        // Define a static text control.
        //-----------------------
        lpw = lpwAlign(lpw);    // Align DLGITEMTEMPLATE on DWORD boundary
        lpdit = (LPDLGITEMTEMPLATE)lpw;
        lpdit->x  = 10; lpdit->y  = 10;
        lpdit->cx = 40; lpdit->cy = 20;
        lpdit->id = ID_TEXT;    // Text identifier
        lpdit->style = WS_CHILD | WS_VISIBLE | SS_LEFT;
    
        lpw = (LPWORD)(lpdit + 1);
        *lpw++ = 0xFFFF;
        *lpw++ = 0x0082;        // Static class
    //===========================
    //====   FOR SOME REASON *lpszMessage is a bad pointer (I think)
    //vvv                                                                          vvvvvvvvvvvvvv
        for (lpwsz = (LPWSTR)lpw; *lpwsz++ = (WCHAR)*lpszMessage++;);
        lpw = (LPWORD)lpwsz;
        lpw = lpwAlign(lpw);    // Align creation data on DWORD boundary
        *lpw++ = 0;             // No creation data
    
        GlobalUnlock(hgbl); 
        ret = DialogBoxIndirect(hinst, 
                               (LPDLGTEMPLATE)hgbl, 
                               hwndOwner, 
                               (DLGPROC)showDLLDialog()); 
        GlobalFree(hgbl); 
        return ret; 
    }

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Are you allowed to trash the first parameter to CreateDialog? How do you call DisplayMyMessage?

  14. #14
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    AFter many changes, I can get the message box before and after the call to launch the main dialog box. Here is the code:
    Code:
    // ConDLL.cpp : Defines the entry point for the DLL application.
    //
    
    #include "stdafx.h"
    #include "commdlg.h"
    #include "richedit.h"
    #include "resource1.h"
    #include "resource.h"
    
    //  Function Prototypes 
    
    BOOL WINAPI MainDlgProc( HWND, UINT, WPARAM, LPARAM );
    BOOL SaveEditAsStream( HWND );
    DWORD CALLBACK RTFSaveStreamCallback( DWORD, LPBYTE, LONG, LONG * );
    BOOL LoadEditAsStream( HWND );
    DWORD CALLBACK RTFLoadStreamCallback( DWORD, LPBYTE, LONG, LONG * );
    BOOL GetNewTextColor( HWND );
    void CreateMenubar( HWND );
    void CloseTheFile(HWND);
    
    //  Global Variables 
    
    HINSTANCE hInst;                                    //  Program instance handle
    HANDLE hRichEdit;                                       //  RICHED32.DLL handle
    DWORD dwTextColor;                                       //  Current text color
    char *initialText = "This editor will allow you to view tests that you have created.  The following \n"
    						  "options are available by clicking “File” from the menu:\n\n"
    						  "\tTo open a test, select the “Open” option from the menu.\n\n"
    						  "\tIf you change the results of a test, you can save the changes by selecting \n\t\tthe “Save” option from the menu.\n\n"
    						  "\tYou may also close the file and discard any changes by selecting “Close” from the menu.\n\n"
    						  "\tTo return to the main program, select “Exit” from the menu.\n";
    char currentFileName;
    
    
    //  Program Entry Points 
    BOOL APIENTRY DllMain( HINSTANCE hInstance/*HANDLE hModule*/, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
    					 )
    
       hInst = hInstance;                       //  Save program instance in global
    
       MessageBox( NULL, initialText, "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
    //  Call main dialog box
    /**/   CreateDialog( NULL, MAKEINTRESOURCE( MAIN_DLG ), NULL, MainDlgProc );
    //  This is the line that should launch the main dialog window   
        MessageBox( NULL, "After call for MainDlgProc", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
       return( TRUE );
    }
    
    //  Main Dialog Proc 
    
    BOOL WINAPI MainDlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
    {
    	static HICON hIcon;
    	static HICON hIconSm;
       MessageBox( NULL, "We're in MainDlgProc", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
    
    
       switch( msg )
       {
          case WM_INITDIALOG:
          {
             CHARFORMAT cf;
    		 CreateMenubar(hDlg);
    
             if( GetDlgItem( hDlg, RICHTEXT_EDIT ) )                         //  Sanity Check
             {
                dwTextColor = 0x00000000;
    
                memset( &cf, 0, sizeof(CHARFORMAT) );      //  Initialize structure
    			
    
                cf.cbSize = sizeof(CHARFORMAT);             //  Initialize RichEdit
                cf.dwMask = CFM_COLOR | CFM_FACE | CFM_SIZE;  //  control structure
                cf.crTextColor = dwTextColor;
                cf.yHeight = 32;
                strcpy( cf.szFaceName, "Courier" );
    			// Set the icon
    			 hIcon = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON1), IMAGE_ICON, 32, 32, 0);
    			 hIconSm = (HICON)LoadImage(GetModuleHandle(NULL), MAKEINTRESOURCE(IDI_ICON2), IMAGE_ICON, 16, 16, 0);
    
                //  Set character formatting and background color
                SendDlgItemMessage( hDlg, RICHTEXT_EDIT, EM_SETCHARFORMAT, 4, (LPARAM)&cf );
                SendDlgItemMessage( hDlg, RICHTEXT_EDIT, EM_SETBKGNDCOLOR, TRUE, 0 );
    			SendMessage(hDlg, WM_SETICON, IDI_ICON1  , (LPARAM)hIcon);
    			SendMessage(hDlg, WM_SETICON, ICON_SMALL, (LPARAM)hIconSm);
    			break;
             }
             return( TRUE );                  //  End of dialog initialization code
          }
    
          case WM_COMMAND:
             if( wParam == /*ID_FILE_CLOSE*/ 102 )                         //  "Color" button pressed
             {
                CloseTheFile(hDlg);
    			SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) );       //  Back to edit control
                break;
             }
             if( wParam == /*ID_FILE_SAVE*/ 103)                          //  "Save" button pressed
             {
                SaveEditAsStream( hDlg );       //  Save rich edit contents to file
                SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) );       //  Back to edit control
                break;
             }
             if( wParam == /*ID_FILE_OPEN*/ 101)                          //  "Load" button pressed
             {
                LoadEditAsStream( hDlg );           //  Load RTF file into richedit
                SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) );       //  Back to edit control
                break;
             }
    		 if( wParam == 50001 )
    		 {
                 GetNewTextColor( hDlg );
                SetFocus( GetDlgItem( hDlg, RICHTEXT_EDIT ) );       //  Back to edit control
                break;
             }
             if( wParam == ID_FILE_EXIT )               //  Close dialog if Esc pressed
             {
                EndDialog( hDlg, TRUE );
                return( TRUE );
             }
             break;
    
    	  case WM_DESTROY:
    		  DestroyIcon(hIcon);
    		  DestroyIcon(hIconSm);
    		  PostQuitMessage(0);
    		  break;
    
    
       }
       return( FALSE );
    }                                                        //  End of MainDlgProc
    //  ==========================================================
    
    //  Set up menu
    
    void CreateMenubar(HWND hwnd) {  // Set up the menu
      HMENU hMenubar;
      HMENU hMenu, cMenu;
       MessageBox( NULL, "CreateMenubar", "Test Viewer", MB_OK | MB_ICONEXCLAMATION );
    
      hMenubar = CreateMenu();
      hMenu = CreateMenu();
      cMenu = CreateMenu();
    
      //  Menu Options   -------------------------------------------
      //       File Options
      AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)hMenu, TEXT("&File"));
      AppendMenu(hMenu, MF_STRING, /*ID_FILE_OPEN*/ 101, TEXT("&Open"));
      AppendMenu(hMenu, MF_STRING, /*ID_FILE_CLOSE*/ 102, TEXT("&Close"));
      AppendMenu(hMenu, MF_STRING, /*ID_FILE_SAVE*/ 103, TEXT("&Save&"));
      AppendMenu(hMenu, MF_STRING, ID_FILE_EXIT, TEXT("E&xit"));
      //       Settings Options
      //AppendMenu(hMenubar, MF_POPUP, (UINT_PTR)cMenu, TEXT("Se&ttings"));
      //AppendMenu(cMenu, MF_STRING, ID_SETTINGS_FONT, TEXT("Font Co&lor"));
      //AppendMenu(cMenu, MF_STRING, 50002, TEXT("&Background Color"));
      SetMenu(hwnd, hMenubar);
    }
    //  ==============================================================
    The main progam only loads and unloads the dll:

    Code:
    theLib = LoadLibrary("ConDLL.dll");
    FreeLibrary(theLib);
    Last edited by JustMax; 02-01-2009 at 04:32 PM.

  15. #15
    Registered User JustMax's Avatar
    Join Date
    Jan 2009
    Posts
    59
    The problem is in the createdialog call on DllMain. I cannot get that dialog to show.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. non-MFC DLL with MFC app question.
    By Kempelen in forum Windows Programming
    Replies: 10
    Last Post: 08-20-2008, 07:11 AM
  2. dll communicating between each other
    By cloudy in forum C++ Programming
    Replies: 5
    Last Post: 06-17-2005, 02:20 AM
  3. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 12:34 PM
  4. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM
  5. Putting a DialogBox into a DLL?
    By xds4lx in forum Windows Programming
    Replies: 1
    Last Post: 11-17-2002, 12:59 AM