在EXE文件中查找指定字符串

例子程序:

// Example.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "iostream"
#include "string"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
	string str="hello world";
	cout<<str<<endl;
	return 0;
}

测试程序:

// FindStringInExe.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include "stdio.h"
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
	HANDLE hFile=NULL;
	BOOL   IsRead=FALSE;
	DWORD  dwFileSize=0;
	char*  buffer=NULL;
	DWORD  dwReadSize=0;
	char*  findString="hello world";
	char*  findResult=NULL;
	//
	hFile=CreateFile("d:\\Example.exe",GENERIC_READ,0,NULL,OPEN_EXISTING,0,NULL);
	if(hFile == INVALID_HANDLE_VALUE)
	{
		printf("create file error %s",GetLastError());
		CloseHandle(hFile);
		return 2;
	}
	dwFileSize=GetFileSize(hFile,NULL);
	if (dwFileSize == 0 )
	{
		printf("the file size zero !");
		CloseHandle(hFile);
		return 2;
	}
	buffer=new char[dwFileSize+1];
	if (buffer == NULL)
	{
		printf("buffer new error !");
		CloseHandle(hFile);
		return 2;
	}
	IsRead=ReadFile(hFile,buffer,dwFileSize,&dwReadSize,NULL);
	if(IsRead == FALSE)
	{
		printf("read file error !");
		CloseHandle(hFile);
		return 2;
	}
	buffer[dwFileSize]=0;
	for(int i=0;i<dwFileSize;i++)
	{
		if(buffer[i] == '\0')
		{
			buffer[i]='\\0';
		}
	}
	findResult=strstr(buffer,findString);
	if (findResult != NULL)
	{
		printf("find string !");
	}
	else
	{
		printf("not find string!");
	}
	CloseHandle(hFile);
	delete[] buffer;
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值