前不久大家都在讨论怎样通过一个程序来生成另一个程序,我提出了将另一个程序当资源文件包含在程序中一起编译,然后在程序运行时根据需要再重新生成出来,下面是我的一个例子, 在这里,我将windows自带的写字板(writer.exe)当资源.
下面是源代码
//资源文件writer.exe长度为204800,在资源文件中定义为"CUSTOM"资源,编号为1111
HRSRC hMyRes; //resource handle
HGLOBAL hgpt; //resource pointer
LPVOID lpBuff; //resource buffer pointer
DWord rcSize=204800; //resource size
HANDLE hFile;//file to write
LPDWORD dwByte;//byte size had been write
dwByte=&rcSize; //locate the resource and load the resource to memory and lock it
hMyRes=FindResource((HMODULE)GetWindowLong(Handle,GWL_HINSTANCE),MAKEINTRE SOURCE(1111),"CUSTOM");
if(hMyRes==NULL)
ShowMessage(SysErrorMessage(GetLastError()));
hgpt=LoadResource(NULL,hMyRes);
if(hgpt==NULL)
ShowMessage(SysErrorMessage(GetLastError()));
lpBuff=LockResource(hgpt);
//now i will read the resource and write it to an file
try
{
hFile=CreateFile("e:\\mywriter.exe",GENERIC_WR99vE,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
WriteFile(hFile,lpBuff,rcSize,dwByte,NULL);
if(*dwByte!=204800)
ShowMessage("Failed to write to file");
}
__finally
{
CloseHandle(hFile);
}
在资源的添加与生成时,我用Borland ResourceWorkShop4.5没办法做,最后是用vb6带的资源编辑器生成的res文件.