构造微小的EXE程序
本文参考了网上的帖子《VC++下编译极小的程序》,《VC8中打造512字节超小应用程序》。废话不用说,直接上两个例子:
例子一: 一个Console程序
1 #include <windows.h>
2
3 #pragma comment(lib, "User32")
4 #pragma comment(lib, "Kernel32")
5 #pragma comment(linker, "/Entry:main")
6 #pragma comment(linker, "/Merge:.rdata=.text")
7
8 bool WINAPI StdOut( LPCTSTR lpString)
9 {
10 HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
11 DWORD numOfCharsWritten=0;
12 return WriteConsole(hStdOut, lpString, lstrlen(lpString), &numOfCharsWritten, NULL);
13 }
14
15 int main()
16 {
17 TCHAR *pChar = TEXT("Hello World!\n");
18 StdOut(pChar);
19 }
采用VC9命令行编译 cl minic.cpp /link /align:16,编译后大小为768 byte。
例子二: 一个窗口程序
1 #include <windows.h>
2
3 #pragma comment (lib,"user32.lib")
4 #pragma comment (lib,"kernel32.lib")
5 #pragma comment(linker, "/Entry:main")
6 #pragma comment(linker, "/Merge:.rdata=.text")
7 #pragma comment(linker, "/Subsystem:windows")
8
9 void *operator new[](unsigned int size){
10 return HeapAlloc (GetProcessHeap(), NULL, size);
11 }
12
13 void operator delete[] (void* memblock){
14 HeapFree (GetProcessHeap(), NULL, memblock);
15 }
16
17 void main ()
18 {
19 TCHAR* str = new TCHAR[250];
20 wsprintf (str, TEXT("The system has run %d\n"), GetTickCount());
21 MessageBox (NULL,str,str,MB_ICONINFORMATION);
22 delete[] str;
23 }
采用VC9命令行编译 cl msgbox.cpp /link /align:16,编译后大小为944 byte。
没有评论:
发表评论