Solved

HWND and HMODULE

Posted on 2008-06-16
19
1,828 Views
Last Modified: 2010-04-21
Hello
I have a code where, following is written for a GUI C/C++ app(VC++ .NET2003):

typedef struct tagAPPINFO {
   INT      iVersion;
   INT      iFiller;

   HWND     hAppInst;
   INT      iAppType;

   HWND     hWndFrame;
   HWND     hWndMDIClient;
   HWND     hWndToolbar;
   HWND     hWndStatbar;

   UINT     uMemBlocks;
   UINT     uMemBlockSize;

   char     szAppName  [MAXOBJNAME      + 2];
   char     szFileName [FILENAME_LENGTH + 2];
   char     szHelpFile [PATHNAME_LENGTH + 2];
   char     szPathName [PATHNAME_LENGTH + 2];
   BOOL     bDeltaMode;
   char     szSysPathName[PATHNAME_LENGTH + 2];
   } APPINFO;

static HMODULE hResourceModuke;
main()
{
APPINFO appinfo;
appinfo.hAppInst=hResourceModuke;
}

As a beginner, I would like to understand the purpose of HWND and HMODULE?

Regards
Sham
0
Comment
Question by:sham_ibmgs
  • 11
  • 8
19 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 21791999
They are both handles. HWND is a window handle and HMODULE is a module handle. They are used to represent, internally to the WIN32 API, a windows and a module respectively. I'm sure you've already figure this out though, so is there anything specific you don't get?
0
 

Author Comment

by:sham_ibmgs
ID: 21792046
@evil: I already figure this out but, i would like to know, when shoule i use HWND and when should i use HMODULE from application perspective? and any difference between HWND and HMODULE?

am new to this kind of GUI programming!!!!!!

Regards
Sham
0
 

Author Comment

by:sham_ibmgs
ID: 21792059
@evil: What do u mean, when u say, "internally to the WIN32 API, a windows and a module respectively"


Regards
Sham
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 40

Accepted Solution

by:
evilrix earned 100 total points
ID: 21792121
I'm not sure what you mean in so far as when to use which. The documentation for a specific WIN32 API function will tell you whether it expects a handle to a window to a handle to a module. Generally, if the function is manipulating a window it will expect a HWND and if it is working with a module it will expect a HMODULE. For example, GetWindowText(), given a HWND to a window will give you the title for that window.

>> What do u mean, when u say, "internally to the WIN32 API, a windows and a module respectively"
The handle is just an opaque 32 bit value that is created by the API to reference an object. The relationship between this and the object it represents is managed by the API, you just need to pass it the handle to the object you wish to reference. For example, when you call CreateWindow, you'll get a handle to the window created. You can then reference that window using this handle. How this handle gets created or what it actually represents internally (it might be a pointer or an index into a table for example) to the API is not relevant to you (this is by design, you shouldn't need to know!), you just need to know that it references the window you've created.
0
 

Author Comment

by:sham_ibmgs
ID: 21792168
@evil: WHat about HMODULE? As u said HWND points to any window kind of object, To which type of object does HMODULE point?

Regards
Sham
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792185
HMODULE represent a module... a module would be a DLL or an executable (EXE).
See the help for GetModuleHandle() and GetModuleFilename()
http://msdn.microsoft.com/en-us/library/ms683199(VS.85).aspx
http://msdn.microsoft.com/en-us/library/ms683197(VS.85).aspx
0
 

Author Comment

by:sham_ibmgs
ID: 21792191
@evil: what do u mean by term module here: "  if it is working with a module it will expect a HMODULE"

module mean a function?

Regards
Sham
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792195
>> if it is working with a module it will expect a HMODULE
If the function is used to work with or manupulate a module, for example GetModuleFilename() returns the filename of a module (see my post above).
0
 

Author Closing Comment

by:sham_ibmgs
ID: 31467526
Done
0
 

Author Comment

by:sham_ibmgs
ID: 21792370
Hello

We have a code where  HMODULE object is asssigned to HWND object as below:

hAPPINST=hResourceModule;

hAPPINST is of type HWND and hResourceModule is of type HMODULE

Regards
Sham
 


0
 

Author Comment

by:sham_ibmgs
ID: 21792376
@evil: How can a module type handle be assigned to window type handle?

Regards
Sham
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792386
Without seeing the code and the context I can't answer that... a HWND and a HMODULE are not related types, I presume there is a cast involved in this otherwise it shouldn't compile?
#include <windows.h>
 
int main()
{
	HWND hWnd = 0;
	HMODULE hModule = hWnd;
}
 
1>------ Build started: Project: testr, Configuration: Debug Win32 ------
1>Compiling...
1>main.cpp
1>main.cpp(6) : error C2440: 'initializing' : cannot convert from 'HWND' to 'HMODULE'
1>        Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file:BuildLog.htm"
1>testr - 1 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Open in new window

0
 

Author Comment

by:sham_ibmgs
ID: 21792439
@evil: Here is the code
typedef struct tagAPPINFO {
   INT      iVersion;
   INT      iFiller;

   HWND     hAppInst;
   INT      iAppType;

   HWND     hWndFrame;
   HWND     hWndMDIClient;
   HWND     hWndToolbar;
   HWND     hWndStatbar;

   UINT     uMemBlocks;
   UINT     uMemBlockSize;

   char     szAppName  [MAXOBJNAME      + 2];
   char     szFileName [FILENAME_LENGTH + 2];
   char     szHelpFile [PATHNAME_LENGTH + 2];
   char     szPathName [PATHNAME_LENGTH + 2];
   BOOL     bDeltaMode;
   char     szSysPathName[PATHNAME_LENGTH + 2];
   } APPINFO;

static HMODULE hResourceModuke;
APPINFO  appInfo;
#define hAPPINST  appInfo.hAppInst  // this is header file  

int WINAPI TNGMain (LPSTR lpszCmdLine, ULONG TNGContext, int nCmdShow, HWND hParent)
{
/* clear application information structure */
   memset  (&appInfo, '\0', sizeof (APPINFO));

   /* save instance handle in structure */
   hAPPINST = hResourceModule;
   hWndParent = hParent;

   /* set other application information in structure */
   appInfo.iVersion      = iVERSION;
   appInfo.uMemBlocks    = NUMMEMBLOCKS;
   appInfo.uMemBlockSize = MEMBLOCKSIZE;
   strncpy(appInfo.szAppName,  szAPPNAME, MAXOBJNAME);
   strcpy(appInfo.szFileName, szFILENAME);
   strcpy(appInfo.szHelpFile, sz2DHELPFILE);

   /* register window classes if this is the first instance */
   if (!RegisterClasses (hAPPINST))
      return FALSE;
.
.
.
}

Regards
Sham

0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792470
Are you sure that...

#define hAPPINST  appInfo.hAppInst  // this is header file  

...isn't being conditionally precompile to be either a HWND or a HMODULE depending upon build settings? Try changing hAPPINST = hResourceModule to be appInfo.hAppInst = hResourceModule and also, put a break point on this line to confirm the type is HWND. As you an see above, the types are not compatible under normal circumstances.
0
 

Author Comment

by:sham_ibmgs
ID: 21792682
am sure about it.

i wrote:

appInfo.hAppInst = hResourceModule; and compiled, it worked

Regards
Sham
 
0
 

Author Comment

by:sham_ibmgs
ID: 21792697
@evil: i placed another question with subject "wndclass and registerclass", Can u please look into that?

Regards
Sham
0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792718
Then, I have to be honest, I don't know. I tested this in VS2005 and the types are not compatible.
0
 

Author Comment

by:sham_ibmgs
ID: 21792720
@evil: How do i know, whether, currently HWND/HMODULE pointer is pointed to some object or is NULL? can we write some debug statement?

Regards
Sham


0
 
LVL 40

Expert Comment

by:evilrix
ID: 21792750
>> How do i know, whether, currently HWND/HMODULE pointer is pointed to some object or is NULL?
Something like this maybe?
#include <windows.h>
 
int main()
{
	HWND hWnd = NULL;
	HMODULE hModule = NULL;
 
	if(NULL == hWnd) { /* it's null */ }
	if(NULL == hModule) { /* it's null */ }
 
	if(NULL != hWnd) { /* it's not null */ }
	if(NULL != hModule) { /* it's not null */ }
}

Open in new window

0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Auto build a project upon execution 20 82
How to split this in C++ 4 101
Beginner to Unreal Engine 4 5 79
Access to class from any project within a solution. 6 23
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now