首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 计算机考试 > 等级考试 > 复习指导 >

C++基础解析十一

2008-11-21 
C++函数(成功的dll注入函数)、NumberEdit控件,能设定小数位和整数位

    一、C++函数(成功的dll注入函数)

    BOOL inject_dll( const char *dll_path, const DWORD remote_pro_id )
  {
  HANDLE h_token;
  if ( OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &h_token ) )
  {
  TOKEN_PRIVILEGES tkp;
  //修改进程权限
  LookupPrivilegeValue( NULL,SE_DEBUG_NAME, &tkp.Privileges[0].Luid );
  tkp.PrivilegeCount = 1;
  tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
  //通知系统修改进程权限
  AdjustTokenPrivileges( h_token, FALSE, &tkp, sizeof( tkp ), NULL, NULL );
  }
  HANDLE h_remote_process;
  //打开远程线程
  if( ( h_remote_process = OpenProcess( PROCESS_CREATE_THREAD | //允许远程创建线程
  PROCESS_VM_OPERATION | //允许远程VM操作
  PROCESS_VM_WRITE, //允许远程VM写
  FALSE, remote_pro_id ) )== NULL )
  {
  //AfxMessageBox("OpenProcess Error!");
  return FALSE;
  }
  char *lib_func_buf;
  //在远程进程的内存地址空间分配DLL文件名缓冲区
  lib_func_buf = (char *) VirtualAllocEx( h_remote_process, NULL, lstrlen(dll_path) + 1,
  MEM_COMMIT, PAGE_READWRITE);
  if( lib_func_buf == NULL )
  {
  //AfxMessageBox("VirtualAllocEx error! ");
  return FALSE;
  }
  //将DLL的路径名复制到远程进程的内存空间
  if( WriteProcessMemory( h_remote_process,
  lib_func_buf, ( void * )dll_path, lstrlen( dll_path ) + 1, NULL ) == 0 )
  {
  //AfxMessageBox("WriteProcessMemory Error");
  return FALSE;
  }
  //计算LoadLibraryA的入口地址
  PTHREAD_START_ROUTINE load_start_addr = ( PTHREAD_START_ROUTINE )
  GetProcAddress( GetModuleHandle( TEXT("Kernel32") ), "LoadLibraryA");
  if( load_start_addr == NULL )
  {
  //AfxMessageBox("GetProcAddress Error");
  return FALSE;
  }
  HANDLE h_remote_thread;
  if( (h_remote_thread = CreateRemoteThread( h_remote_process, NULL, 0,
  load_start_addr, lib_func_buf, 0, NULL ) ) == NULL)
  {
  //AfxMessageBox("CreateRemoteThread Error");
  return FALSE;
  }
  return TRUE;
  }

    二、NumberEdit控件,能设定小数位和整数位

    实现代码如下:
  //NumberEdit.h
  #ifndef __NUMBEREDIT_H__
  #define __NUMBEREDIT_H__
  class CNumberEdit : public CEdit
  {
  public:
  CNumberEdit();
  virtual ~CNumberEdit();
  void SetAfterDotLen(UINT iAfterDotLen){m_iAfterDotLen =iAfterDotLen;}
  UINT GetAfterDotLen(){return m_iAfterDotLen;}
  void SetAfterIntLen(UINT iAfterIntLen){m_iAfterIntLen =iAfterIntLen;}
  UINT GetAfterIntLen(){return m_iAfterIntLen;}
  protected:
  BOOL CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags);
  //BOOL CheckOnePlus(UINT nChar,UINT nRepCnt,UINT nFlags);
  BOOL CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags);
  BOOL CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags);
  int GetCaretXPos();
  CString m_str;
  UINT m_iAfterDotLen;
  UINT m_iAfterIntLen;
  protected:
  //{{AFX_MSG(CUntcNumberEdit)
  afx_msg void OnChar(UINT nChar, UINT nRepCnt, UINT nFlags);
  afx_msg void OnKillfocus();
  //}}AFX_MSG
  DECLARE_MESSAGE_MAP()
  };
  #endif
  //NumberEdit.cpp
  #include "stdafx.h"
  #include "NumberEdit.h"
  CNumberEdit::CNumberEdit()
  {
  m_iAfterDotLen = 4;
  m_iAfterIntLen = 4;
  m_str = _T("0.00");
  }
  CNumberEdit::~CNumberEdit()
  {
  }
  BEGIN_MESSAGE_MAP(CNumberEdit, CEdit)
  //{{AFX_MSG_MAP(CNumberEdit)
  ON_WM_CHAR()
  ON_CONTROL_REFLECT(EN_KILLFOCUS, OnKillfocus)
  //}}AFX_MSG_MAP
  END_MESSAGE_MAP()
  void CNumberEdit::OnChar(UINT nChar, UINT nRepCnt, UINT nFlags)
  {
  // TODO: Add your message handler code here and/or call default
  if(nChar == 8)
  {
  CEdit::OnChar(nChar, nRepCnt, nFlags);
  return;
  }
  BOOL bChange = FALSE;
  GetWindowText(m_str);
  if(CheckNumber(nChar,nRepCnt,nFlags))
  {
  bChange = TRUE;
  }
  //else if(CheckOnePlus(nChar,nRepCnt,nFlags))
  //{
  // ;//do nothing
  //}
  else if(CheckOneMinus(nChar,nRepCnt,nFlags))
  {
  bChange = TRUE;
  }
  else if(CheckOneDot(nChar,nRepCnt,nFlags))
  {
  bChange = TRUE;
  }
  if(bChange)
  {
  CEdit::OnChar(nChar, nRepCnt, nFlags);
  //SetFormatText();
  //m_Point.x += m_Point.y*6;
  //SetCaretPos(m_Point);
  }
  }
  void CNumberEdit::OnKillfocus()
  {
  GetWindowText(m_str);
  int iLoop = m_iAfterDotLen;
  if(m_str.IsEmpty())
  {
  m_str = "0.";
  }
  else if(m_str.GetLength() == 1 && m_str[0] == TCHAR('-'))
  {
  m_str = "0.";
  }
  else
  {
  int iDotPos = m_str.Find(TCHAR('.'));
  if(iDotPos == 0)
  m_str = "0"+ m_str;
  else if(iDotPos <0)
  m_str+=".";
  iDotPos = m_str.Find(TCHAR('.'));
  int iLen = m_str.GetLength() - 1 - iDotPos;
  iLoop = (int)m_iAfterDotLen - iLen;
  }
  if(iLoop >= 0)
  {
  for(int i=0;i<iLoop;i++)
  m_str+="0";
  }
  else
  {
  m_str = m_str.Mid(0,m_str.GetLength()+iLoop);
  }
  SetWindowText(m_str);
  }
  BOOL CNumberEdit::CheckNumber(UINT nChar,UINT nRepCnt,UINT nFlags)
  {
  if(::isdigit(nChar)==0)
  return FALSE;
  if (m_str =="0")
  {
  return FALSE;
  }
  int iDotPos = m_str.Find(TCHAR('.'));
  if(iDotPos >= 0)
  {
  int iLen = m_str.GetLength() - 1 - iDotPos;
  if( (GetCaretXPos() > iDotPos) && (iLen >= (int)m_iAfterDotLen))
  {
  return FALSE;
  }
  CString first = m_str.Left(1);
  if (first=="0")
  {
  if ((GetCaretXPos() <= iDotPos) && (GetCaretXPos() != 0))
  {
  return FALSE;
  }
  }
  int iIntLen = iDotPos;
  if (iIntLen>=(int)m_iAfterIntLen)
  {
  return FALSE;
  }
  }
  else
  {
  int iIntLen = m_str.GetLength();
  if (iIntLen>=(int)m_iAfterIntLen)
  {
  return FALSE;
  }
  }
  return TRUE;
  }
  BOOL CNumberEdit::CheckOneMinus(UINT nChar,UINT nRepCnt,UINT nFlags)
  {
  if(nChar != '-')
  return FALSE;
  if(GetCaretXPos() != 0)
  return FALSE;
  if(!m_str.IsEmpty() && m_str.GetAt(0) == TCHAR('-'))
  return FALSE;
  if(nChar == '-')
  return FALSE;
  return TRUE;
  }
  BOOL CNumberEdit::CheckOneDot(UINT nChar,UINT nRepCnt,UINT nFlags)
  {
  if(nChar != '.')
  return FALSE;
  if(m_str.Find(TCHAR('.')) >=0)
  return FALSE;
  int iPos = GetCaretXPos();
  if(iPos == 0)
  return FALSE;
  else if(iPos==1 && m_str[0] == TCHAR('-'))
  return FALSE;
  return TRUE;
  }

 

3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/

热点排行