例子程序:
// 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;
}