首页
社区
课程
招聘
[讨论]关于MapAndLoad的使用问题
发表于: 2007-6-28 16:18 7281

[讨论]关于MapAndLoad的使用问题

HSQ 活跃值
8
2007-6-28 16:18
7281
为什么将MapAndLoad()最后一个参数设为FALSE,就出错??? 查MSDN如下:
ERROR:20   ERROR_BAD_UNIT  The system cannot find the device specified.
为什么在第一种情况下不会出现'系统找不到指定设备',而后者就出现呢,可是我对于要处理的文件可是固定的,另外测试环境为: WIN2K PRO SP4  */

#include <stdio.h>
#include <windows.h>
#include <imagehlp.h>

#pragma comment(lib, "imagehlp.lib")

LPDWORD GetExeEntryPoint(PSTR);

//简单的测试一下子程序
int main(void)
{
	DWORD	dwEntryPoint;
    dwEntryPoint = (DWORD)GetExeEntryPoint("D:\\FTP.EXE");	//固定文件,方便测试
	return 0;
}

 //////////////////////////////////////////////////////////////////////////
//************************************************************************
//模块名字:GetExeEntryPoint(LPCTSTR)
//模块功能:找到目标进程的入口,并将可执行节追加可写属性
//返回数值:成功返回找到的入口地址,失败返回FALSE
//注意事项:无
//************************************************************************
//参数说明:参数名         |   输入/输出  |        参数说明
//      lpApplicationName  |    IN        |     要处理的可执行文件名
//************************************************************************
///////////////////////////////////////////////////////////////////////////

LPDWORD  GetExeEntryPoint(PSTR lpApplicationName)
{
	PIMAGE_NT_HEADERS		pNTHeader;
	PIMAGE_SECTION_HEADER	pSectionHeader;
	DWORD					pEntryPoint,dwSectionNum;
	LOADED_IMAGE			Image;
	PLOADED_IMAGE			pImage=&Image;

	if(!MapAndLoad(lpApplicationName, NULL,pImage,FALSE,TRUE))
	//if(!MapAndLoad(lpApplicationName, NULL,pImage,FALSE,FALSE))
	/*为什么将最后一个参数设为FALSE,就出错??? 查MSDN如下:
	 ERROR:20   ERROR_BAD_UNIT  The system cannot find the device specified. 
	为什么在第一种情况下不会出现'系统找不到指定设备',而后者就出现呢,可是我
	对于要处理的文件可是固定的,另外测试环境为: WIN2K PRO SP4  */
	{  
		printf("MapAndLoad ERROR:%X\n",GetLastError());
		return 0;
	}

	pNTHeader = pImage->FileHeader;
	dwSectionNum = pImage->NumberOfSections;
	pSectionHeader = pImage->LastRvaSection;
    printf("NumberOfSections=%X\n",dwSectionNum);
	for(DWORD j,i=0;i<dwSectionNum;i++)
	{
		j=pSectionHeader->Characteristics;
		__asm
			{
				mov		eax,j
				rol		eax,4
				cmp		al,6
				jne		_notexec
				add		al,8
			_notexec:
				ror		eax,4
				mov		j,eax		
			}
		pSectionHeader->Characteristics=j;
	    printf("IMAGE_SECTION_HEADER[%d].Characteristics=%X %X\n",
			i+1,(pSectionHeader++)->Characteristics,pSectionHeader);
	}
	pEntryPoint = pNTHeader->OptionalHeader.AddressOfEntryPoint + pNTHeader->OptionalHeader.ImageBase;

	UnMapAndLoad(pImage);

	return (LPDWORD)pEntryPoint;
}

///////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////
/*
附:MapAndLoad 使用说明

The MapAndLoad function maps an image and preloads data from the mapped file.
BOOL MapAndLoad(
    IN LPSTR ImageName,	
    IN LPSTR DllPath,	
    OUT PLOADED_IMAGE LoadedImage,	
    IN BOOL DotDll,	
    IN BOOL ReadOnly	
   );	
 
Parameters

ImageName
The name of the image that is loaded.
DllPath
The path used to locate the image if the name provided cannot be found. If NULL is used, then the search path rules set forth in the SearchPath function will apply.
LoadedImage
A pointer to a LOADED_IMAGE structure. This structure receives information about the image after it is loaded.
DotDll
If the image needs to be located and the image name does not contain a file extension, this parameter controls the default extension used for the search process. If the value is TRUE, a .dll extension is used. If the value is FALSE, then a .exe extension is used.
ReadOnly
If this value is TRUE, the file is mapped for read-access only. If the value is FALSE, the file is mapped for read and write access.

Return Values
If the function succeeds, the return value is TRUE.
If the function fails, then the return value is FALSE. To retrieve extended error information, call GetLastError.

Remarks
The MapAndLoad function maps an image and preloads data from the mapped file. The corresponding function, UnMapAndLoad
, must be used to deallocate all resources that are allocated by the MapAndLoad function.
*/


这是刚在XP SP2 中测试的结果:


今天干脆装了个XP,不是代码的问题,是系统的问题,在XP中一切正常;而在2K下总是出现莫名其妙的错误,真是见鬼了???

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

上传的附件:
收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 381
活跃值: (140)
能力值: ( LV13,RANK:330 )
在线值:
发帖
回帖
粉丝
2
我刚才在网吧的XP SP2系统中测试了下,好象没有任何错误发生,难道是我那WIN2K PRO SP4  的系统太老了???这个函数在98是就有的,理论上在WIN2K PRO SP4 上应该不会出问题的,困惑中,还望大虾们解惑!!!
2007-6-28 16:35
0
游客
登录 | 注册 方可回帖
返回
//