|
|
用户名:chriszz 笔名:Chris 地区: 黑龙江-哈尔滨 行业:其他 |
| 日 | 一 | 二 | 三 | 四 | 五 | 六 |
For honor, for freedom
[转贴] Win 2000/XP自启动程序解析
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 /?
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.:)
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 的实际执行过程是:
所以,ExitProcess不负责任何对象的析构,exit只负责析构全局对象,return 0可以析构局部对象并调用exit,因此能析构全部对象。
小鼠故事
喜欢和朋友坐在一起聊天。
生活中总是充满了各式各样的乐趣。
快乐的生活一直持续到开始找工作的那天......
自此生活不再多彩......
渐渐的发现了自己的浅薄。
甚至一度走入绝境......
待续...
小议static
转载自:
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 用以生成唯一的标志。
珍惜……
调试程序时发现的两段错误代码,挺有趣的。
调试程序时发现这么两段代码。
虽然错误挺低级的,但是感觉还算蛮经典的。
案例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 ++;
}
运行时动态调用未知函数的方法
在做项目时,遇到这样一个问题:需要编写一个类似插件的功能,即可以动态调用一个动态链接库中的函数。然而,由于种种原因,函数的参数的个数和类型均是未知的。因此,不得不想出一个能够动态调用未知个数的参数的函数的方法(听上去有些拗口,呵呵)
to be continued...