Link to home
Create AccountLog in
Avatar of sham_ibmgs
sham_ibmgs

asked on

HWND and HMODULE

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
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

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?
Avatar of sham_ibmgs
sham_ibmgs

ASKER

@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
@evil: What do u mean, when u say, "internally to the WIN32 API, a windows and a module respectively"


Regards
Sham
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
@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
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
@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
>> 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).
Done
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
 


@evil: How can a module type handle be assigned to window type handle?

Regards
Sham
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

@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

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.
am sure about it.

i wrote:

appInfo.hAppInst = hResourceModule; and compiled, it worked

Regards
Sham
 
@evil: i placed another question with subject "wndclass and registerclass", Can u please look into that?

Regards
Sham
Then, I have to be honest, I don't know. I tested this in VS2005 and the types are not compatible.
@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


>> 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