Some time ago i wanted to know .Net programs in folder
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <stdio.h>
int ScanDirs(char *szDir);
int ProcessFile(TCHAR *szFile);
int CheckFile(PBYTE pMem, TCHAR *szFile);
TCHAR szPath[MAX_PATH];
int nCount = 0;
int __cdecl main(int argc, char **argv)
{
if (argc < 2) return 1;
strcpy(szPath, argv[1]);
ScanDirs(szPath);
printf("count: %d\n", nCount);
return 0;
}
int ScanDirs(char *szDir)
{
WIN32_FIND_DATA fdFile;
HANDLE hFile;
int i;
// char szTmp[MAX_PATH];
i = 0;
int nLen = lstrlen(szDir);
lstrcpy(szDir+nLen, "\\*.*");
hFile = FindFirstFile(szDir, &fdFile);
if (hFile != INVALID_HANDLE_VALUE)
do {
if ((fdFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
!= FILE_ATTRIBUTE_DIRECTORY) {
//puts(fdFile.cFileName);
*(szDir+nLen) = '\\';
lstrcpy(szDir+nLen+1, fdFile.cFileName);
//puts(szDir);
ProcessFile(szDir);
i++;
} else {
BOOL bPDir;
if (*fdFile.cFileName == '.') {
if (!*(fdFile.cFileName+1)) bPDir = TRUE;
else if (*(fdFile.cFileName+1) == '.' && !*(fdFile.cFileName+2)) bPDir = TRUE;
else bPDir = FALSE;
} else bPDir = FALSE;
if (!bPDir) {
*(szDir+nLen) = '\\';
lstrcpy(szDir+nLen+1, fdFile.cFileName);
//puts(szDir);
ScanDirs(szDir); // recursion
*(szDir + nLen) = 0; // previous level
}
}
} while (FindNextFile(hFile, &fdFile));
FindClose(hFile);
return 0;
}
int ProcessFile(TCHAR *szFile)
{
HANDLE hFile, hMapping;
VOID *pMem;
hFile = CreateFile(szFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (hMapping)
{
pMem = MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
if (pMem)
{
CheckFile(pMem, szFile);
UnmapViewOfFile(pMem);
} else
puts("File open error");
CloseHandle(hMapping);
} else
puts("FileMapping error");
} else
puts("File open error");
return 0;
}
int CheckFile(PBYTE pMem, TCHAR *szFile)
{
PIMAGE_DOS_HEADER pDosHdr;
PIMAGE_NT_HEADERS pNtHdrs;
PIMAGE_NT_HEADERS64 pNtHdrs64;
DWORD dwRVAdr;
pDosHdr = (PIMAGE_DOS_HEADER)pMem;
if (pDosHdr->e_magic != IMAGE_DOS_SIGNATURE)
return 1;
pNtHdrs = (PIMAGE_NT_HEADERS)(pMem+pDosHdr->e_lfanew);
if (pNtHdrs->Signature != IMAGE_NT_SIGNATURE)
return 2;
pNtHdrs64 = (PIMAGE_NT_HEADERS64)pNtHdrs;
BOOL bIs64Bit = ( pNtHdrs->OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC );
if (bIs64Bit) dwRVAdr = (DWORD)((PIMAGE_NT_HEADERS64)(pNtHdrs))->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
else dwRVAdr = (DWORD)((PIMAGE_NT_HEADERS32)(pNtHdrs))->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR].VirtualAddress;
if (!dwRVAdr) return 2;
puts(szFile);
nCount++;
return 0;
}
BTW:
The first AMD64-based processor, the Opteron, was released in April 2003.