Code:
#include <windows.h>
#include <stdio.h>
//#include <windows.h>
//#include <stdio.h>
#include <conio.h>
#include<winnt.h>
#define BUF_SIZE 256
TCHAR szName[]=TEXT("process.c");
TCHAR szMsg[]=TEXT("Message from first process");
/*
* Map a file for read access. Returns size of view in lpcbSize
*/
LPCTSTR MapFileRead(LPCTSTR szFileName, size_t * lpcbSize)
{
HANDLE hFile, hMapping;
DWORD dwFileSize;
LPCTSTR lpView;
MEMORY_BASIC_INFORMATION mbi;
HANDLE hMapFile;
LPCTSTR pBuf;
PIMAGE_NT_HEADERS pimage_nt_headers;
*lpcbSize = 0;
hFile = CreateFile(szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (INVALID_HANDLE_VALUE == hFile)
{
return NULL;
}
dwFileSize = GetFileSize(hFile, NULL);
if (INVALID_FILE_SIZE == dwFileSize)
{
CloseHandle(hFile);
return NULL;
}
hMapping = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);
if (NULL == hMapping)
{
CloseHandle(hFile);
return NULL;
}
lpView = (LPTSTR)MapViewOfFile(hMapping, FILE_MAP_READ, 0, 0, 0);
printf("%d",lpView);
PIMAGE_DOS_HEADER pimage_dos_header = PIMAGE_DOS_HEADER(lpView);
printf("%d...",pimage_dos_header->e_lfanew+lpView);
pimage_nt_headers = (PIMAGE_NT_HEADERS)
(lpView + pimage_dos_header->e_lfanew);
DWORD it_voffset = pimage_nt_headers->OptionalHeader.
DataDirectory[IMAGE_DIRECTORY_ENTRY_IMPORT].VirtualAddress; printf("%lu",it_voffset);
getch();
CloseHandle(hMapping);
CloseHandle(hFile);
if (NULL != lpView)
{
if (VirtualQuery(lpView, &mbi, sizeof(mbi)) >= sizeof(mbi))
{
*lpcbSize = min(dwFileSize, mbi.RegionSize);
}
else
{
*lpcbSize = dwFileSize;
}
}
return lpView;
}
/*
* Close a file mapping view.
*/
BOOL MapFileClose(LPCVOID lpView)
{
return UnmapViewOfFile(lpView);
}
int main(void)
{
size_t cbSize, i;
const char * file_view;
file_view = (const char *) MapFileRead(TEXT("const.cpp"), &cbSize);
if (file_view)
{
try
{
for (i = 0;i < cbSize; i++)
{
printf("%c", file_view[i]);
}
}
catch (...)
{
printf("Oh oh, not good doctor.");
}
MapFileClose(file_view);
}
getchar();
return 0;
}
Code is given above .....
the point where i am getting error is underlined in italic...
and error is Memory could not be read