Guest User

com_va.cpp

a guest
Jun 13th, 2011
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.65 KB | None | 0 0
  1. /* Made by Volodya and Flankerx, (c) 2004 */
  2. #include <windows.h>
  3. #include <ole2.h>
  4. #include <iostream>
  5. using namespace::std;
  6.  
  7. char* lpFuncType[] = {
  8.     "FUNC_VIRTUAL",
  9.     "FUNC_PUREVIRTUAL",
  10.     "FUNC_NONVIRTUAL",
  11.     "FUNC_STATIC",
  12.     "FUNC_DISPATCH"
  13. };
  14.  
  15. char* lpTypeKind[] = {
  16.     "enum",
  17.     "struct",
  18.     "module",
  19.     "interface",
  20.     "dispinterface",
  21.     "coclass",
  22.     "typedef",
  23.     "union"
  24. };
  25.  
  26. int wmain(int argc, wchar_t *argv[ ], wchar_t *envp[ ])
  27. {
  28.     ITypeLib *pITypeLib = 0;
  29.     ITypeLib *pTL = 0;
  30.     ITypeInfo *ptInfo = 0;
  31.     ITypeInfo *pInfo = 0;
  32.     TYPEATTR *lpTypeAttr = 0;
  33.     TYPEATTR *lpIntAttr = 0;
  34.     VARDESC  *lpVarDesc = 0;
  35.     BSTR bstrGUID = 0;
  36.     BSTR bstrName = 0;
  37.     GUID clsid;
  38.     GUID iid;
  39.  
  40.     CoInitialize( 0 );
  41.  
  42.     if( argc != 2)
  43.     {
  44.         wcerr << "Usage: com_va filename\n"
  45.                  "(c) 2004 Volodya and Flankerx\n";
  46.         return -1;
  47.     }
  48.  
  49.  
  50.     if ((LoadTypeLib(argv[1], &pITypeLib )) != S_OK)
  51.     {
  52.         wcerr << "LoadTypeLib failed for file: " << argv[1] << endl;
  53.         return -1;
  54.     }
  55.  
  56.     unsigned tiCount = pITypeLib->GetTypeInfoCount();
  57.  
  58.     /* procedure outline:
  59.         foreach typeinfo
  60.             if typekind == TKIND_COCLASS
  61.                 foreach member interface
  62.                     print_interface_info(clsid, iid)
  63.                 end foreach
  64.             end if
  65.         end foreach
  66.     */
  67.  
  68.     for ( unsigned i = 0; i < tiCount; i++ )
  69.     {
  70.         if (!LOWORD(pITypeLib->GetTypeInfo(i, &ptInfo)))
  71.         {
  72.             if(!LOWORD(ptInfo->GetTypeAttr(&lpTypeAttr)))
  73.             {
  74.                 if(lpTypeAttr->typekind != TKIND_COCLASS)
  75.                     continue;
  76.  
  77.                 clsid = lpTypeAttr->guid;
  78.                 StringFromCLSID(clsid, &bstrGUID);
  79.                 pITypeLib->GetDocumentation(i, &bstrName, 0, 0, 0);
  80.  
  81.                 wcout << "coclass " << bstrName << " GUID: " << bstrGUID << endl;
  82.                
  83.                 SysFreeString(bstrName);
  84.                 SysFreeString(bstrGUID);
  85.  
  86.                 unsigned mCount = lpTypeAttr->cImplTypes;
  87.                 //foreach member interface
  88.                 for(unsigned j = 0; j < mCount; j++)
  89.                 {
  90.                     HREFTYPE ref = 0;
  91.                     ptInfo->GetRefTypeOfImplType(j, &ref);
  92.                     if(!LOWORD(ptInfo->GetRefTypeInfo(ref, &pInfo)))
  93.                     {
  94.                         // get index in typelib
  95.                         UINT index = 0;
  96.                         pInfo->GetContainingTypeLib(&pTL, &index);
  97.                         pTL->GetDocumentation(index, &bstrName, 0, 0, 0);
  98.                         pInfo->GetTypeAttr(&lpIntAttr);
  99.                         iid = lpIntAttr->guid;
  100.                         StringFromCLSID(iid, &bstrGUID);
  101.  
  102.                         wcout << "\t" << lpTypeKind[lpIntAttr->typekind] << " " << bstrName << "GUID: " << bstrGUID << endl;
  103.  
  104.                         SysFreeString(bstrName);
  105.                         SysFreeString(bstrGUID);
  106.                        
  107.                         unsigned fCount = lpIntAttr->cFuncs;
  108.                         for(unsigned k = 0; k < fCount; k++)
  109.                         {
  110.                             FUNCDESC* fdesc = 0;
  111.                             pInfo->GetFuncDesc(k, &fdesc);
  112.  
  113.                             pInfo->GetDocumentation(fdesc->memid, &bstrName, 0, 0, 0);
  114.  
  115.                             LPVOID iUnk = 0;
  116.                             if (!LOWORD(CoCreateInstance(clsid, 0, CLSCTX_ALL, iid, &iUnk)))
  117.                             {
  118.                                 BYTE* pVTable = (BYTE*)*(DWORD*)(iUnk);
  119.                                 DWORD pFunction = *(DWORD*)(pVTable + fdesc->oVft);
  120.                                
  121.                                 wcout.unsetf( ios_base::dec );
  122.                                 wcout.setf( ios_base::hex );
  123.                                 wcout.width(8);
  124.                                 wcout << "\t\t" << lpFuncType[fdesc->funckind] << " " << bstrName << " VA: 0x" << pFunction << " Offset: 0x" << fdesc->oVft << endl;
  125.                             }
  126.  
  127.                             SysFreeString(bstrName);
  128.                             pInfo->ReleaseFuncDesc(fdesc);
  129.                         }
  130.                         pInfo->ReleaseTypeAttr(lpIntAttr);
  131.                         pInfo->Release();
  132.                     }//if(!LOWORD(ptInfo->GetRefTypeInfo(ref, &pInfo)))
  133.                 }//for(UINT j = 0; j < mCount; j++)
  134.                 ptInfo->ReleaseTypeAttr(lpTypeAttr);
  135.             }//if(!LOWORD(ptInfo->GetTypeAttr(&lpTypeAttr)))
  136.             ptInfo->Release();
  137.         }//if (!LOWORD(pITypeLib->GetTypeInfo(i, &ptInfo)))
  138.     }//for ( UINT i = 0; i < tiCount; i++ )
  139.  
  140.     pITypeLib->Release();
  141.     CoUninitialize();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment