You should create your own segment to store data (like hashes, encrypted values etc). I protect my modules in nearly the same way.
Example:
#pragma once
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#include <winnt.h>
#ifndef IMAGE_SIZEOF_NT_OPTIONAL_HEADER
#define IMAGE_SIZEOF_NT_OPTIONAL_HEADER sizeof(IMAGE_OPTIONAL_HEADER)
#endif // IMAGE_SIZEOF_NT_OPTIONAL_HEADER
#define MYSEGMENT "mydata" // max 8 chars
#pragma data_seg( MYSEGMENT )
struct _MYHASHVALUE
{
unsigned int hash;
} MYHASHVALUE = { 42, };
#pragma data_seg()
unsigned int FindSegment(HANDLE hfile,const char* sSegment,unsigned int& cbsize)
{
IMAGE_DOS_HEADER img_dos;
IMAGE_FILE_HEADER img_file;
IMAGE_OPTIONAL_HEADER img_optional;
IMAGE_SECTION_HEADER img_section;
long more_dos[16];
long nt_sign;
unsigned long cb;
SetFilePointer(hfile,0,0,FILE_BEGIN);
cb = ReadFile(hfile,&img_dos,sizeof(img_dos),&cb,0)?cb:0;
if(!cb) return 0;
if(IMAGE_DOS_SIGNATURE != img_dos.e_magic) return 0;
ReadFile(hfile,more_dos,sizeof(more_dos),&cb,0);
SetFilePointer(hfile,img_dos.e_lfanew,0,FILE_BEGIN);
ReadFile(hfile,&nt_sign,sizeof(nt_sign),&cb,0); ReadFile(hfile,&img_file,IMAGE_SIZEOF_FILE_HEADER,&cb,0);
unsigned int seek_optional = SetFilePointer(hfile,0,0,FILE_CURRENT);
ReadFile(hfile,&img_optional,IMAGE_SIZEOF_NT_OPTIONAL_HEADER,&cb,0);
for(unsigned int nofs = 0;nofs<img_file.NumberOfSections;nofs++)
{
ReadFile(hfile,&img_section,IMAGE_SIZEOF_SECTION_HEADER,&cb,0);
if(IMAGE_SIZEOF_SECTION_HEADER>cb) break;
if(!strcmp((char*)img_section.Name,sSegment))
{
cbsize = img_section.SizeOfRawData;
return img_section.PointerToRawData;
}
}
return 0;
}
int _tmain(int argc, _TCHAR* argv[])
{
if(0<argc)
{
HANDLE hfile = CreateFile(argv[0],GENERIC_READ,0,0,OPEN_EXISTING,0,0);
unsigned int cbsize = 0;
unsigned int offset = FindSegment(hfile,MYSEGMENT,cbsize);
if((0<offset) && (cbsize>=sizeof(MYHASHVALUE)))
{
struct _MYHASHVALUE myhash;
unsigned long rd;
SetFilePointer(hfile,offset,0,FILE_BEGIN);
if(ReadFile(hfile,&myhash,sizeof(myhash),&rd,0) && (rd==sizeof(myhash)))
{
_tprintf(__T("hash: %i\r\n"),(int)myhash.hash);
}
}
CloseHandle(hfile);
}
_tprintf(__T("<key> ")); _gettch();
return 0;
}
Good luck.