技术开发 频道

编程实现Windows应用程序自产生代码

  注意:如果你想定义一个像unsigned char myfunction_00001_code[]那样的unsigned char,它的maxlength是2048。这受到了VC++ 6.0开发环境的限制。因此,如果该函数代码太长,我必须编写加密的代码数据到多unsigned char变量中。在build_h中的该函数会像如下所示的那样:

//
void build_h(
BYTE* pInBuf,int InBufSize,char* function_name)
{
    DWORD syslen
=InBufSize;
    
BYTE* sysbuffer=pInBuf;

    
char hname[MAX_PATH];
    sprintf(hname,
"%s%s",function_name,".h");

    HANDLE hHandle
=CreateFileA(hname,
        GENERIC_WRITE,
0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);

    SetFilePointer(hHandle,
0,0,FILE_BEGIN);

    DWORD RW;
    
char _code_end[]="\";
    
int l_end=strlen(&_code_end[0]);

    
char cname[MAX_PATH];
    
char* pszSlash=hname;

    
char TX[5];
    
int count=0;
    
int arrary_num=1;
    
int ar_num=0;

    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);

    WriteFile(hHandle,
"//",2,&RW,NULL);
    WriteFile(hHandle,hname,strlen(hname),
&RW,NULL);
    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);
    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);

    WriteFile(hHandle,
        
"//Created by Your Name",strlen("//Created by Your Name"),&RW,NULL);
    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);

    WriteFile(hHandle,
"//your email", strlen("//your email"), &RW, NULL);

    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);

    
for(UINT i=0;i<syslen;i++){
        
if(ar_num==0){
            WriteFile(hHandle,
"\r",1,&RW,NULL);
            WriteFile(hHandle,
"\n",1,&RW,NULL);

            
int ee=sprintf(cname,"%s","unsigned char ");
            ee
+=sprintf(cname+ee,"%s",pszSlash);
            ee
=ee-2;
            ee
+=sprintf(cname+ee,"%s%05d","_",arrary_num);
            ee
=sprintf(cname+ee,"%s",_code[]=\"\\\r\n");
            
int l=strlen(&cname[0]);
            WriteFile(hHandle,cname,l,
&RW,NULL);
        }

        WriteFile(hHandle,
"\\x",2,&RW,NULL);
        sprintf(TX,
"%02x",sysbuffer[i]);
        WriteFile(hHandle,TX,
2,&RW,NULL);
        count
++;
        ar_num
=ar_num+4;

        
if (count==20){
        count
=0;
        WriteFile(hHandle,
"\\",1,&RW,NULL);
        WriteFile(hHandle,
"\r",1,&RW,NULL);
        WriteFile(hHandle,
"\n",1,&RW,NULL);
        }

        
if(ar_num>2030){

        arrary_num
=arrary_num+1;
        WriteFile(hHandle,_code_end,l_end,
&RW,NULL);

        WriteFile(hHandle,
"\r",1,&RW,NULL);
        WriteFile(hHandle,
"\n",1,&RW,NULL);

        
char len[MAX_PATH];
        
int ee=sprintf(len,"%s","#define ");
        ee
+=sprintf(len+ee,"%s",cname+14);
        ee
=ee-7;
        ee
+=sprintf(len+ee,"%s","_LEN ");
        ee
+=sprintf(len+ee,"%d",ar_num/4);
        WriteFile(hHandle,
len,ee,&RW,NULL);

        WriteFile(hHandle,
"\r",1,&RW,NULL);
        WriteFile(hHandle,
"\n",1,&RW,NULL);

        ar_num
=0;
        count
=0;
        }
    }

    
double yushu=fmod(syslen*1.0,508.0);
    
if(yushu!=0){
        WriteFile(hHandle,_code_end,l_end,
&RW,NULL);

        WriteFile(hHandle,
"\r",1,&RW,NULL);
        WriteFile(hHandle,
"\n",1,&RW,NULL);

        
char len[MAX_PATH];
        
int ee=sprintf(len,"%s","#define ");
        ee
+=sprintf(len+ee,"%s",cname+14);
        ee
=ee-7;
        ee
+=sprintf(len+ee,"%s","_LEN ");
        ee
+=sprintf(len+ee,"%d",ar_num/4);
        WriteFile(hHandle,
len,ee,&RW,NULL);

        WriteFile(hHandle,
"\r",1,&RW,NULL);
        WriteFile(hHandle,
"\n",1,&RW,NULL);
    }

    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);

    
char lLen[MAX_PATH];
    
int ee=sprintf(lLen,"%s","#define ");
    ee
+=sprintf(lLen+ee,"%s",cname+14);
    ee
=ee-17;
    ee
+=sprintf(lLen+ee,"%s","ARRAY_NUM ");
    ee
+=sprintf(lLen+ee,"%d",arrary_num);


    WriteFile(hHandle,lLen,ee,
&RW,NULL);

    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);


    ee
=sprintf(lLen,"%s","#define ");
    ee
+=sprintf(lLen+ee,"%s",cname+14);
    ee
=ee-17;
    ee
+=sprintf(lLen+ee,"%s","CODE_LEN ");
    ee
+=sprintf(lLen+ee,"%d",syslen);


    WriteFile(hHandle,lLen,ee,
&RW,NULL);

    WriteFile(hHandle,
"\r",1,&RW,NULL);
    WriteFile(hHandle,
"\n",1,&RW,NULL);
    CloseHandle(hHandle);
}
//
0
相关文章