关于作者

姓名:郑仲

性别:男

出生日期:1980-03-14

地区:黑龙江-哈尔滨

联系电话:

QQ:6154774婚否:未婚
用户名:chriszz
笔名:Chris
地区: 黑龙江-哈尔滨
行业:其他

日历  

快速登录

+ 用户名:
+ 密 码:

在线留言



My friends' blogs

Some Links

访问统计:
文章个数:11
评论个数:11
留言条数:0




Powered by BlogDriver 2.1

神之黄昏

 

For honor, for freedom

文章

[转贴] Win 2000/XP自启动程序解析
当Windows完成登录过程,鼠标指针从繁忙到安静,除桌面上的图标,你还看到了什么?也许表面没什么变化,但你有没有注意到,你的系统托盘区多出了许多图标,你的进程表中出现了很多的进程!Windows在启动的时候,自动加载了很多程序,你知道它们是在什么地方被加载的吗? 
  许多程序的自启动,给我们带来了很多方便,这是不争的事实,但是否每个自启动的程序对我们都有用呢?更甚者,也许有病毒或木马在自启动行列,而你却不知!

  到现在,你是不是觉得了解自启动文件的藏身之处有必要呢?那好,下面我就一一指出,让它们无外可藏!

  其实Windows2000/XP中的自启动文件,除了从以前系统中遗留下来的Autoexec.bat文件中加载外,按照两个文件夹和9个核心注册表子键来自动加载程序的。

  1)“启动”文件夹--最常见的自启动程序文件夹。它位于系统分区的“Documents and Settings-->User-->〔开始〕菜单-->程序”目录下。这时的User指的是你登录的用户名。

  2)“All Users”中的自启动程序文件夹--另一个常见的自启动程序文件夹。它位于系统分区的“Documents and Settings-->All User-->〔开始〕菜单-->程序”目录下。前面提到的“启动”文件夹运行的是登录用户的自启动程序,而“All Users”中启动的程序是在所有用户下都有效(不论你用什么用户登录)。

  3)“Load”键值--一个埋藏得较深的注册表键值。位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows\load〕主键下。

  4)“Userinit”键值--它则位于〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\Userinit〕主键下,也是用于系统启动时加载程序的。一般情况下,其默认值为“userinit.exe”,由于该子键的值中可使用逗号分隔开多个程序,因此,在键值的数值中可加入其它程序。

  5)“Explorer\Run”键值--与“load”和“Userinit”两个键值不同的是,“Explorer\Run”同时位于〔HKEY_CURRENT_USER〕和〔HKEY_LOCAL_MACHINE〕两个根键中。它在两个中的位置分别为〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run〕下。

  6)“RunServicesOnce”子键--它在用户登录前及其它注册表自启动程序加载前面加载。这个键同时位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServicesOnce〕下。

  7)“RunServices”子键--它也是在用户登录前及其它注册表自启动程序加载前面加载。这个键同时位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunServices〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunServices〕下。

  8)“RunOnce\Setup”子键--其默认值是在用户登录后加载的程序。这个键同时位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce\Setup〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce\Setup〕下。

  9)“RunOnce”子键--许多自启动程序要通过RunOnce子键来完成第一次加载。这个键同时位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\RunOnce〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce〕下。位于〔HKEY_CURRENT_USER〕根键下的RunOnce子键在用户登录扣及其它注册表的Run键值加载程序前加载相关程序,而位于〔HKEY_LOCAL_MACHINE〕主键下的Runonce子键则是在操作系统处理完其它注册表Run子键及自启动文件夹内的程序后再加载的。在Windows XP中还多出一个〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnceEX〕子键,其道理相同。

  10)“Run”子键--目前最常见的自启动程序用于加载的地方。这个键同时位于〔HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run〕和〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run〕下。其中位于〔HKEY_CURRENT_USER〕根键下的Run键值紧接着〔HKEY_LOCAL_MACHINE〕主键下的Run键值启动,但两个键值都是在“启动”文件夹之前加载。

  11)再者就是Windows中加载的服务了,它的级别较高,用于最先加载。其位于〔HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services〕下,看到了吗,你所有的服务加载程序都在这里了!

  12)Windows Shell──它位于〔HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Winlogon\〕下面的Shell字符串类型键值中,基默认值为Explorer.exe,当然可能木马程序会在此加入自身并以木马参数的形式调用资源管理器,以达到欺骗用户的目的。

  13)BootExecute──它位于注册表中〔HKEY_LOCAL_MACHINE\System\ControlSet001\Session Manager\〕下面,有一个名为BootExecute的多字符串值键,它的默认值是"autocheck autochk *",用于系统启动时的某些自动检查。这个启动项目里的程序是在系统图形界面完成前就被执行的,所以具有很高的优先级。

  14)策略组加载程序——打开Gpedit.msc,展开“用户配置——管理模板——系统——登录”,就可以看到“在用户登录时运行这些程序”的项目,你可以在里面添加。在注册表中[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Group Policy Objects\本地User\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer\Run]你也可以看到相对应的键值。

- 作者: chriszz 2005年07月15日, 星期五 15:16  回复(0) |  引用(0) 加入博采

Command Line Tools: reg.exe

Tools Name: reg.exe

Location:      %windir%\SYSTEM32\reg.exe

Remarks: This is a powerful tools for you to query/modify your registry.

Example:

reg query HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework | find ".NETFramework\v"

Brief Information:

Console Registry Tool for Windows - version 3.0
Copyright (C) Microsoft Corp. 1981-2001.  All rights reserved


REG Operation [Parameter List]

  Operation  [ QUERY   | ADD    | DELETE  | COPY    |
               SAVE    | LOAD   | UNLOAD  | RESTORE |
               COMPARE | EXPORT | IMPORT ]

Return Code: (Except of REG COMPARE)

  0 - Succussful
  1 - Failed

For help on a specific operation type:

  REG Operation /?

Examples:

  REG QUERY /?
  REG ADD /?
  REG DELETE /?
  REG COPY /?
  REG SAVE /?
  REG RESTORE /?
  REG LOAD /?
  REG UNLOAD /?
  REG COMPARE /?
  REG EXPORT /?
  REG IMPORT /?

- 作者: Chris 2005年07月14日, 星期四 18:27  回复(0) |  引用(0) 加入博采

阴天的公园
摘要:公元2005年5月8号。同mm去哈尔滨儿童公园拍照。 捡了几张照片,特此留念。 查看全文

- 作者: Chris 2005年05月7日, 星期六 23:59  回复(3) |  引用(0) 加入博采

Customize CSplitterWnd

Maybe you have ever used the MFC class CSplitterWnd in your applications. But you may find that the class have quite few interface functions for you to customize the style of the splitter bars and the windows. To resolve the problem we have to derive a class of our own from the base class CSplitterWnd.


The decleration of the CSplitterWnd class is:

/////////////////////////////////////////////////////////////////////////////
// Splitter Window

#define SPLS_DYNAMIC_SPLIT  0x0001
#define SPLS_INVERT_TRACKER 0x0002  // obsolete (now ignored)

class CSplitterWnd : public CWnd
{
 DECLARE_DYNAMIC(CSplitterWnd)

// Construction
public:
 CSplitterWnd();
 // Create a single view type splitter with multiple splits
 virtual BOOL Create(CWnd* pParentWnd,
    int nMaxRows, int nMaxCols, SIZE sizeMin,
    CCreateContext* pContext,
    DWORD dwStyle = WS_CHILD | WS_VISIBLE |
     WS_HSCROLL | WS_VSCROLL | SPLS_DYNAMIC_SPLIT,
    UINT nID = AFX_IDW_PANE_FIRST);

 // Create a multiple view type splitter with static layout
 virtual BOOL CreateStatic(CWnd* pParentWnd,
    int nRows, int nCols,
    DWORD dwStyle = WS_CHILD | WS_VISIBLE,
    UINT nID = AFX_IDW_PANE_FIRST);

 virtual BOOL CreateView(int row, int col, CRuntimeClass* pViewClass,
   SIZE sizeInit, CCreateContext* pContext);

// Attributes
public:
 int GetRowCount() const;
 int GetColumnCount() const;

 // information about a specific row or column
 void GetRowInfo(int row, int& cyCur, int& cyMin) const;
 void SetRowInfo(int row, int cyIdeal, int cyMin);
 void GetColumnInfo(int col, int& cxCur, int& cxMin) const;
 void SetColumnInfo(int col, int cxIdeal, int cxMin);

 // for setting and getting shared scroll bar style
 DWORD GetScrollStyle() const;
 void SetScrollStyle(DWORD dwStyle);

 // views inside the splitter
 CWnd* GetPane(int row, int col) const;
 BOOL IsChildPane(CWnd* pWnd, int* pRow, int* pCol);
 AFX_DEPRECATED BOOL IsChildPane(CWnd* pWnd, int& row, int& col); // obsolete
 int IdFromRowCol(int row, int col) const;

 BOOL IsTracking();  // TRUE during split operation

// Operations
public:
 virtual void RecalcLayout();    // call after changing sizes

// Overridables
protected:
 // to customize the drawing
 enum ESplitType { splitBox, splitBar, splitIntersection, splitBorder };
 virtual void OnDrawSplitter(CDC* pDC, ESplitType nType, const CRect& rect);
 virtual void OnInvertTracker(const CRect& rect);

public:
 // for customing scrollbar regions
 virtual BOOL CreateScrollBarCtrl(DWORD dwStyle, UINT nID);

 // for customing DYNAMIC_SPLIT behavior
 virtual void DeleteView(int row, int col);
 virtual BOOL SplitRow(int cyBefore);
 virtual BOOL SplitColumn(int cxBefore);
 virtual void DeleteRow(int rowDelete);
 virtual void DeleteColumn(int colDelete);

 // determining active pane from focus or active view in frame
 virtual CWnd* GetActivePane(int* pRow = NULL, int* pCol = NULL);
 virtual void SetActivePane(int row, int col, CWnd* pWnd = NULL);
protected:
 AFX_DEPRECATED CWnd* GetActivePane(int& row, int& col); // obsolete

public:
 // high level command operations - called by default view implementation
 virtual BOOL CanActivateNext(BOOL bPrev = FALSE);
 virtual void ActivateNext(BOOL bPrev = FALSE);
 virtual BOOL DoKeyboardSplit();

 // synchronized scrolling
 virtual BOOL DoScroll(CView* pViewFrom, UINT nScrollCode,
  BOOL bDoScroll = TRUE);
 virtual BOOL DoScrollBy(CView* pViewFrom, CSize sizeScroll,
  BOOL bDoScroll = TRUE);

// Implementation
public:
 virtual ~CSplitterWnd();
#ifdef _DEBUG
 virtual void AssertValid() const;
 virtual void Dump(CDumpContext& dc) const;
#endif

 // implementation structure
 struct CRowColInfo
 {
  int nMinSize;       // below that try not to show
  int nIdealSize;     // user set size
  // variable depending on the available size layout
  int nCurSize;       // 0 => invisible, -1 => nonexistant
 };

protected:
 // customizable implementation attributes (set by constructor or Create)
 CRuntimeClass* m_pDynamicViewClass;
 int m_nMaxRows, m_nMaxCols;

 // implementation attributes which control layout of the splitter
 int m_cxSplitter, m_cySplitter;         // size of splitter bar
 int m_cxBorderShare, m_cyBorderShare;   // space on either side of splitter
 int m_cxSplitterGap, m_cySplitterGap;   // amount of space between panes
 int m_cxBorder, m_cyBorder;             // borders in client area

 // current state information
 int m_nRows, m_nCols;
 BOOL m_bHasHScroll, m_bHasVScroll;
 CRowColInfo* m_pColInfo;
 CRowColInfo* m_pRowInfo;

 // Tracking info - only valid when 'm_bTracking' is set
 BOOL m_bTracking, m_bTracking2;
 CPoint m_ptTrackOffset;
 CRect m_rectLimit;
 CRect m_rectTracker, m_rectTracker2;
 int m_htTrack;

 // implementation routines
 BOOL CreateCommon(CWnd* pParentWnd, SIZE sizeMin, DWORD dwStyle, UINT nID);
 virtual int HitTest(CPoint pt) const;
 virtual void GetInsideRect(CRect& rect) const;
 virtual void GetHitRect(int ht, CRect& rect);
 virtual void TrackRowSize(int y, int row);
 virtual void TrackColumnSize(int x, int col);
 virtual void DrawAllSplitBars(CDC* pDC, int cxInside, int cyInside);
 virtual void SetSplitCursor(int ht);
 CWnd* GetSingParent();

 // starting and stopping tracking
 virtual void StartTracking(int ht);
 virtual void StopTracking(BOOL bAccept);

 // special command routing to frame
 virtual BOOL OnCommand(WPARAM wParam, LPARAM lParam);
 virtual BOOL OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult);

 //{{AFX_MSG(CSplitterWnd)
 afx_msg BOOL OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message);
 afx_msg void OnMouseMove(UINT nFlags, CPoint pt);
 afx_msg void OnPaint();
 afx_msg void OnLButtonDown(UINT nFlags, CPoint pt);
 afx_msg void OnLButtonDblClk(UINT nFlags, CPoint pt);
 afx_msg void OnLButtonUp(UINT nFlags, CPoint pt);
 afx_msg void OnCancelMode();
 afx_msg void OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags);
 afx_msg void OnSize(UINT nType, int cx, int cy);
 afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);
 afx_msg BOOL OnMouseWheel(UINT nFlags, short zDelta, CPoint pt);
 afx_msg BOOL OnNcCreate(LPCREATESTRUCT lpcs);
 afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
 afx_msg void OnDisplayChange();
 //}}AFX_MSG
 DECLARE_MESSAGE_MAP()
};

You cannot derive this class from MFC Class Wizard. Instead, you may manully choose Generic C++ Class form Class Wizard, and fill the Base Class' name with "CSplitterWnd". The Visual Studio will automatically include the header file "winext.h".

After these steps, you can customize the style of the splitter windows as you wish.:)

- 作者: chriszz 2005年04月17日, 星期日 17:13  回复(0) |  引用(0) 加入博采

Windows下return,exit和ExitProcess的区别和分析

(转载请注明出处)

通常,我们为了使自己的程序结束,会在主函数中使用return或调用exit()。在windows下还有ExitProcess()和TerminateProcess()等函数。
本文的目的是比较以上几种结束程序的方式的区别,并分析其原理。


首先我们用一个例子来说明几种结束方式的区别。
测试环境为Windows XP HOME SP2,编译器为Visual Studio.net 2003

测试代码如下:
#include
#include
#include

class Test
{
public:
 Test (int i) {m_i=i; printf ("construct %d\n", m_i);};
 ~Test () {printf ("destruct %d\n", m_i);};
private:
 int m_i;
};

Test t_1 (1);

int main(int argc, char* argv[])
{
 Test t_2 (2);
 printf("Hello World!\n");
// return 0;
// exit (0);
// ExitProcess (0);
}

我们的目标是察看两种结束方式有什么不同。

程序在运行的结果为:

使用return 0结束时:
construct 1
construct 2
Hello World!
destruct 2
destruct 1

使用exit (0)结束时:
construct 1
construct 2
Hello World!
destruct 1

使用ExitProcess (0)结束时:
construct 1
construct 2
Hello World!

从结果上我们可以看出来,采用return来结束进程可以正确的析构全局和局部对象。而采用exit()来结束进程时全局对象可以正确析构,但局部对象没有正确析构。采用ExitProcess(0)结束时全局和局部对象都没有正确析构。

为什么会出现这样的情况呢?
《Windows核心编程》中我们可以得到以下解释:
"当主线程的进入点函数(WinMain、wWinMain、main或wmain)返回时,它将返回给C/C++运行期启动代码,它能够正确地清楚该进程使用的所有C运行期资源。当C运行期资源被释放之后,C运行期启动代码就显式的调用ExitProcess,并将进入点函数返回的值传递给它。"

那么,通过跟踪代码我们可以发现:
return 0实际上执行了以下操作:
 return 0;
00401035  mov         dword ptr [ebp-0D4h],0
0040103F  lea         ecx,[t_2]
00401042  call        Test::~Test (4010F0h)
00401047  mov         eax,dword ptr [ebp-0D4h]
}
0040104D  push        edx 
0040104E  mov         ecx,ebp
00401050  push        eax 
00401051  lea         edx,ds:[401072h]
00401057  call        _RTC_CheckStackVars (4011E0h)
0040105C  pop         eax 
0040105D  pop         edx 
0040105E  pop         edi 
0040105F  pop         esi 
00401060  pop         ebx 
00401061  add         esp,0D8h
00401067  cmp         ebp,esp
00401069  call        _RTC_CheckEsp (4011B0h)
0040106E  mov         esp,ebp
00401070  pop         ebp 
00401071  ret             
在ret之后,程序返回到启动main函数的代码,并执行以下操作:
            if ( !managedapp )
                exit(mainret);
            _cexit();

可见return 0上调用了局部对象t_2的析构函数。


void __cdecl exit (
        int code
        )
{
        doexit(code, 0, 0); /* full term, kill process */
}

void __cdecl _cexit (
        void
        )
{
        doexit(0, 0, 1);    /* full term, return to caller */
}
         
实际上程序调用了doexit函数。
static void __cdecl doexit (
        int code,
        int quick,
        int retcaller
        )
{
#ifdef _DEBUG
        static int fExit = 0;
#endif  /* _DEBUG */

#ifdef _MT
        _lockexit();        /* assure only 1 thread in exit path */
        __TRY
#endif  /* _MT */

            if (_C_Exit_Done == TRUE)                               /* if doexit() is being called recursively */
                    TerminateProcess(GetCurrentProcess(),code);     /* terminate with extreme
prejudice */
            _C_Termination_Done = TRUE;

            /* save callable exit flag (for use by terminators) */
            _exitflag = (char) retcaller;  /* 0 = term, !0 = callable exit */

            if (!quick) {

                /*
                 * do _onexit/atexit() terminators
                 * (if there are any)
                 *
                 * These terminators MUST be executed in reverse order (LIFO)!
                 *
                 * NOTE:
                 *  This code assumes that __onexitbegin points
                 *  to the first valid onexit() entry and that
                 *  __onexitend points past the last valid entry.
                 *  If __onexitbegin == __onexitend, the table
                 *  is empty and there are no routines to call.
                 */

                if (__onexitbegin) {
                    while ( --__onexitend >= __onexitbegin )
                    /*
                     * if current table entry is non-NULL,
                     * call thru it.
                     */
                    if ( *__onexitend != NULL )
                        (**__onexitend)();
                }

                /*
                 * do pre-terminators
                 */
                _initterm(__xp_a, __xp_z);
            }

            /*
             * do terminators
             */
            _initterm(__xt_a, __xt_z);


#ifndef CRTDLL
#ifdef _DEBUG
            /* Dump all memory leaks */
            if (!fExit && _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG) & _CRTDBG_LEAK_CHECK_DF)
            {
                fExit = 1;
                _CrtDumpMemoryLeaks();
            }
#endif  /* _DEBUG */
#endif  /* CRTDLL */

            /* return to OS or to caller */

#ifdef _MT
        __FINALLY
            if (retcaller)
                _unlockexit();      /* unlock the exit code path */
        __END_TRY_FINALLY
#endif  /* _MT */
        if (retcaller)
            return;


        _C_Exit_Done = TRUE;

        __crtExitProcess(code);
}

其中部分源代码如下:
                if (__onexitbegin) {
00406056  cmp         dword ptr [___onexitbegin (412DA8h)],0
0040605D  je          doexit+70h (406090h)
                    while ( --__onexitend >= __onexitbegin )
0040605F  mov         edx,dword ptr [___onexitend (412DA4h)]
00406065  sub         edx,4
00406068  mov         dword ptr [___onexitend (412DA4h)],edx
0040606E  mov         eax,dword ptr [___onexitend (412DA4h)]
00406073  cmp         eax,dword ptr [___onexitbegin (412DA8h)]
00406079  jb          doexit+70h (406090h)
                    /*
                     * if current table entry is non-NULL,
                     * call thru it.
                     */
                    if ( *__onexitend != NULL )
0040607B  mov         ecx,dword ptr [___onexitend (412DA4h)]
00406081  cmp         dword ptr [ecx],0
00406084  je          doexit+6Eh (40608Eh)
                        (**__onexitend)();
00406086  mov         edx,dword ptr [___onexitend (412DA4h)]
0040608C  call        dword ptr [edx]
                }
0040608E  jmp         doexit+3Fh (40605Fh)

程序在0040608C处跳转到如下代码:
0040EC10  push        ebp 
0040EC11  mov         ebp,esp
0040EC13  sub         esp,0C0h
0040EC19  push        ebx 
0040EC1A  push        esi 
0040EC1B  push        edi 
0040EC1C  lea         edi,[ebp-0C0h]
0040EC22  mov         ecx,30h
0040EC27  mov         eax,0CCCCCCCCh
0040EC2C  rep stos    dword ptr [edi]
0040EC2E  mov         ecx,offset t_1 (412760h)
0040EC33  call        Test::~Test (4010F0h)
0040EC38  pop         edi 
0040EC39  pop         esi 
0040EC3A  pop         ebx 
0040EC3B  add         esp,0C0h
0040EC41  cmp         ebp,esp
0040EC43  call        _RTC_CheckEsp (4011B0h)
0040EC48  mov         esp,ebp
0040EC4A  pop         ebp 
0040EC4B  ret             
在这里,全局变量t_1被析构。
在doexit的最后,程序调用
        __crtExitProcess(code);

void __cdecl __crtExitProcess (
        int status
        )
{
        HMODULE hmod;
        PFN_EXIT_PROCESS pfn;

        hmod = GetModuleHandle("mscoree.dll");
        if (hmod != NULL) {
            pfn = (PFN_EXIT_PROCESS)GetProcAddress(hmod, "CorExitProcess");
            if (pfn != NULL) {
                pfn(status);
            }
        }

        /*
         * Either mscoree.dll isn't loaded,
         * or CorExitProcess isn't exported from mscoree.dll,
         * or CorExitProcess returned (should never happen).
         * Just call ExitProcess.
         */

        ExitProcess(status);
}
在这里,终于调用到了ExitProcess。至此,全局对象t_1和局部对象t_2都完成了析构操作。

从分析过程,我们可以得出以下结论。
在Windows下,return 0 的实际执行过程是:

  • 先析构main函数内的局部对象。
  • 返回至调用main的函数。
  • 调用exit函数,由exit函数调用doexit函数,在doexit函数中完成对全局对象的析构。
  • 最后调用ExitProcess结束进程。

所以,ExitProcess不负责任何对象的析构,exit只负责析构全局对象,return 0可以析构局部对象并调用exit,因此能析构全部对象。

- 作者: chriszz 2005年03月8日, 星期二 21:21  回复(1) |  引用(0) 加入博采

小鼠故事
大家好,我是故事的主角,是一只快乐的小鼠......


 

喜欢和朋友坐在一起聊天。

生活中总是充满了各式各样的乐趣。


快乐的生活一直持续到开始找工作的那天......


自此生活不再多彩......


渐渐的发现了自己的浅薄。


甚至一度走入绝境......


待续...

- 作者: chriszz 2004年12月28日, 星期二 21:52  回复(2) |  引用(0) 加入博采

小议static
注:本文为转载文章。
转载自:
http://blog.csdn.net/hustli/

转载自:
http://blog.csdn.net/hustli/
这是很早之前写的一篇文章,呵呵,铁出来仅供参考 

1、什么是static?
       static 是C++中很常用的修饰符,它被用来控制变量的存储方式和可见性。

    2、为什么要引入static?
       函数内部定义的变量,在程序执行到它的定义处时,编译器为它在栈上分配空间,大家知道,函数在栈上分配的空间在此函数执行结束时会释放掉,这样就产生了一 个问题: 如果想将函数中此变量的值保存至下一次调用时,如何实现? 最容易想到的方法是定义一个全局的变量,但定义为一个全局变量有许多缺点,最明显的缺点是破坏了此变量的访问范围(使得在此函数中定义的变量,不仅仅受此 函数控制)。

    3、什么时候用static?
       需要一个数据对象为整个类而非某个对象服务,同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见。

    4、static的内部机制:
       静态数据成员要在程序一开始运行时就必须存在。因为函数在程序运行中被调用,所以静态数据成员不能在任何函数内分配空间和初始化。
       这样,它的空间分配有三个可能的地方,一是作为类的外部接口的头文件,那里有类声明;二是类定义的内部实现,那里有类的成员函数定义;三是应用程序的main()函数前的全局数据声明和定义处
      静态数据成员要实际地分配空间,故不能在类的声明中定义(只能声明数据成员)。类声明只声明一个类的"尺寸和规格",并不进行实际的内存分配,所以在类声 明中写成定义是错误的。它也不能在头文件中类声明的外部定义,因为那会造成在多个使用该类的源文件中,对其重复定义。
      static被引入以告知编译器,将变量存储在程序的静态存储区而非栈上空间,静态
数据成员按定义出现的先后顺序依次初始化,注意静态成员嵌套时,要保证所嵌套的成员已经初始化了。消除时的顺序是初始化的反顺序。

    5、static的优势:
       可以节省内存,因为它是所有对象所公有的,因此,对多个对象来说,静态数据成员只存储一处,供所有对象共用。静态数据成员的值对每个对象都是一样,但它的 值是可以更新的。只要对静态数据成员的值更新一次,保证所有对象存取更新后的相同的值,这样可以提高时间效率。

    6、引用静态数据成员时,采用如下格式:
         <类名>::<静态成员名>
    如果静态数据成员的访问权限允许的话(即public的成员),可在程序中,按上述格式来引用静态数据成员。

    7、注意事项:
      (1)类的静态成员函数是属于整个类而非类的对象,所以它没有this指针,这就导致了它仅能访问类的静态数据和静态成员函数。
      (2)不能将静态成员函数定义为虚函数。
      (3)由于静态成员声明于类中,操作于其外,所以对其取地址操作,就多少有些特殊,变量地址是指向其数据类型的指针 ,函数地址类型是一个"nonmember函数指针"。
      (4)由于静态成员函数没有this指针,所以就差不多等同于nonmember函数,结果就产生了一个意想不到的好处:成为一个callback函数, 使得我们得以将C++和C-based X Window系统结合,同时也成功的应用于线程函数身上。
      (5)static并没有增加程序的时空开销,相反她还缩短了子类对父类静态成员的访问时间,节省了子类的内存空间。
      (6)静态数据成员在<定义或说明>时前面加关键字static。
      (7)静态数据成员是静态存储的,所以必须对它进行初始化。
      (8)静态成员初始化与一般数据成员初始化不同:
            初始化在类体外进行,而前面不加static,以免与一般静态变量或对象相混淆;
            初始化时不加该成员的访问权限控制符private,public等;
            初始化时使用作用域运算符来标明它所属类;
            所以我们得出静态数据成员初始化的格式:
         <数据类型><类名>::<静态数据成员名>=<值>
      (9)为了防止父类的影响,可以在子类定义一个与父类相同的静态变量,以屏蔽父类的影响。这里有一点需要注意:我们说静态成员为父类和子类共享,但我们有 重复定义了静态成员,这会不会引起错误呢?不会,我们的编译器采用了一种绝妙的手法:name-mangling 用以生成唯一的标志。

 

- 作者: chriszz 2004年12月24日, 星期五 20:03  回复(0) |  引用(0) 加入博采

珍惜……
从论坛上copy来的别人的签名档:

「一年」有多少价值,你去问一个失败重修的学生。
「一月」有多少价值,你去问一个不幸早产的母亲。
「一小时」有多少价值,你去问一对等待相聚的恋人。
「一秒钟」有多少价值,你去问一个死里逃生的幸运儿。
「一毫秒」有多少价值,你去问一个错失金牌的运动员。

珍视美好时光,不要把时间花在争辩,吵架和面子上



- 作者: chriszz 2004年12月16日, 星期四 11:35  回复(0) |  引用(0) 加入博采

调试程序时发现的两段错误代码,挺有趣的。

调试程序时发现这么两段代码。

虽然错误挺低级的,但是感觉还算蛮经典的。


案例1:
定义
#typedef struct _PROBE {
  int id;
  char ip[20];
  struct _PROBE *next
} * PROBE;

之后分配一链表头节点,
PROBE head;
head = (PROBE) malloc (sizeof(PROBE));
bzero (head, sizeof(PROBE));

之后对链表进行追加操作。
发现经过一系列操作后,
head->next值莫名奇妙的就变了。

跟踪程序,发现head->next是被一段毫无关系的程序修改的值。

案例2:
声明全局变量:
char *string = "fadgadfsagaAadf\0";
然后在某个函数内执行:
int i = 0;
while (string[i]!=0) {
  if (string[i]>="A" && stirng[i]<="Z") string[i]+= 0x20;
  i ++;

}

- 作者: chriszz 2004年10月27日, 星期三 16:04  回复(1) |  引用(0) 加入博采

运行时动态调用未知函数的方法

在做项目时,遇到这样一个问题:需要编写一个类似插件的功能,即可以动态调用一个动态链接库中的函数。然而,由于种种原因,函数的参数的个数和类型均是未知的。因此,不得不想出一个能够动态调用未知个数的参数的函数的方法(听上去有些拗口,呵呵)

to be continued...


由于未知参数个数,因此传统的参数传递方法是不可行的了。于是想到可不可以人为的书写函数的参数栈,只要我们能够按照c的规则将参数写入栈中,那么只需要让程序跳转到函数的入口地址就ok了~~

- 作者: chriszz 2004年10月6日, 星期三 20:33  回复(0) |  引用(0) 加入博采