找回密码
 注册
搜索
热搜: 回贴

优化C 程序编译效率的实例,C ,Win32/64编程

2010-1-30 18:28| 发布者: admin| 查看: 121| 评论: 0|原作者: 小可爱


优化C 程序编译效率的实例,C ,Win32/64编程
2008年06月23日 星期一 下午 09:38
(1)自定义程序的入口

(2)合并区段

(3)不用调malloc,free等函数

(3)不用cout来输出,cin来输入

(4)如果调用了很多库函数如memset,stycpy等等

的话,请导入msvcrt.lib,不然编译器会在程序里面

导入静态库,这样的话程序就会大很多。。

(5)window编程不用mfc...

减少程序运行内存占用量可以调用

SetProcessWorkingSetSize(GetCurrentProcess(),-1,-1);



----------sample.cpp---------------------

#include

#include "sample.h"

#include "mydll.h"

#include "resource.h"

//这下面自定义函数入口

#pragma comment(linker, "/ENTRY:EntryPoint")

#pragma comment(linker,"/ALIGN:0x400")

//设置区段属性,跟区段在内存起始地址

//这里面要加写入的权限,不然程序就运行不了了

//E为执行,R为可读,W为可写

//更多的说明请参见msdn

#pragma comment(linker,"/SECTION:.text,ERW /ALIGN:0x1000")

//下面合并区段,

#pragma comment(linker,"/merge:.data=.text")

#pragma comment(linker,"/merge:.rdata=.text")

//下面导入函数

#pragma comment(lib,"mydll.lib")

//下面是函数的入口

//得到WinMain里面的几个参数

//HINSTANCE hInstance=GetModuleHandle(NULL)

//LPSTR lpCmdLine= GetCommandLine()

//int nCmdShow 这个可以自己填

void EntryPoint()

{

HINSTANCE hInstance;

hInstance=GetModuleHandle(NULL);

Sample pro(hInstance);

DialogBoxParam(hInstance,MAKEINTRESOURCE(IDD_DIALOG),NULL,(DLGPROC)Sample::DialogProc,(LPARAM)&pro);

}

INT_PTR CALLBACK Sample::DialogProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)

{

static Sample *pro;

switch(message)

{

case WM_INITDIALOG:

pro=(Sample *)lParam;

pro->hwnd=hwnd;

pro->Init();

break;

case WM_COMMAND:

pro->WndPro_Command(wParam,lParam);

break;

case WM_CLOSE:

pro->WndPro_Close();

break;

case WM_TIMER:

//这里每秒钟触发一次

//来减少内存占用量

//试着在任务管理器里面看一下,sample.exe内存占用量是不是只有200多k,在我这里是这样的

if(wParam==100)

{

SetProcessWorkingSetSize(GetCurrentProcess(),

-1,-1);

}

break;

}

return 0;

}

Sample::Sample(HINSTANCE hi)

{

this->hInstance=hi;

}

int Sample::WndPro_Command(WPARAM wParam,LPARAM lParam)

{

switch(wParam)

{

case IDOK:

this->LoadDll();

break;

case IDCANCEL:

this->WndPro_Close();

break;

}

return 0;

}

int Sample::WndPro_Close()

{

EndDialog(hwnd,0);

return 0;

}

int Sample::Init()

{

SetTimer(hwnd,100,100,NULL);

return 0;

}

int Sample::LoadDll()

{

test(hwnd);

return 0;

}

UINT malloc_(size_t num)

{

return (UINT)VirtualAlloc(NULL, num,MEM_RESERVE |MEM_COMMIT,PAGE_EXECUTE_READWRITE);

}

void free_(void * p)

{

VirtualFree(p,NULL,MEM_RELEASE);

}

void ZeroMem(char * mem,int len)

{

for(int i=0;i
mem[i]=0;

}

-------------------------------

----------sample.h--------

//下面是主函数的类

class Sample

{

public:

static INT_PTR CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);

Sample(HINSTANCE);

private:

HINSTANCE hInstance;

HWND hwnd;

int WndPro_Command(WPARAM,LPARAM);

int WndPro_Close();

int Init();

int LoadDll();

};

///-->下为精简的库函数

//分配内存最好不用库函数malloc,

//不要用cout 来输出

//如果你调用了很多库函数的话

//请在导入 msvcrt.lib

UINT malloc_(size_t);//分配内存

void free_(void *);//释放内存

void ZeroMem(char *,int);//清零

-------------------------------

---------mydll.cpp-----------

#include

#include "mydll.h"

#pragma comment(linker, "/ENTRY:DllEntry")

#pragma comment(linker,"/ALIGN:0x400")

#pragma comment(linker,"/SECTION:.text,ERW /ALIGN:0x1000")

#pragma comment(linker,"/merge:.data=.text")

#pragma comment(linker,"/merge:.rdata=.text")

BOOL APIENTRY DllEntry()

{

return 1;

}

INT APIENTRY test(HWND hwnd)

{

MessageBox(hwnd,"调用dll"," ",0);

return 0;

}




最新评论

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 13:18 , Processed in 0.170071 second(s), 12 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

返回顶部