Thread: Dialog Boxes inside DLL

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Dialog Boxes inside DLL

    Hello,
    I am working on a Win32 project with VS2008 Express, where I am attempting to write a C++ program that places two dialog boxes inside a DLL. The two dialog boxes will be accessed with a C program and displayed on the terminal. I've gotten to the point where the only remaining errors are:
    Code:
    ------ Build started: Project: TS104, Configuration: Release Win32 ------
    Compiling...
    TS104.cpp
    .\TS104.cpp(12) : error C2065: 'hDlg' : undeclared identifier
    .\TS104.cpp(12) : error C2065: 'hWndParent' : undeclared identifier
    .\TS104.cpp(12) : error C2065: 'GoTo1Proc' : undeclared identifier
    .\TS104.cpp(40) : error C2065: 'hDlg' : undeclared identifier
    .\TS104.cpp(40) : error C2065: 'hWndParent' : undeclared identifier
    .\TS104.cpp(40) : error C2065: 'GoTo2Proc' : undeclared identifier
    .\TS104.cpp(64) : error C2065: 'nResult' : undeclared identifier
    stdafx.cpp
    dllmain.cpp
    Build log was saved at "file://f:\VS2008\TS104\TS104\Release\BuildLog.htm"
    TS104 - 7 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    The documentation for compiler error code C2065 talks about defining namespaces. I created the following namespace:
    Code:
    // stdafx.h : include file for standard system include files,
    // or project specific include files that are used frequently, but
    // are changed infrequently
    //
    //
    #pragma once
    #include "targetver.h"
    #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
    // Windows Header Files:
    #include <Windows.h>
    #include <Winuser.h>
    #include <WinDef.h>
    #include <BaseTsd.h>
    #include <WinNT.h>
    #include "resource.h"
    #include "TS104.h"
    #include "Header.h"
    // namespace
    namespace std
    {
     HWND  hDlg;
     HWND  hWndParent;
     DLGPROC  GoTo1Proc();
     DLGPROC  GoTo2Proc();
     INT_PTR  nResult;
    }
    using namespace std;
    // TODO: reference additional headers your program requires here
    Now the only error message that I get says the compiler cannot convert a 'INT_PTR' to an 'HWND':
    Code:
    ------ Build started: Project: TS104, Configuration: Release Win32 ------
    Compiling...
    TS104.cpp
    .\TS104.cpp(40) : error C2440: '=' : cannot convert from 'INT_PTR' to 'HWND'
            Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
    stdafx.cpp
    dllmain.cpp
    Build log was saved at "file://f:\VS2008\TS104\TS104\Release\BuildLog.htm"
    TS104 - 1 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    The statement that the compiler cannot make an implicit conversion on is:
    Code:
    hDlg = DialogBox( hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hWndParent, (DLGPROC) GoTo2Proc ); 
    
    The two issues are: 1. Is the namespace defined correctly and 2. What is the proper way to perform a cast on the statement shown.

    Can anybody provide a suggestion about how to resolve these issues? I'm not sure if the namespace creation was handled incorrectly, and that caused the conversion error, or whether the namespace creation was satisfactory, but that had uncovered the conversion error? Any help would be greatly appreciated!!
    Regards,
    -Frank
    Last edited by fmuir; 02-23-2013 at 05:25 PM. Reason: Additional info added.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    So first, you namespace does absolutely nothing. So remove the namespace std. For that matter, don't create a namespace called std, because that's what the standard uses. There are exceptions, but for now, avoid it.
    Then remove the using namespace std too.
    Then, remove the cast (DLGPROC)GoTo2Proc. It should compile without it. If not, then you've declared your call function wrong.
    And for the INT_PTR error... take a look at what DialogBox returns: DialogBox function (Windows)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    11

    Dialog Boxes inside DLL

    Hello Elysia,

    Thanks for your response.


    Namespace std is history in this project. I also removed the cast (DLGPROC) from both the CREATEDIALOG and DIALOGBOX statements. The documation stated that that cast was needed for both GoTo1Proc and Goto2Proc, which are synonyms for DialogProc. I first removed the parentheses from around DLGPROC and tried compiling. That produced quite a few more errors. I then removed DLGPROC from the statements. That brought me back to the list of undeclared indentifiers that I had this morning. I have read and re-read MSDN's documentation for their functions that I am using.

    I have two questions! First, what is the correct way to declare undeclared identifiers? Secondly, are MSDN header files substitutes for prototypes? I had tried creating prototypes for all my functions and MSDN functions, according to documenation from MSDN, before I included MSDN header files in my project. Unfortunately, I didn't have a lot of success with that! The Internet offered conflicting information!

    I am enclosing code from both MSDN and myself below:
    Code:
      
    //---------------------------------
    MSDN Syntax for CreateDialog:
    HWND WINAPI CreateDialog( HINSTANCE hInstance,  LPCTSTR lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc );
    The MAKEINTRESOURCE() Nacro can be substituted for the second input parameter.
    My current source statement for CreateDialog:
    hDlg = CreateDialog( hInstance, MAKEINTRESOURCE(IDD_DIALOG2), hWndParent, GoTo1Proc ); 
    //----------------------------------
    MSDN Syntax for DialogBox:
    INT_PTR WINAPI DialogBox( HINSTANCE hInstance, LPCTSTR lpTemplate, HWND hWndParent, DLGPROC lpDialogFunc );
    The MAKEINTRESOURCE() Nacro can be substituted for the second input parameter.
    My current source statement for DialogBox:
    hDlg = DialogBox( hInstance, MAKEINTRESOURCE(IDD_DIALOG1), hWndParent, GoTo2Proc ); 
    //-----------------------------------
    MSDN Syntax for DialogProc:
    INT_PTR CALLBACK DialogProc( HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam );
    My current statements for DialogProc:
    INT_PTR CALLBACK GoTo1Proc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
    INT_PTR CALLBACK GoTo2Proc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam )
    //-----------------------------------
    If you see any obvious ommissions in the code samples, please let me know?
    Regards,
    -Frank
    Last edited by fmuir; 02-24-2013 at 03:37 AM.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Please include some compilable minimal example of your code.
    I tried this minimal example, and it compiled fine:

    Code:
    #include <Windows.h>
    
    INT_PTR CALLBACK GoTo1Proc( HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM lParam ) { return 0; }
    
    int main()
    {
    	HINSTANCE hInstance = 0;
    	HWND hWndParent = 0;
    	HWND res = CreateDialog( hInstance, MAKEINTRESOURCE(0), hWndParent, &GoTo1Proc ); 
    }
    Interesting note: MSDN says CreateDialog returns an INT_PTR, but when I looked it up in the actual headers, it returned an HWND. Don't know what's up with that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Paste the functions that are generating these errors - i.e. the functions that contain lines 12 , 40, and 64. It sounds like you're using a bunch of undeclared local variables inside the functions.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  6. #6
    Registered User
    Join Date
    Feb 2013
    Posts
    11
    Quote Originally Posted by Elysia View Post
    Interesting note: MSDN says CreateDialog returns an INT_PTR, but when I looked it up in the actual headers, it returned an HWND. Don't know what's up with that.
    @ Elysia,
    Your example of a minimal coding sample that compiled was spot on. While thinking about something that would work, I decided to create a Win32 app using a minimal amount of code from my DLL app. I copied the .rc and resource.h files into the new app. After making some changes to the new app, I was able to compile it and display the modal dialog box from my DLL app. The buttons for the dialog box don't work, but I didn't expect them to. I noticed in the sample coding that was provided in the Win32 app, that references to DialogProc were neither cast with (DLGPROC) nor have an ampersand attached. The use of '&' and '*' has always been a weakness of mine. Another thing that I noticed was that I had to make a global variable of 'nResult' in the new app for it to compile. I'll make similar changes to my DLL app. Thanks for assisting me with this issue!!
    @ Cat
    Your reference to not having 'local variables' prompted me to make any undeclared identifier a global variable. I'll straighten the issue about a variable being local or global later. I had become confused about #define, #typedef and namespace usage in C++. Thanks for responding!

    Regards,
    -Frank
    Last edited by fmuir; 02-24-2013 at 05:57 PM.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by fmuir View Post
    ...I noticed in the sample coding that was provided in the Win32 app, that references to DialogProc were neither cast with (DLGPROC) nor have an ampersand attached. The use of '&' and '*' has always been a weakness of mine....
    & means the address of a function or a variable if put to the left of the function name or variable name. Incidentally, when it comes to functions, simply the function name (without parenthesises), also means a pointer to the function, same as when using & before its name.
    I simply prefer being explicit.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dialog boxes in Dev-C++
    By jmd15 in forum Windows Programming
    Replies: 4
    Last Post: 07-22-2005, 03:58 PM
  2. dialog boxes
    By dreadrocksean in forum C++ Programming
    Replies: 0
    Last Post: 04-10-2005, 11:28 AM
  3. Dialog Boxes
    By Thantos in forum Windows Programming
    Replies: 7
    Last Post: 08-26-2003, 12:49 PM
  4. Dialog Boxes
    By cerion in forum Windows Programming
    Replies: 4
    Last Post: 06-10-2002, 06:54 PM
  5. Dialog Boxes
    By Unregistered in forum Windows Programming
    Replies: 5
    Last Post: 01-03-2002, 11:13 AM