//--从当前进程模块程序读数据
//--参数
//--buf/接收数据的缓冲区
//--size/期望读入的数据字节数(buf必须足够)
//--pos/开始读数据的偏移位置
//--返回
//--实际读入数据字节数(一般应该等于size)
int accessME(char* buf, unsigned int size, unsigned int pos = 0)
{
int ret = 0;
if (NULL == buf || size <= 0 || pos < 0) return ret;
HMODULE hModule = GetModuleHandle(NULL);
char lpFilename[MAX_PATH] = {0};
::GetModuleFileName(hModule, lpFilename, sizeof(lpFilename));
HANDLE hFile = ::CreateFile(lpFilename
, GENERIC_READ
, FILE_SHARE_READ
, NULL
, OPEN_EXISTING
, FILE_FLAG_RANDOM_ACCESS
, NULL);
if (NULL != hFile && INVALID_HANDLE_VALUE != hFile)
{
DWORD dwSize = ::GetFileSize(hFile, NULL);
if (pos > 0 && pos < dwSize)
::SetFilePointer(hFile, pos, NULL, FILE_BEGIN);
//--read
DWORD dwRead = 0;
::ReadFile(hFile, buf, size, &dwRead, NULL);
::CloseHandle(hFile);
ret = dwRead;
}
return ret;
}
本文地址:http://com.8s8s.com/it/it912.htm