做一个最小EXE代码的图形界面的hello,world我是在vc6下用Win32Application向导生成的一个图形界面helloworld程序
然后去掉所有资源和不相关代码
然后在编译选项和链接选项里面做了调整,
主要就是去掉ingore default library以及加上 /OPT:REF /OPT:NOWIN98
最后生成的EXE为3KB,用ultraedit打开查看,后面还有很多为0的字节, 偶不懂PE格式,所以也不知道这些0能不能够也去掉怎么做还可以更小,请高手指导指导

解决方案 »

  1.   

    已经是纯sdk了,只引用了kerner32.dll user32.dll中的必要函数
      

  2.   

    可以到1k,在windows下最小了.修改EntryPoint,合并一些段.所能做的也只能是这些了.
      

  3.   

    Anikan(皮皮鱼)
    entrypoint已经修改成了WinMain的,什么叫合并一些段?能具体清楚点么
      

  4.   

    ah haha
    搜索google找到了,;)
    是要加了merge section的链接选项
    /MERGE
    现在只有1.42KB了,用ultraedit查看后面已经没有那些冗余的0字节了,估计没法再小了由于这是gui helloworld,里面还有不少代码,没有任何代码,最小可以到480字节(VC6)
      

  5.   

    #include "windows.h" #pragma comment(linker, "/ENTRY:EntryPoint") 
    //下面的优化是段合并,不推荐使用,在很多程序里也许不能使用 
    #pragma comment(linker, "/OPT:NOWIN98") 
    #pragma comment(linker, "/SECTION:MiniPE,")             //创建自定义的 MiniPE Section 
    #pragma comment(linker, "/MERGE:.data=MiniPE")  //合并.data Section 到 MiniPE Section 
    #pragma comment(linker, "/MERGE:.text=MiniPE")  //合并.text Section 到 MiniPE Section 
    #pragma comment(linker, "/MERGE:.rdata=MiniPE") //合并.rdata Section 到 MiniPE Section 
    //////////////////////////////////////////////////////////////////////////////////////////////////// 
    HWND                                    g_hWnd;         
    HINSTANCE                               g_hInst;        const char                              c_szAppName[] = "MiniPE"; 
    LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam); 
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow); void EntryPoint() 

       ExitProcess(WinMain(GetModuleHandle(NULL), NULL, GetCommandLine(), SW_SHOWNORMAL)); } int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow) 

           MSG                                             sMsg; 
           WNDCLASSEX                              sWndClassEx; 
           g_hInst = hInstance;        sWndClassEx.cbSize = sizeof(WNDCLASSEX); 
           sWndClassEx.style = CS_VREDRAW | CS_HREDRAW; 
           sWndClassEx.lpfnWndProc = (WNDPROC) WindowProc; 
           sWndClassEx.cbClsExtra = 0; 
           sWndClassEx.cbWndExtra = 0; 
           sWndClassEx.hInstance = g_hInst; 
           sWndClassEx.hIcon = LoadIcon(NULL, IDI_APPLICATION); 
           sWndClassEx.hCursor = LoadCursor(NULL, IDC_ARROW); 
           sWndClassEx.hbrBackground = (HBRUSH) (COLOR_WINDOW); 
           sWndClassEx.lpszMenuName = NULL; 
           sWndClassEx.lpszClassName = c_szAppName; 
           sWndClassEx.hIconSm = NULL; 
           RegisterClassEx(&sWndClassEx);        g_hWnd = CreateWindowEx(0, c_szAppName, c_szAppName, WS_OVERLAPPEDWINDOW, 
                   CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 
                   NULL, NULL, g_hInst, NULL); 
           ShowWindow(g_hWnd, iCmdShow); 
           UpdateWindow(g_hWnd);        while (GetMessage(&sMsg, NULL, 0, 0)) 
           { 
                   TranslateMessage(&sMsg); 
                   DispatchMessage(&sMsg); 
           } 
           return((int) sMsg.wParam); 
    } LRESULT CALLBACK WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) 

           switch (uMsg) 
           { 
           case WM_DESTROY: 
                   PostQuitMessage(0); 
                   break;        default: 
                   return(DefWindowProc(hWnd, uMsg, wParam, lParam)); 
           } 
           return(0); 

     
     
      

  6.   

    #pragma comment(linker, "/FILEALIGN:16") 
    #pragma comment(linker, "/ALIGN:16")
    把默认对齐改变了会更小吧,但是FileAlign小了可能造成加载EXE失败另外把entrypoint改为WinMain,去掉你写的EntryPoint,也可以运行,还少了点代码,:)我把函数也减少到2个,去掉画HelloWorld的代码,最后只有1.1kb了, 呵呵谢谢,谢谢~~~能否解释一下为什么不推荐使用那几个merge section,有什么坏处?
      

  7.   

    ft.. 发现还有点垃圾里面.. 1.04KB...