WetCode

Untitled

Dec 8th, 2013
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.18 KB | None | 0 0
  1.  
  2. #include "IconChanger.h"
  3. #include <iostream>
  4.  
  5. using namespace std;
  6. #include <QMessageBox>
  7.  
  8. QString ICONPATH;
  9. QString EXEPATH;       /* Exe path with one \  */
  10. QString EXEPATHPADDED; /* Exe path with two \\ */
  11.  
  12.  
  13. BOOL ResLang( HMODULE hModule, LPCTSTR lpszType, LPCTSTR lpszName, WORD wIDLanguage, LONG_PTR lParam)
  14. {
  15.     if (!IS_INTRESOURCE(lpszName))
  16.     {
  17. //        cout << "LANG -: " << wIDLanguage << endl;
  18. //        cout << "TYPE -: " << lpszType << endl;
  19. //        cout << "NAME -: " << lpszName << endl;
  20.  
  21.  
  22.         FreeLibrary( hModule );
  23.  
  24.         HANDLE hCleanUpdate = BeginUpdateResource( EXEPATHPADDED.toStdString().c_str() , FALSE );
  25.         if ( hCleanUpdate == NULL )
  26.         {
  27.             QMessageBox mess;
  28.             mess.setText(QString("Start Clean UP FAILED: ").append(QString::number( GetLastError() ) ) );
  29.             mess.exec();
  30.         }
  31.  
  32.         if ( !UpdateResource( hCleanUpdate, lpszType, lpszName, wIDLanguage, NULL, 0) )
  33.         {
  34.             QMessageBox mess;
  35.             mess.setText(QString("Clean UP FAILED: : ").append(QString::number( GetLastError() ) ) );
  36.             mess.exec();
  37.             return FALSE;
  38.         }
  39.  
  40.  
  41.         if ( !EndUpdateResource( hCleanUpdate, FALSE) )
  42.         {
  43.             QMessageBox mess;
  44.             mess.setText(QString(" End Clean UP FAILED: ").append(QString::number( GetLastError() ) ) );
  45.             mess.exec();
  46.         }
  47.  
  48.         hModule = LoadLibrary( EXEPATH.toStdString().c_str() );
  49.         if ( hModule == NULL )
  50.         {
  51.             QMessageBox mess;
  52.             mess.setText(QString("Unable to get handle to EXE/DLL\n").append(EXEPATH).append("\nError Code: ").append( QString::number(GetLastError()) ) );
  53.             mess.exec();
  54.         }
  55.  
  56.     } else {
  57. //        cout << "LANG -: " << (long)wIDLanguage << endl;
  58. //        cout << "TYPE -: " << (long)lpszType << endl;
  59. //        cout << "NAME -: " << (long)lpszName << endl;
  60. //        cout << "FILE -: " << EXEPATHPADDED.toStdString().c_str() << endl;
  61.  
  62.         FreeLibrary( hModule );
  63.  
  64.         HANDLE hCleanUpdate = BeginUpdateResource( EXEPATHPADDED.toStdString().c_str() , FALSE );
  65.         if ( hCleanUpdate == NULL )
  66.         {
  67.             QMessageBox mess;
  68.             mess.setText(QString("Start Clean UP FAILED: ").append(QString::number( GetLastError() ) ) );
  69.             mess.exec();
  70.         }
  71.  
  72.         if ( !UpdateResource( hCleanUpdate, MAKEINTRESOURCE((long)lpszType), MAKEINTRESOURCE((long)lpszName), wIDLanguage, NULL, 0) )
  73.         {
  74.             QMessageBox mess;
  75.             mess.setText(QString("Clean UP FAILED: : ").append(QString::number( GetLastError() ) ) );
  76.             mess.exec();
  77.             return FALSE;
  78.         }
  79.  
  80.  
  81.         if ( !EndUpdateResource( hCleanUpdate, FALSE) )
  82.         {
  83.             QMessageBox mess;
  84.             mess.setText(QString(" End Clean UP FAILED: ").append(QString::number( GetLastError() ) ) );
  85.             mess.exec();
  86.         }
  87.  
  88.         hModule = LoadLibrary( EXEPATH.toStdString().c_str() );
  89.         if ( hModule == NULL )
  90.         {
  91.             QMessageBox mess;
  92.             mess.setText(QString("Unable to get handle to EXE/DLL\n").append(EXEPATH).append("\nError Code: ").append( QString::number(GetLastError()) ) );
  93.             mess.exec();
  94.         }
  95.  
  96.     }
  97.  
  98.     return TRUE;
  99. }
  100.  
  101. BOOL ResNames(HMODULE hModule, LPCTSTR lpType, LPTSTR lpName, LONG lParam)
  102. {
  103.     if (!IS_INTRESOURCE(lpName))
  104.     {
  105. //        cout << "is int resource. NAMES" << endl;
  106. //        cout << "TYPE in Names: " << lpType << endl;
  107. //        cout << "NAME in Names: " << lpName << endl;
  108.         EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  109.  
  110.     } else {
  111. //        cout << "is int resource. NAMES" << endl;
  112. //        cout << "TYPE in Names: " << (long)lpType << endl;
  113. //        cout << "NAME in Names: " << (long)lpName << endl;
  114.         EnumResourceLanguages( hModule, lpType, lpName, (ENUMRESLANGPROC)ResLang, 0);
  115.     }
  116.  
  117.    return TRUE;
  118. }
  119.  
  120. BOOL ResTypes(HMODULE hModule, LPTSTR lpType, LONG lParam)
  121. {
  122.     if (!IS_INTRESOURCE(lpType))
  123.     {
  124. //      cout << "INT is not resource TYPE" << endl;
  125. //      cout << "TYPE:" << lpType << endl;
  126.         if ( (lpType == RT_ICON ) || (lpType == (RT_ICON + 11)) )
  127.         {
  128.             EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  129.         }
  130.  
  131.  
  132.     } else {
  133. //        cout << "INT is resource TYPE" << endl;
  134. //        cout << "TYPE:" << (long)lpType << endl;
  135.         if ( (lpType == RT_ICON ) || (lpType == (RT_ICON + 11)) )
  136.         {
  137.             EnumResourceNames(hModule, lpType, (ENUMRESNAMEPROC)ResNames, 0);
  138.         }
  139.     }
  140.  
  141.    return TRUE;
  142. }
  143.  
  144.  
  145. IconChanger::IconChanger() // Constructor
  146. {
  147.     hExeSource = NULL;
  148.     hrResSource = NULL;
  149.     hGlobSource = NULL;
  150.     lpIconBuffer = NULL;
  151.     nIconCount = 0;
  152. }
  153.                                                             // Retrive Icon Buffer
  154. void IconChanger::GetIconFromSource(QString sIconPath, QString sExePath) // from source Exe/DLL
  155. {
  156.     ICONPATH = sIconPath; EXEPATH = sExePath;
  157.  
  158.     LPICONDIRENTRY pIconDirEntry(NULL);
  159.     LPGRPICONDIR pGrpIconDir(NULL);
  160.     HEADER header;
  161.     LPBYTE pIconBytes(NULL);
  162.     HANDLE hIconFile(NULL);
  163.     DWORD dwRet(0), nSize(0), nGSize(0), dwReserved(0);
  164.     HANDLE hUpdate(NULL);
  165.     BOOL ret(FALSE);
  166.     WORD i(0);
  167.     //sIconPath.replace("\\", "\\\\", Qt::CaseSensitive);
  168.     //sExePath.replace("\\", "\\\\", Qt::CaseSensitive);
  169.  
  170.     hIconFile = CreateFile( sIconPath.toStdString().c_str(), GENERIC_READ,
  171.                             0, 0, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
  172.     if ( hIconFile == INVALID_HANDLE_VALUE )
  173.     {
  174.         QMessageBox mess; mess.setText("Unable to open file.\n" + sIconPath );
  175.         mess.exec();
  176.     }
  177.  
  178.     ret = ReadFile( hIconFile, &header, sizeof(HEADER), &dwReserved, NULL );
  179.     if (!ret)
  180.     {
  181.         CloseHandle(hExeSource);
  182.         QMessageBox mess; mess.setText("Unable to Read Icon File " + sIconPath );
  183.         mess.exec();
  184.         return;
  185.     }
  186.  
  187.     pIconDirEntry = (LPICONDIRENTRY)new BYTE[ header.idCount * sizeof(ICONDIRENTRY) ];
  188.     if ( pIconDirEntry == NULL )
  189.     {
  190.         CloseHandle(hIconFile);
  191.         QMessageBox mess; mess.setText("Unable to allocate memory for Icon Group Header" + sIconPath );
  192.         mess.exec();
  193.         return;
  194.     }
  195.  
  196.     ret = ReadFile(hIconFile, pIconDirEntry, header.idCount*sizeof(ICONDIRENTRY), &dwReserved, NULL);
  197.     if (!ret)
  198.     {
  199.         delete[] pIconDirEntry;
  200.         CloseHandle(hIconFile);
  201.         QMessageBox mess;
  202.         mess.setText("Unable to read Icon Dir Entrys" + sIconPath );
  203.         mess.exec();
  204.         return;
  205.     }
  206.  
  207.     nGSize = sizeof(GRPICONDIR)+header.idCount*sizeof(ICONDIRENTRY);
  208.     pGrpIconDir = (LPGRPICONDIR)new BYTE[nGSize];
  209.     ZeroMemory( pGrpIconDir, nSize );
  210.  
  211.     pGrpIconDir->idReserved = header.idReserved;
  212.     pGrpIconDir->idType = header.idType;
  213.     pGrpIconDir->idCount = header.idCount;
  214.  
  215.    // nIconCount = 0;
  216.  
  217.     for ( i = 0; i < header.idCount; i++ )
  218.     {
  219.         pGrpIconDir->idEntries[i].bWidth = pIconDirEntry[i].bWidth;
  220.         pGrpIconDir->idEntries[i].bHeight = pIconDirEntry[i].bHeight;
  221.         pGrpIconDir->idEntries[i].bColorCount = pIconDirEntry[i].bColorCount;
  222.         pGrpIconDir->idEntries[i].bReserved = pIconDirEntry[i].bReserved;
  223.         pGrpIconDir->idEntries[i].wPlanes = pIconDirEntry[i].wPlanes;
  224.         pGrpIconDir->idEntries[i].wBitCount = pIconDirEntry[i].wBitCount;
  225.         pGrpIconDir->idEntries[i].dwBytesInRes = pIconDirEntry[i].dwBytesInRes;
  226.         pGrpIconDir->idEntries[i].nID = i+1;
  227.     }
  228.  
  229.     EXEPATHPADDED = EXEPATH;
  230.     EXEPATHPADDED.replace("\\", "\\\\", Qt::CaseInsensitive);
  231.     hExeSource = LoadLibrary( sExePath.toStdString().c_str() );
  232.     if ( hExeSource == NULL )
  233.     {
  234.         QMessageBox mess;
  235.         mess.setText("Unable to get handle to EXE/DLL\n" + sExePath );
  236.         mess.exec();
  237.     }
  238.  
  239.     BOOL more = true;
  240.     while(more){
  241.         more = EnumResourceTypes( hExeSource, (ENUMRESTYPEPROC)ResTypes, 0);
  242.     }
  243.  
  244.     CloseHandle(hIconFile);
  245.     FreeLibrary( hExeSource );
  246.     QMessageBox mess;
  247.     mess.setText("Cleanup DONE!... " );
  248.     mess.exec();
  249. }
  250.  
  251. IconChanger::~IconChanger() // De-Constructor
  252. {
  253.  
  254. }
Advertisement
Add Comment
Please, Sign In to add comment