www.pudn.com > ExpBar_src.zip > dllmain.c


/* 
 * file           : dllmain.c 
 * language       : ANSI/ISO C 
 * plattform      : Win32 (Windows 98/Me/NT/2000/XP) 
 * description    : entry point and global stuff for explorerbar.dll 
 * 
 * revision history: 
 * ================= 
 * 
 * date:       author:           description: 
 * -------------------------------------------------------------------------- 
 * 02/17/2004  Ingo A. Kubbilun  first published version 1.0 
 */ 
 
#include  
#include  
  
#pragma data_seg(".dllshared") 
static UINT g_uProcesses = 0L; 
       UINT g_uOSVersion = UNKNOWN_WINDOWS; 
#pragma data_seg()         
#pragma comment(linker, "/section:.dllshared,RWS") 
 
HINSTANCE         g_hInstDLL    = NULL; 
BOOL              g_bXPTheme    = FALSE; 
HMODULE           g_hUxThemeDLL = NULL; 
BOOL              g_bEBI        = FALSE; 
EXPBARINFO        g_sEBI; 
 
static void* APIENTRY GetXPThemeProc ( LPCSTR szProc, void* pfnFail ); 
 
BOOL WINAPI DllMain ( HINSTANCE hinstDLL, 
                      DWORD     fdwReason, 
                      LPVOID    lpvReserved ) 
{ 
  switch(fdwReason) 
  { 
    case DLL_PROCESS_ATTACH: 
      if (!g_uProcesses) 
      { 
        if (!InitGlobal()) 
          return FALSE; 
      } 
      if (!InitProcess(hinstDLL)) 
        return FALSE; 
      g_uProcesses++; 
      break; 
 
    case DLL_PROCESS_DETACH: 
      if (g_uProcesses) 
      { 
        g_uProcesses--; 
        if (!g_uProcesses) 
        { 
          if (!CleanUpGlobal()) 
            return FALSE; 
        } 
      } 
      if (!CleanUpProcess()) 
        return FALSE; 
      break; 
 
    case DLL_THREAD_ATTACH: 
      break; 
     
    case DLL_THREAD_DETACH: 
      break; 
     
    default: 
      break; 
  } 
  return TRUE; 
} 
 
BOOL APIENTRY InitGlobal ( void ) 
{ 
  OSVERSIONINFOEX osvi; 
  BOOL            bOsVersionInfoEx; 
 
  ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX)); 
  osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX); 
 
  if( !(bOsVersionInfoEx = GetVersionEx ((OSVERSIONINFO *) &osvi)) ) 
  { 
    osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO); 
    if (! GetVersionEx ( (OSVERSIONINFO *) &osvi) )  
      return FALSE; 
  } 
 
  g_uOSVersion = UNKNOWN_WINDOWS; 
  switch (osvi.dwPlatformId) 
  { 
    case VER_PLATFORM_WIN32_NT: 
 
      if ((osvi.dwMajorVersion > 5) || 
          ((osvi.dwMajorVersion==5) && (osvi.dwMinorVersion>2)) 
         ) 
        g_uOSVersion = POST_WINDOWS_XP; 
      else 
      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) 
        g_uOSVersion = WINDOWS_DOTNET_SERVER_2003; 
      else 
      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1 ) 
        g_uOSVersion = WINDOWS_XP; 
      else 
      if ( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) 
        g_uOSVersion = WINDOWS_2000; 
      else 
      if ( osvi.dwMajorVersion <= 4 ) 
        g_uOSVersion = WINDOWS_NT; 
 
      if ( bOsVersionInfoEx ) 
      { 
        if ( osvi.wProductType == VER_NT_WORKSTATION ) 
        { 
          if( osvi.dwMajorVersion == 4 ) 
            g_uOSVersion |= WINDOWS_NT_WORKSTATION_4; 
          else  
          if( osvi.wSuiteMask & VER_SUITE_PERSONAL ) 
            g_uOSVersion |= WINDOWS_HOME_EDITION; 
          else 
            g_uOSVersion |= WINDOWS_PROFESSIONAL_EDITION; 
        } 
        else  
        if ( osvi.wProductType == VER_NT_SERVER ) 
        { 
          if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2 ) 
          { 
            if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) 
              g_uOSVersion |= WINSERVER_DATACENTER_EDITION; 
            else  
            if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) 
              g_uOSVersion |= WINSERVER_ENTERPRISE_EDITION; 
            else  
            if ( osvi.wSuiteMask == VER_SUITE_BLADE ) 
              g_uOSVersion |= WINSERVER_WEB_EDITION; 
            else 
              g_uOSVersion |= WINSERVER_STANDARD_EDITION; 
          } 
          else  
          if( osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0 ) 
          { 
            if( osvi.wSuiteMask & VER_SUITE_DATACENTER ) 
              g_uOSVersion |= WINSERVER_DATACENTER_EDITION; 
            else  
            if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) 
              g_uOSVersion |= WINSERVER_ADVANCED_SERVER; 
            else 
              g_uOSVersion |= WINSERVER_SERVER; 
          } 
          else  // Windows NT 4.0  
          { 
            if( osvi.wSuiteMask & VER_SUITE_ENTERPRISE ) 
              g_uOSVersion |= WINSERVER_SERVER4_ENTERPRISE_EDITION; 
            else 
              g_uOSVersion |= WINSERVER_SERVER4; 
          } 
        } 
      } 
      else 
      { 
        HKEY    hKey; 
        TCHAR   szProductType[80]; 
        DWORD   dwBufLen=80; 
        LONG    lRet; 
 
        lRet = RegOpenKeyEx( HKEY_LOCAL_MACHINE,TEXT("SYSTEM\\CurrentControlSet\\Control\\ProductOptions"), 
                             0, KEY_QUERY_VALUE, &hKey ); 
        if( lRet != ERROR_SUCCESS ) 
          return FALSE; 
 
        lRet = RegQueryValueEx( hKey, TEXT("ProductType"), NULL, NULL, 
                               (LPBYTE) szProductType, &dwBufLen); 
        if( (lRet != ERROR_SUCCESS) || (dwBufLen > 80) ) 
          return FALSE; 
 
        RegCloseKey( hKey ); 
 
        if ( lstrcmpi( __T("WINNT"), szProductType) == 0 ) 
          g_uOSVersion |= WINDOWS_WORKSTATION; 
        if ( lstrcmpi( __T("LANMANNT"), szProductType) == 0 ) 
          g_uOSVersion |= WINSERVER_SERVER; 
        if ( lstrcmpi( __T("SERVERNT"), szProductType) == 0 ) 
          g_uOSVersion |= WINSERVER_ADVANCED_SERVER; 
      } 
      break; 
 
    case VER_PLATFORM_WIN32_WINDOWS: 
      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 0) 
      { 
        if ( osvi.szCSDVersion[1] == 'C' || osvi.szCSDVersion[1] == 'B' ) 
          g_uOSVersion = WINDOWS_95_OSR2; 
        else 
          g_uOSVersion = WINDOWS_95; 
      }  
      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 10) 
      { 
        if ( osvi.szCSDVersion[1] == 'A' ) 
          g_uOSVersion = WINDOWS_98_SE; 
        else 
          g_uOSVersion = WINDOWS_98; 
      }  
 
      if (osvi.dwMajorVersion == 4 && osvi.dwMinorVersion == 90) 
      { 
        g_uOSVersion = WINDOWS_ME; 
      }  
      break; 
 
    case VER_PLATFORM_WIN32s: 
      g_uOSVersion = WINDOWS_WIN32s; 
      break; 
  } 
  return TRUE; 
} 
 
BOOL APIENTRY CleanUpGlobal ( void ) 
{ 
 
 
  return TRUE; 
} 
 
BOOL APIENTRY InitProcess ( HINSTANCE hinstDLL ) 
{ 
  WNDCLASSEX      wcx; 
 
  g_hInstDLL = hinstDLL; 
 
  DisableThreadLibraryCalls(hinstDLL); 
 
  InitCommonControls(); 
 
  memset(&wcx,0,sizeof(wcx)); 
  wcx.cbSize 			  = sizeof(WNDCLASSEX); 
	wcx.style         = CS_GLOBALCLASS; 
	wcx.lpfnWndProc   = ColXPBarWndProc; 
	wcx.cbClsExtra    = 0; 
	wcx.cbWndExtra    = 0; 
	wcx.hInstance     = hinstDLL; 
	wcx.hIcon         = (HICON) NULL; 
	wcx.hCursor       = LoadCursor(NULL,IDC_ARROW); 
	wcx.hbrBackground = NULL; 
	wcx.lpszMenuName  = NULL; 
	wcx.lpszClassName = COLXPBARCLASSNAME; 
	wcx.hIconSm       = (HICON) NULL; 
	RegisterClassEx(&wcx); 
 
  memset(&wcx,0,sizeof(wcx)); 
  wcx.cbSize 			  = sizeof(WNDCLASSEX); 
	wcx.style         = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW; 
	wcx.lpfnWndProc   = TaskLinkWndProc; 
	wcx.cbClsExtra    = 0; 
	wcx.cbWndExtra    = 0; 
	wcx.hInstance     = hinstDLL; 
	wcx.hIcon         = (HICON) NULL; 
	wcx.hCursor       = LoadCursor(NULL,IDC_ARROW); 
	wcx.hbrBackground = NULL; 
	wcx.lpszMenuName  = NULL; 
	wcx.lpszClassName = TASKLINKCLASSNAME; 
	wcx.hIconSm       = (HICON) NULL; 
	RegisterClassEx(&wcx); 
 
  memset(&wcx,0,sizeof(wcx)); 
  wcx.cbSize 			  = sizeof(WNDCLASSEX); 
	wcx.style         = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW; 
	wcx.lpfnWndProc   = CollapsibleHeaderWndProc; 
	wcx.cbClsExtra    = 0; 
	wcx.cbWndExtra    = 0; 
	wcx.hInstance     = hinstDLL; 
	wcx.hIcon         = (HICON) NULL; 
	wcx.hCursor       = LoadCursor(NULL,IDC_ARROW); 
	wcx.hbrBackground = NULL; 
	wcx.lpszMenuName  = NULL; 
	wcx.lpszClassName = COLHEADERCLASSNAME; 
	wcx.hIconSm       = (HICON) NULL; 
	RegisterClassEx(&wcx); 
 
  memset(&wcx,0,sizeof(wcx)); 
  wcx.cbSize 			  = sizeof(WNDCLASSEX); 
	wcx.style         = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; 
	wcx.lpfnWndProc   = CBStaticWndProc; 
	wcx.cbClsExtra    = 0; 
	wcx.cbWndExtra    = 0; 
	wcx.hInstance     = hinstDLL; 
	wcx.hIcon         = (HICON) NULL; 
	wcx.hCursor       = LoadCursor(NULL,IDC_ARROW); 
	wcx.hbrBackground = NULL; 
	wcx.lpszMenuName  = NULL; 
	wcx.lpszClassName = COLBARSTATICCLASSNAME; 
	wcx.hIconSm       = (HICON) NULL; 
	RegisterClassEx(&wcx); 
 
  memset(&wcx,0,sizeof(wcx)); 
  wcx.cbSize 			  = sizeof(WNDCLASSEX); 
	wcx.style         = CS_GLOBALCLASS|CS_HREDRAW|CS_VREDRAW|CS_DBLCLKS; 
	wcx.lpfnWndProc   = CBButtonWndProc; 
	wcx.cbClsExtra    = 0; 
	wcx.cbWndExtra    = 0; 
	wcx.hInstance     = hinstDLL; 
	wcx.hIcon         = (HICON) NULL; 
	wcx.hCursor       = LoadCursor(NULL,IDC_ARROW); 
	wcx.hbrBackground = NULL; 
	wcx.lpszMenuName  = NULL; 
	wcx.lpszClassName = COLBARBUTTONCLASSNAME; 
	wcx.hIconSm       = (HICON) NULL; 
	RegisterClassEx(&wcx); 
 
  // Windows XP Themes stuff 
 
  g_bXPTheme    = FALSE; 
  g_hUxThemeDLL = NULL; 
  if (g_uOSVersion>=WINDOWS_XP) 
  { 
    if (FindResource(GetModuleHandle(NULL),MAKEINTRESOURCE(MANIFEST_RESOURCE_ID),MAKEINTRESOURCE(RT_MANIFEST))!=NULL) 
    { 
      g_hUxThemeDLL = LoadLibrary(TEXT("UxTheme.dll")); 
      if (g_hUxThemeDLL) 
      { 
        g_bXPTheme = TRUE; 
      } 
    } 
  } 
 
  memset(&g_sEBI,0,sizeof(EXPBARINFO)); 
  g_sEBI.cbSize = sizeof(EXPBARINFO); 
  g_bEBI = GetExplorerBarInfo(&g_sEBI,FALSE); 
 
  return TRUE; 
} 
 
BOOL APIENTRY CleanUpProcess ( void ) 
{ 
  if (g_bEBI) 
  { 
    FreeExplorerBarInfo(&g_sEBI); 
    g_bEBI = FALSE; 
  } 
 
  if (g_hUxThemeDLL) 
  { 
    FreeLibrary(g_hUxThemeDLL); 
    g_hUxThemeDLL = NULL; 
  } 
 
  UnregisterClass(COLXPBARCLASSNAME,g_hInstDLL); 
	UnregisterClass(TASKLINKCLASSNAME,g_hInstDLL); 
	UnregisterClass(COLHEADERCLASSNAME,g_hInstDLL); 
	UnregisterClass(COLBARSTATICCLASSNAME,g_hInstDLL); 
	UnregisterClass(COLBARBUTTONCLASSNAME,g_hInstDLL); 
 
  return TRUE; 
} 
 
// e.g. "Explorer\\Navigating" 
// ".Default\\Minimize" 
// ".Default\\Maximize" 
void APIPRIVATE PlaySoundThemeSound ( LPCTSTR lpcszRegAppSound ) 
{ 
  HKEY        hKey = NULL; 
  TCHAR       szString[256]; 
  TCHAR       szWaveFile[MAX_PATH]; 
  TCHAR       szWaveFileExpanded[MAX_PATH]; 
  DWORD       dwType,dwSize = MAX_PATH; 
   
  szWaveFile[0]=0x00; 
  wsprintf(szString,_T("AppEvents\\Schemes\\Apps\\%s\\.Current"),lpcszRegAppSound); 
  if ((ERROR_SUCCESS==RegOpenKey(HKEY_CURRENT_USER,szString,&hKey)) && (hKey)) 
  { 
    if (ERROR_SUCCESS==RegQueryValueEx(hKey,NULL,NULL,&dwType,(LPBYTE)szWaveFile,&dwSize)) 
    { 
      if (dwType==REG_EXPAND_SZ) 
      { 
        if (ExpandEnvironmentStrings(szWaveFile,szWaveFileExpanded,MAX_PATH)) 
          PlaySound(szWaveFileExpanded,NULL,SND_ASYNC|SND_FILENAME|SND_NODEFAULT|SND_NOSTOP|SND_NOWAIT); 
      } 
      else 
      if (dwType==REG_SZ) 
      { 
        PlaySound(szWaveFile,NULL,SND_ASYNC|SND_FILENAME|SND_NODEFAULT|SND_NOSTOP|SND_NOWAIT); 
      } 
    } 
    RegCloseKey(hKey); 
  } 
} 
 
BOOL APIENTRY InitExpBarDll ( void ) 
{ 
  return TRUE; 
} 
 
void APIENTRY CleanUpExpBarDll ( void ) 
{ 
} 
 
#define BACK_R          255 
#define BACK_G          0 
#define BACK_B          0 
 
#define NRM_ARROW_R     13 
#define NRM_ARROW_G     14 
#define NRM_ARROW_B     15 
 
#define DIS_ARROW_R     16 
#define DIS_ARROW_G     17 
#define DIS_ARROW_B     18 
 
HIMAGELIST APIPRIVATE CreateArrowImageList ( HINSTANCE     hInstance, 
                                             UINT          uResourceId, 
                                             UINT          uIconWidth, 
                                             PUINT         puNumIcons, 
                                             PUINT         puIconHeight ) 
{ 
  HDC               hDC; 
  HIMAGELIST        hIList = NULL; 
  HBITMAP           hSrcBitmap,hBmpMask,hBmpImg; 
  DIBSECTION        ds; 
  BITMAPINFO        bif; 
  BITMAPINFOHEADER  bih; 
  UINT              uRealWidth; 
  COLORREF          crArrowNrm = GetSysColor(COLOR_BTNTEXT); 
  COLORREF          crArrowDis = GetSysColor(COLOR_GRAYTEXT); 
  BYTE              byAN_R     = GetRValue(crArrowNrm); 
  BYTE              byAN_G     = GetGValue(crArrowNrm); 
  BYTE              byAN_B     = GetBValue(crArrowNrm); 
  BYTE              byAD_R     = GetRValue(crArrowDis); 
  BYTE              byAD_G     = GetGValue(crArrowDis); 
  BYTE              byAD_B     = GetBValue(crArrowDis); 
  PBYTE             pbyBuffer,pbyData; 
  int               i,j; 
 
  *puNumIcons   = 0; 
  *puIconHeight = 0; 
 
  if (!uIconWidth)  
    return NULL; 
 
  hDC = GetDC(NULL); 
  if (!hDC)  
    return hIList; 
 
  hSrcBitmap = LoadImage(hInstance,MAKEINTRESOURCE(uResourceId),IMAGE_BITMAP,0,0,LR_CREATEDIBSECTION); 
  if (!hSrcBitmap) 
  { 
    ReleaseDC(NULL,hDC); 
    return hIList; 
  } 
 
  memset(&ds,0,sizeof(ds)); 
  if (GetObject(hSrcBitmap,sizeof(DIBSECTION),&ds)!=sizeof(DIBSECTION)) 
  { 
    ErrorExit1: 
    DeleteObject(hSrcBitmap); 
    ReleaseDC(NULL,hDC); 
    return hIList; 
  } 
 
  if (ds.dsBm.bmBitsPixel!=24) 
    goto ErrorExit1; 
 
  if (ds.dsBm.bmWidth % ((int)uIconWidth)) 
    goto ErrorExit1; 
 
  *puNumIcons = (UINT)(ds.dsBm.bmWidth / ((int)uIconWidth)); 
 
  *puIconHeight = (UINT)ds.dsBm.bmHeight; 
 
  uRealWidth = ds.dsBm.bmWidthBytes; 
 
  pbyBuffer = (PBYTE)malloc(uRealWidth*ds.dsBm.bmHeight); 
  if (!pbyBuffer) 
    goto ErrorExit1; 
 
  memcpy(pbyBuffer,ds.dsBm.bmBits,uRealWidth*ds.dsBm.bmHeight); 
  for (i=0;i