http://www.vckbase.com/vckbase/vckbase12/vc/nonctrls/misc_21/1221002.htm or you can think about PE head
插入资源,导入那个exe,释放资源的代码的写法和普通的资源一样。我成功的,绝不是骗你。
A simple and common method: 1.Instert a application to your resouce. Name it "IDR_EXE_APP" in "EXE". 2.Build your project. Now, this application is gone to your program. 3.Here, extract it from yours.Use the code below: // we will extract it to the system directory // Ok, let's go ..... HANDLE hFile = NULL; HRSRC hRsrc = NULL; DWORD dwFileSize, dwImageSize; dwFileSize = dwImageSize = -1; // get the size of the installed driver TCHAR sDriverPath[MAX_PATH]; if(GetWindowsDirectory(sDriverPath, MAX_PATH) == 0) { TRACE("There was an error getting the windows directory!\n"); return; } _tcscat(sDriverPath, _T("\\XXXX.exe")); // application name hFile = CreateFile(sDriverPath,GENERIC_READ|GENERIC_WRITE,0,NULL, OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile != INVALID_HANDLE_VALUE) { dwFileSize = GetFileSize(hFile, NULL); // in case of errors, dwSize is -1 CloseHandle(hFile); } hRsrc = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_EXE_APP), "EXE"); if(NULL == hRsrc) { TRACE("Couldn't locate driver binary in the resources!"); return; } dwImageSize = SizeofResource(AfxGetResourceHandle(), hRsrc); if(dwImageSize == 0) { TRACE("Size of image is 0!"); retrun; }
// check if we need to copy the app.exe file if (dwImageSize != dwFileSize ) { HGLOBAL hBinImage = LoadResource(AfxGetResourceHandle(), hRsrc); if(NULL == hBinImage) { TRACE("Couldn't load binary image from resources!"); return; } PVOID pBinImage = LockResource(hBinImage); if(NULL == pBinImage) { TRACE("Couldn't lock binary image from resources!"); return; } // create a new file hFile = CreateFile(sDriverPath,GENERIC_READ | GENERIC_WRITE,0,NULL, CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL); if (hFile == INVALID_HANDLE_VALUE) { TRACE("Error creating >%s<: might be in use or protected by some rights!\n", sDriverPath); return; } DWORD dwWritten = 0; // write the content to the file if (!WriteFile(hFile, pBinImage, dwImageSize, &dwWritten, NULL)) { TRACE("Failed to write %d bytes to >%s<\n", dwImageSize, sDriverPath); CloseHandle(hFile); return; } // truncate the file if needed if (!SetEndOfFile(hFile)) { TRACE("Couldn't truncate disk file to %d bytes! ... proceed at your own risk!", dwImageSize); } CloseHandle(hFile);
} 4.Now , use some function CreateProcess, ShellExecute or WinExec to run it.
作为资源也行,创建一个临时文件,然后用ShellExecute执行它。具体我有代码,要的话给我消息。
三个SDK函数 winexec, shellexecute,createprocess可以使用。
WinExec最简单,两个参数,前一个指定路径,后一个指定显示方式.后一个参数
值得说一下,比如泥用 SW_SHOWMAXMIZED方式去加栽一个无最大化按钮的 程序,
呵呵就是Neterm,calc等等,就不会出现正常的窗体,但是已经被加到任务列表里
了。
ShellExecute较 WinExex灵活一点,可以指定工作目录,下面的 Example就是直接
打开 c:\temp\1.txt,而不用加栽与 txt文件关联的应用程序,很多安装程序完成
后都会打开一个窗口,来显示Readme or Faq,就是这么作的啦.ShellExecute(NULL,NULL,_T("1.txt"),NULL,_T("c:\\temp"),SW_SHOWMAXMIZED);
CreateProcess最复杂,一共有十个参数,不过大部分都可以用NULL代替,它可以
指定进程的安全属性,继承信息,类的优先级等等.来看个很简单的 Example:STARTUPINFO stinfo; //启动窗口的信息
PROCESSINFO procinfo; //进程的信息CreateProcess(NULL,_T("notepad.exe"),NULL,NULL.FALSE, NORMAL_PRIORITY_
CLASS,NULL,NULL, &stinfo,&procinfo);
or you can think about PE head
1.Instert a application to your resouce.
Name it "IDR_EXE_APP" in "EXE".
2.Build your project. Now, this application is gone to your program.
3.Here, extract it from yours.Use the code below:
// we will extract it to the system directory
// Ok, let's go .....
HANDLE hFile = NULL;
HRSRC hRsrc = NULL;
DWORD dwFileSize, dwImageSize;
dwFileSize = dwImageSize = -1; // get the size of the installed driver TCHAR sDriverPath[MAX_PATH];
if(GetWindowsDirectory(sDriverPath, MAX_PATH) == 0)
{
TRACE("There was an error getting the windows directory!\n");
return;
} _tcscat(sDriverPath, _T("\\XXXX.exe")); // application name hFile = CreateFile(sDriverPath,GENERIC_READ|GENERIC_WRITE,0,NULL,
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile != INVALID_HANDLE_VALUE)
{
dwFileSize = GetFileSize(hFile, NULL); // in case of errors, dwSize is -1
CloseHandle(hFile);
} hRsrc = FindResource(AfxGetResourceHandle(), MAKEINTRESOURCE(IDR_EXE_APP), "EXE");
if(NULL == hRsrc)
{
TRACE("Couldn't locate driver binary in the resources!");
return;
}
dwImageSize = SizeofResource(AfxGetResourceHandle(), hRsrc);
if(dwImageSize == 0)
{
TRACE("Size of image is 0!");
retrun;
}
// check if we need to copy the app.exe file if (dwImageSize != dwFileSize )
{
HGLOBAL hBinImage = LoadResource(AfxGetResourceHandle(), hRsrc);
if(NULL == hBinImage)
{
TRACE("Couldn't load binary image from resources!");
return;
}
PVOID pBinImage = LockResource(hBinImage);
if(NULL == pBinImage)
{
TRACE("Couldn't lock binary image from resources!");
return;
} // create a new file
hFile = CreateFile(sDriverPath,GENERIC_READ | GENERIC_WRITE,0,NULL,
CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
TRACE("Error creating >%s<: might be in use or protected by some rights!\n", sDriverPath);
return;
}
DWORD dwWritten = 0; // write the content to the file
if (!WriteFile(hFile, pBinImage, dwImageSize, &dwWritten, NULL))
{
TRACE("Failed to write %d bytes to >%s<\n", dwImageSize, sDriverPath);
CloseHandle(hFile);
return;
}
// truncate the file if needed
if (!SetEndOfFile(hFile))
{
TRACE("Couldn't truncate disk file to %d bytes! ... proceed at your own risk!", dwImageSize);
}
CloseHandle(hFile);
}
4.Now , use some function CreateProcess, ShellExecute or WinExec to run it.
有没有写入的?能不能给个详细的例子?