home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n12 / undr1295.exe / WIN95WND.CPP next >
C/C++ Source or Header  |  1995-12-01  |  3KB  |  103 lines

  1. //==================================
  2. // WIN95WND - Matt Pietrek 1995
  3. // FILE: WIN95WND.CPP
  4. //==================================
  5. #define WIN32_LEAN_AND_MEAN
  6. #include <windows.h>
  7. #include <stdio.h>
  8. #pragma hdrstop
  9.  
  10. //------------------------ Prototypes ---------------------------------------
  11. // Get the 32 bit linear address of USER.EXE's DGROUP segment
  12. DWORD GetUSER16DGroupLinearAddress( void );
  13.  
  14. // Given an HWND, return the linear address of its corresponding WND structure
  15. PVOID HWndToLinearAddress( HWND hWnd );
  16.  
  17. // Self-explanatory
  18. BOOL IsWindows95( void );
  19.  
  20. //-------------------------- Start of code ----------------------------------
  21.  
  22. int main( int argc, char *argv[] )
  23. {
  24.     HWND hWndDesktop, hWndDesktop2;
  25.     PVOID pDesktopWindow;               // Linear address of the WND structure
  26.  
  27.     if ( !IsWindows95() )
  28.     {
  29.         printf( "%s only runs on Windows 95\n", argv[0] );
  30.         return 1;
  31.     }
  32.         
  33.     hWndDesktop = GetDesktopWindow();   // Get the desktop HWND
  34.  
  35.     // Get the linear address of the desktop HWND
  36.     pDesktopWindow = HWndToLinearAddress( hWndDesktop );
  37.  
  38.     // Reach 0x46 bytes into the WND structure and grab out the 16 bit HWND
  39.     hWndDesktop2 = (HWND) *(PWORD)((DWORD)pDesktopWindow + 0x46);
  40.  
  41.     printf("Desktop HWND:%04X  Address:%08X  HWND from WND struct:%04X",
  42.             hWndDesktop, pDesktopWindow, hWndDesktop2 );
  43.  
  44.     return 0;
  45. }
  46.  
  47. // Given an HWND, return the linear address of its corresponding WND structure
  48. PVOID HWndToLinearAddress( HWND hWnd )
  49. {
  50.     if ( !IsWindow(hWnd) )
  51.         return 0;
  52.  
  53.     DWORD user16DgroupBase = GetUSER16DGroupLinearAddress();
  54.     if ( !user16DgroupBase )
  55.         return 0;
  56.  
  57.     // Add USER.EXE's DGROUP address, the handle conversion table
  58.     // offset (0x10000), and the hWnd value to get a pointer to the
  59.     // WND pointer.  This WND ptr is relative to USER.EXE's DGROUP.
  60.     DWORD WND_offset = *(PDWORD)(user16DgroupBase + 0x10000 + (DWORD)hWnd);
  61.  
  62.     // Take the USER.EXE DGROUP relative pointer, add the USER.EXE DGROUP
  63.     // base to it, and return the result.
  64.     return (PVOID)(user16DgroupBase + WND_offset);
  65. }
  66.  
  67. // Get the 32 bit linear address of USER.EXE's DGROUP segment
  68. DWORD GetUSER16DGroupLinearAddress( void )
  69. {
  70.     // The desktop window is created by USER.EXE, so it will have USER.EXE's
  71.     // HINSTANCE
  72.     DWORD USER16_hInstance = GetWindowLong(GetDesktopWindow(), GWL_HINSTANCE);
  73.  
  74.     if ( !USER16_hInstance )
  75.         return 0;
  76.     
  77.     LDT_ENTRY descriptor;
  78.  
  79.     // Use GetThreadSelectorEntry to copy the descriptor associated with
  80.     // USER.EXE's DGROUP selector
  81.     if ( FALSE == GetThreadSelectorEntry( GetCurrentThread(),
  82.                                           USER16_hInstance,
  83.                                           &descriptor) )
  84.         return 0;
  85.  
  86.     // Assemble the components of the descriptor into a usable linear address
  87.     return  (descriptor.HighWord.Bytes.BaseHi << 24 )
  88.           + (descriptor.HighWord.Bytes.BaseMid << 16 )
  89.           +  descriptor.BaseLow;
  90. }
  91.  
  92. // Self-explanatory
  93. BOOL IsWindows95( void )
  94. {
  95.     if ( 0 == (GetVersion() & 0x80000000) ) // High bit not set for NT
  96.         return FALSE;
  97.     
  98.     if ( LOBYTE(GetVersion()) != 4 )        // Win95 is version 4.  Win32s
  99.         return FALSE;                       // shows up as version 3.
  100.                                             // Disallow all other versions.
  101.     return TRUE;
  102. }
  103.