home *** CD-ROM | disk | FTP | other *** search
/ Troubleshooting Netware Systems / CSTRIAL0196.BIN / attach / msj / v10n04 / multilin.exe / TRANSLAT.CPP < prev    next >
C/C++ Source or Header  |  1995-04-01  |  8KB  |  276 lines

  1. // translat.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "global.h"
  6. #include "translat.h"
  7.  
  8. #ifdef _DEBUG
  9. #undef THIS_FILE
  10. static char BASED_CODE THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. /////////////////////////////////////////////////////////////////////////////
  14. // CTranslations dialog
  15.  
  16.  
  17. CTranslations::CTranslations(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CTranslations::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CTranslations)
  21.     m_SysTrans = _T("");
  22.     //}}AFX_DATA_INIT
  23. }
  24.  
  25.  
  26. void CTranslations::DoDataExchange(CDataExchange* pDX)
  27. {
  28.     CDialog::DoDataExchange(pDX);
  29.     //{{AFX_DATA_MAP(CTranslations)
  30.     DDX_Text(pDX, IDC_SYSTRANS, m_SysTrans);
  31.     //}}AFX_DATA_MAP
  32. }
  33.  
  34.  
  35. BEGIN_MESSAGE_MAP(CTranslations, CDialog)
  36.     //{{AFX_MSG_MAP(CTranslations)
  37.     //}}AFX_MSG_MAP
  38. END_MESSAGE_MAP()
  39.  
  40.  
  41. /////////////////////////////////////////////////////////////////////////////
  42. // CTranslations message handlers
  43. #define GT_SYSTEMFILE TRUE
  44. typedef UINT CPID;    // code page identifier
  45.  
  46. BOOL GetTranslations(
  47.     CString &str,        // reference to string variable
  48.     LPTSTR  lpszFile,    // address of filename
  49.     BOOL    fSysDir        // True if in System directory
  50.     )
  51. {            
  52.     DWORD  dwHandle=0;    // unused parameter for some APIs,must be 0
  53.     const int cchSysDir = MAX_PATH;
  54.     TCHAR * lpszSysDir = new TCHAR[cchSysDir];
  55.  
  56.     // if file is a system file, look in system directory
  57.     if(fSysDir)
  58.     {
  59.         UINT cchActual = GetSystemDirectory(lpszSysDir, cchSysDir);
  60.         if( cchActual >= cchSysDir + _tcslen(lpszFile) )
  61.         {
  62.             // Not enough space for system dir path
  63.         } else {
  64.             lpszSysDir = _tcscat(lpszSysDir, TEXT("\\"));
  65.             lpszFile = _tcscat(lpszSysDir, lpszFile);    
  66.         }
  67.     }
  68.  
  69.     DWORD cbBuf=GetFileVersionInfoSize(
  70.             lpszFile,    // address of filename
  71.             &dwHandle     // always set to zero
  72.     );    
  73.  
  74.     BYTE * lpvData = new BYTE[cbBuf];
  75.  
  76.     if(!lpvData)
  77.     {
  78.         // memory allocation failed
  79.         return FALSE;
  80.     }
  81.     BOOL rc = GetFileVersionInfo(
  82.             lpszFile,    // address of filename
  83.             dwHandle,    // ignored
  84.             cbBuf,        // size of buffer
  85.             lpvData     // address of buffer for file-version info.
  86.     );    
  87.  
  88.     // VerQueryValue returns pairs of Language/characterset information
  89.     // (need to use debug to get the exact format)
  90.     UINT cbTrans = 0;
  91.     DWORD *lpdwTrans = NULL;
  92.     if(!(rc= VerQueryValue(lpvData,_T("\\VarFileInfo\\Translation"), (VOID**)&lpdwTrans, &cbTrans)))
  93.     {
  94.         DWORD dwerr = GetLastError();
  95.     } 
  96.     else 
  97.     {
  98.            // Get the language name for
  99.         for(int i = cbTrans/sizeof(DWORD); i > 0; --i)  // each language
  100.         {
  101.  
  102.  
  103.             LCID lcid = LOWORD(lpdwTrans[i-1]);
  104. #if 0
  105.             CPID cpid = HIWORD(lpdwTrans[i-1]);
  106.             LCIDToCString(lcid, str);
  107. #else
  108.             UINT cchLangName = 32;
  109.             DWORD cchActual = VerLanguageName(lcid, 
  110.                           str.GetBufferSetLength(cchLangName), 
  111.                           cchLangName); 
  112.             if( cchActual > cchLangName )
  113.             {
  114.                 // Language name doesn't fit the buffer
  115.             }
  116.             // Add name to listbox
  117.             {
  118.             }
  119. #endif
  120.         }
  121.     }
  122.  
  123.     delete []lpszSysDir;
  124.     delete []lpvData; 
  125.  
  126.     return TRUE;    // for now
  127. }
  128.  
  129. /////////////////////
  130. // The following code will locate all the available languages for a resource,
  131. // no matter whether you are using resources in separate DLLs, resources in 
  132. // the EXE, or both. This example assumes that the DLLs follow the naming 
  133. // convention from the earlier example. With a sufficiently unique combination
  134. // of resource name and type as a flag you could search for your DLLs anywhere
  135. // on the path, no matter what their names.
  136.  
  137. BOOL CALLBACK EnumResLangProc(
  138.  
  139. HINSTANCE hModule,         // resource-module handle 
  140. LPCTSTR lpszType,         // address of resource type 
  141. LPCTSTR lpszName,         // address of resource name 
  142. LANGID  wIDLanguage,     // resource language identifier 
  143. LPARAM  lParam)         // application-defined parameter  
  144. {
  145.     PLBDATA pLBData = (PLBDATA)lParam;
  146.     static TCHAR szItemText[ NAMESTART + MAX_PATH];
  147.  
  148.                                 // Display language ID components in the list
  149.                                 // box text. The format of the text will be:
  150.                                 // PriLang SubLang - File Name
  151.  
  152.     wsprintf( szItemText, TEXT("%#02x %#02x - "), 
  153.                           PRIMARYLANGID(wIDLanguage),
  154.                           SUBLANGID(wIDLanguage));
  155.  
  156.                                 // Append the name of the file containing the
  157.                                 // resource.
  158.     if ( GetModuleFileName( hModule, 
  159.                             &szItemText[ lstrlen( szItemText)], 
  160.                             MAX_PATH) > 0 )
  161.     {
  162.  
  163.                                 // Add the line to the list box
  164.         ::SendDlgItemMessage( pLBData->hWnd, pLBData->nListBox, LB_ADDSTRING, 
  165.                             0, (LPARAM)szItemText);
  166.     }
  167.     else
  168.     {                           // Send reason for the failure of the
  169.                                 // GetModuleFileName call back to our caller.
  170.         pLBData->dwErrCode = GetLastError();
  171.     }
  172.                                 // If this call failed, cause the enumeration
  173.                                 // process in EnumResourceLanguages to halt.
  174.     return( pLBData->dwErrCode == ERROR_SUCCESS ? TRUE : FALSE);
  175. }
  176.  
  177. ///////////////////////////
  178. // MyFindAllLanguages
  179. //
  180. // Fill the given list box with the languages in which the given resource is
  181. // available. This function will add the data to the given list box. No
  182. // assumption is made about any of the list box's attributes (sorted, etc.).
  183. // This function first empties the list box.
  184. //
  185. // Return code is ERROR_SUCCESS if successful or restults of GetLastError().
  186.  
  187. DWORD CTranslations::FindTranslationsOf( 
  188. LPCTSTR lpszType,   // Name or ID number of resource type to be enum'd
  189. LPCTSTR lpszName)   // Name or ID number of resource name to be enum'd
  190. {
  191.                                 // make sure the list box is empty
  192.     
  193.     LBDATA lbData;
  194.     lbData.hWnd      = m_hWnd;          // Fill in the LBData fields so the
  195.     lbData.nListBox  = IDC_APPTRANS;    // data gets sent to EnumResLangProc
  196.     lbData.dwErrCode = ERROR_SUCCESS;   // Assume we will be successful
  197.  
  198.     SendDlgItemMessage( lbData.nListBox, LB_RESETCONTENT, 0, 0);
  199.  
  200.     WIN32_FIND_DATA  ffd;         // for info from Find{First,Next}File
  201.  
  202.     HANDLE hFind = FindFirstFile(TEXT("???.DLL"), &ffd);
  203.  
  204.     if ( hFind != INVALID_HANDLE_VALUE )
  205.     {
  206.         do
  207.         {
  208.             HINSTANCE hModule = LoadLibrary( ffd.cFileName);
  209.  
  210.             if ( ! EnumResourceLanguages(hModule,  // Look in the .DLL
  211.                      lpszType,                 // Given resource type
  212.                      lpszName,                 // Given resource name
  213.                     (ENUMRESLANGPROC) 
  214.                         EnumResLangProc,    // address of callback function
  215.                     (LPARAM)&lbData) )        // application-defined parameter
  216.             {
  217.                 lbData.dwErrCode = GetLastError();
  218.             }
  219.             FreeLibrary(hModule);
  220.         } while( lbData.dwErrCode == ERROR_SUCCESS 
  221.               && FindNextFile(hFind, &ffd) );
  222.  
  223.         FindClose( hFind );
  224.     }
  225.     else
  226.     {
  227.         lbData.dwErrCode = GetLastError();
  228.     }
  229.     
  230.     if ( lbData.dwErrCode == ERROR_SUCCESS
  231.       || lbData.dwErrCode == ERROR_FILE_NOT_FOUND )
  232.     {
  233.         lbData.dwErrCode = ERROR_SUCCESS;
  234.  
  235.         if ( ! EnumResourceLanguages( NULL,    // look in current EXE
  236.                      lpszType,             // Given resource type
  237.                      lpszName,             // Given resource name
  238.                      (ENUMRESLANGPROC) 
  239.                         EnumResLangProc,// callback function
  240.                      (LPARAM)&lbData) )
  241.         {
  242.             lbData.dwErrCode = GetLastError();
  243.         }
  244.     }
  245.     return( lbData.dwErrCode);
  246. }
  247.  
  248. BOOL CTranslations::OnInitDialog() 
  249. {
  250.     CDialog::OnInitDialog();
  251.     
  252.     // TODO: Add extra initialization here
  253.           
  254.     // Translations field
  255.     BOOL rcgt= GetTranslations(m_SysTrans, _T("Kernel32.DLL"), GT_SYSTEMFILE);
  256.  
  257.  
  258.      // Additional translations
  259.         
  260.     DWORD dwRetCode = FindTranslationsOf( MAKEINTRESOURCE( RT_MENU),
  261.                                           MAKEINTRESOURCE( IDR_MAINFRAME));
  262.  
  263.     /*-----
  264.          rcgt= GetTranslations(m_UserTrans,_T("Global.EXE"), !GT_SYSTEMFILE);
  265.  
  266.     --*/
  267.  
  268.     UpdateData(FALSE);
  269.  
  270.     return TRUE;  // return TRUE unless you set the focus to a control
  271.                   // EXCEPTION: OCX Property Pages should return FALSE
  272. }
  273.  
  274.  
  275.  
  276.