最近需要把一个文件以二进制方式放到程序里,发现winhex复制太多会无效,所以就写了这个程序
最近写小软件很喜欢用纯windowsapi,写的东西很小.
大家如果需要可以把软件放到sendto文件夹这样直接发送到就可以生成了
使用方法:把需要转换的文件拖放到程序上就Ok了.
入口点设置为My,我不要crt运行库
只是感兴趣,失误之处请各位大虾指正!
/********************************************************************
created: 2014:5:5 10:41:32
filename: a
file ext: c
author: sleshep
purpose: make a file to c char[]
*********************************************************************/
#pragma comment(linker,"/MERGE:.data=.text")
#pragma comment(linker,"/MERGE:.rdata=.text")
#pragma comment(linker, "/FILEALIGN:16")
#pragma comment(linker, "/ALIGN:16")
#include <Windows.h>
void My()
{
char* a=GetCommandLine();
char* p=strstr(a," ")+1;
char pnew[MAX_PATH];
char fHead[]="unsigned char mdata[]={";
DWORD dwfilesize=0;
DWORD dwded=0;
HANDLE r=NULL;
HANDLE w=NULL;
char* pR=NULL;
char* pW=NULL;
char* pWpos=NULL;
char* pRpos=NULL;
if (!p)return;
r=CreateFile(p,GENERIC_READ,0,0,OPEN_EXISTING,0,0);
if (INVALID_HANDLE_VALUE==r)
{
MessageBox(0,"不存在文件","打开失败",MB_ICONSTOP);
return ;
}
dwfilesize=GetFileSize(r,0);
wsprintf(pnew,"%s_new.h",p);
w=CreateFile(pnew,GENERIC_READ|GENERIC_WRITE,0,0,CREATE_ALWAYS,0,0);
if (w==INVALID_HANDLE_VALUE)
{
MessageBox(0,"创建文件失败!","创建失败",MB_ICONSTOP);
return ;
}
pR=(char*)VirtualAlloc(0,dwfilesize,MEM_COMMIT,PAGE_EXECUTE_READWRITE);
pRpos=pR;
pW=(char*)VirtualAlloc(0,25+(dwfilesize*4+dwfilesize-1),MEM_COMMIT,PAGE_EXECUTE_READWRITE);
pWpos=pW;
ReadFile(r,pR,dwfilesize,&dwded,0);
CloseHandle(r);
memcpy(pW,fHead,strlen(fHead));
pWpos+=strlen(fHead);
for (;pRpos<(pR+dwfilesize);)
{
wsprintf(fHead,"0x%02x,",*(byte*)pRpos);
memcpy(pWpos,fHead,5);
pWpos+=5;
pRpos+=1;
}
*(pWpos-1)='}';
*(pWpos)=';';
WriteFile(w,pW,25+(dwfilesize*4+dwfilesize-1),&dwded,0);
CloseHandle(w);
VirtualFree(pR,dwfilesize,MEM_DECOMMIT);
VirtualFree(pW,25+(dwfilesize*4+dwfilesize-1),MEM_DECOMMIT);
MessageBox(0,"成功!","成功创建.h文件",0x40);
}
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!