当前位置:编程学习 > VC++ >>

利用批处理快速编译测试VC网络程序

来自邪恶八进制  文章作者:syspro

        在用VC编写一个网络程序时需要反复测试客户端和服务段。尤其是服务端的程序需要释放Dll。编译测试的时候需要进行以下操作:
        1、编译Dll模块。
         2、将Dll模块加壳压缩。
        3、将Dll模块用BINTOC转换成文本数组。
        4、将转换的文本数组粘贴到服务段主程序的代码中。
        5、编译主服务端主程序。
        6、配置服务端主程序。
        7、测试运行。
        如果手动进行以上操作,每次测试都浪费不少时间。测试频繁的时候还容易的强迫症。于是改写了bintoc,再写了个命令行下的服务段配置程序,最后写了个批处理来完成编译测试过程,内容如下。
@echo off
@echo compile server

rem  server_dll和server_install工程文件夹和编译输出文件夹分别为
rem  server_dll、server_install和compile。
rem  在编译之前要分别在server_dll和server_install的工程中点击“Project”-〉“Export MakeFIle”
rem  生成server_dll.mak和mouse_install.mak文件。

rem  1、编译Dll模块。
@echo compile server_dll project
cd server_dll
del /Q Release
nmake /f server_dll.mak

rem  2、将Dll模块加壳压缩,本例中用UPX ,只要支持命令行的都可以。
@echo compress server_dll use upx
cd..
cd compile
upx -9 server_dll.dll

rem 3、将Dll模块用BINTOC转换成数组头文件。
@echo convert server_dll to GenFile.h
bintoc.exe server_dll.dll  GenFile.h

rem 4、将数组头文件覆盖服务段主程序的头文件。
@echo copy GenFile.h to server_install project
cd..
copy /Y %cd%compileGenFile.h %cd%server_installGenFile.h

rem 5、编译主服务端主程序。
@echo compile server_install
cd server_install
del /Q Release
nmake /f server_install.mak

rem 6、配置服务端主程序。
@echo config server_install
cd..
cd compile
serverconfig.exe server_install.exe config.txt

rem 7、测试运行。
@echo run server_install
server_install.exe
cd ..

    
        下面是改写的bintoc源码
#include <stdio.h>
FILE *in;
FILE *out;
int main(int argc, char *argv[])
{
    unsigned char ch;
    int    cnt = 0;
    if (argc < 3)
    {
        printf("usage: bin2c infile.bin outfile.h ");
        return -1;
    }


    if ((out = fopen(argv[2], "wb")) == NULL)
    {
        printf("Couldnt open output file. ");
        return -1;
    }

    if ((in = fopen("start.h", "rb")) != NULL)
    {
        printf("open start.h file. ");
        ch = fgetc(in);
        while (!feof(in))
        {
            fprintf(out, "%c", ch);
            ch = fgetc(in);
        }
        fclose(in);
    }

 

    if ((in = fopen(argv[1], "rb")) == NULL)
    {
        printf("Couldnt open data file. ");
        return -1;
    }


    fseek(in,0,SEEK_END); 
    long   lFileSize   =   ftell(in);
    fprintf(out, "int  DllLen = %ld; ", lFileSize);
    fprintf(out, "unsigned char DllData[%ld] = {", lFileSize);
    fseek(in,0,SEEK_SET);
    ch = fgetc(in);
    while (!feof(in))
    {
        if (cnt != 0)
            fprintf(out, ", ");
        if (!(cnt % 10))
            fprintf(out, " ");
        fprintf(out, "0x%02x", (int)ch);
        cnt++;
        ch = fgetc(in);
    }
    fprintf(out, " }; ");
    fclose(in);

    if ((in = fopen("end.h", "rb")) != NULL)
    {
        printf("open end.h file. ");
        ch = fgetc(in);
        while (!feof(in))
        {
            fprintf(out, "%c", ch);
            ch = fgetc(in);
        }   
        fclose(in);
    }


    fclose(out);
    return 0;
}

        下面是改写的bintoc需要的文件start.h
#include "stdafx.h"

#include <stdio.h>


BOOL CreateDllFile( LPSTR szPathFile );

       下面是改写的bintoc需要的文件end.h
BOOL CreateDllFile( LPSTR szPathFile )
{
    FILE *fp;
    fp=fopen(szPathFile, "wb");
    if (fp!=NULL)
    {
        fwrite(DllData, 1, DllLen, fp);
        fclose(fp);
    } 
     return TRUE;
}

补充:软件开发 , Vc ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,