编程杂记

1.libiconv库能实现utf8,多字节等编码之间的转换

2.log4cplus是gnu的开源日志系统。

3.CxImage是对图片格式之间转换的开源类。

4.暴风影音是从开源项目mpc起家的。

5.GetPrivateProfileStruct/WritePrivateProfileStruct 可以向 .ini 文件写入结构体。

6.MessageBeep函数可以以声音的方式产生提醒消息。

7.strtok可以把一个字符串按指定的分隔符隔离开。

8.对一个类加了命名空间就一定不要忘了在用该类做类型的时候加上命名空间,另外就是对于继承该类的时候,在前面也要加上 命名空间

9.CComBSTR需要加头文件 #include   <atlbase.h>

10.不要随便用 string & 做参数,这样的话, “dsdf” 等常量字符串就不能赋给它。

11. CStdString strIpPlusPort = NodeForAdd.IP + NodeForAdd.Port ;不行,因为string 没有重载这样的构造函数,可以写成

CStdString strIpPlusPort = NodeForAdd.Ip ;

strIpPlusPort += NodeForAdd ;

12.别忘了进行转换 NodeForAdd = (CNode*)new CSSMQNode ;

13.const型指针和非const型指针可以直接用括号来强制转换。

14.对于迭代器,要加上*后才能操作其内部的成员

 vector<CNode*>::iterator pVector = m_MQVectorAn.begin() ;

(*pVector)->ID == NodeForAdd->ID

15.GetCurrentThreadId()可以获取当前线程的id。

16.函数_tsplitpath需要加头文件 #include <tchar.h>

17.msdn的运行文件是 dexplore.exe

18.itoa(value, buffer,10) 第二个参数buffer不能为NULL;会抛异常,说明在里面用了 断言 确保不为NULL.

19.有的时候调dll的时候,测试程序可能进不到dll的函数里面,可以先把dll重新编译下,然后测试程序也重新编译下。。。应该就可以了。

20.string::substring(100),参数100不能超过string的最大长度,否则会导致运行时错误,注意,不会导致编译错误。 

21.加壳工具:upx,VMProtect,Armadillo,Themida,WinLicense

下面简单介绍下加壳的原理:

对于可执行程序资源压缩,是保护文件的常用手段. 俗称加壳,加壳过的程序可以直接运行,但是不能查看源代码.要经过脱壳才可以查看源代码.

  加壳:其实是利用特殊的算法,对EXE、DLL文件里的资源进行压缩。类似WINZIP 的效果,只不过这个压缩之后的文件,可以独立运行,解压过程完全隐蔽,都在内存中完成。解压原理,是加壳工具在文件头里加了一段指令,告诉CPU,怎么才能解压自己。当加壳时,其实就是给可执行的文件加上个外衣。用户执行的只是这个外壳程序。当执行这个程序的时候这个壳就会把原来的程序在内存中解开,解开后,以后的就交给真正的程序。

22.请注意:对于声明外部变量和外部函数基本相同,但也有点差别,如:

// 1.cpp

extern void Fun()// 这里的extern可加也可不加,表示这个函数可以被其它文件调用,如果不想被其它文件调用,只能在本文件中调用,则把extern替换成static

{}

int A = 0 ;// 这里的最前面一定不能加extern,加了将会出错,如果不想被其它文件调用,只能在本文本件中使用,则须在最前面加上static

// 2.cpp

void main()

{

extern void Fun() ;

extern int A ;

}

23.

typedef HINSTANCE HMODULE; 
24.将char*转换为BSTR:
LPSTR   a   =   "Hello   World ";
CComBSTR   b   =   a;
BSTR   c   =   b.m_str;
 

 char*     str[]   =   "abce ";
OLECHAR*     pwsz;

mbstowcs(pwsz,   str,   strlen(str));
BSTR   bstr   =   SysAllocString((OLESTR)   pwsz);
//   Using   the   bstr   ...

SysFreeString(bstr);  
25.想要用到com的一些特性,如ConvertBSTRToString(仅仅BSTR不需要),需要加载comsupp.lib或comsuppd.lib(debug版本的形式)如果是Unicode版本用的是comsuppw.lib 和comsuppwd.lib ,加载方式如下:#pragma comment (lib, "comsupp.lib"),加上头文件comutil.h(貌似不加也可以,自己看着办吧)

26.bstr_t需要加头文件 #include   <comdef.h>

27._com_util::ConvertBSTRToString()

28.char* key; 

BSTR strKey = bstr_t(key);

BSTR bstrValue = NULL;// 一定要初始化

nStatus = m_pIStore->GET(m_hHandle, strKey, &bstrValue);

CStdString strValue;

strValue =(char*)_bstr_t(bstrValue);

SysFreeString(bstrValue);

29.

m_Lock.on() ;
   if(m_mapMessage.count(strKey))
   {// 有值
    strValueTemp = m_mapMessage[strKey] ;
   }
   m_Lock.off() ;

   if (!strValueTemp.empty())
   {
    pWAMQ->DelMsg(WA_MQ_I, strValueTemp) ;
    // 提交网页分析结果返还消息
    CommitResultToRoutine(0, strValueTemp) ;
    // 删除消息映射中的相应的map项
    m_Lock.on() ;
    m_mapMessage.erase(key) ;
    m_Lock.off() ;
   }

 30.strSelectSql = "select * from tb_server_list where server_name = 'redis'"

31.注册服务的方法 WPS_Service.exe \RegServer(\unregserver反注册);WPS_Service.exe -service把程序变成服务,等待后台运行或你自己手动启动。

32.要查看或更改一个工程里一个.h文件或.cpp文件文件的查找路径:先选择要更改路径的文件,然后-》视图-》其它窗口-》属性窗口,然后更改 File和FullName。

33.VxD:虚拟设备驱动程序。通常控制真正的硬件设备并对该设备在各虚拟机之间的共享进行管理;另外如果你需要做一些在第0级才能做的工作,你就可以编写一个VxD程序来完成你的工作,这样,由于VxD没有虚拟任何设备,你就可以把VxD当成仅仅是你程序的扩展。

34.Dependency Walker 即是 Depends,使用可以见我转载的一篇博客,官网:http://www.dependencywalker.com/

35.查看服务的方法:  我地电脑-》右键-》服务  或   我的电脑-》右键-》管理-》服务和应用程序-》服务   或   开始-》管理工具-》服务。

36.GetExitCodeThread可以获取线程的结束的代码。

37.首先判断以字符串的结尾是否是单引号',如果是则删除之。

strKWRT是string类型

if (!strKWRT.empty())
 {
  if ('\'' == strKWRT[strKWRT.size() - 1])
  {
   strKWRT.erase(--strKWRT.end()) ;
  }
 }

如果strKWRT是CStdString(我们公司自己写的),要写成如下形式

if (!strKWRT.empty())
 {
  if ('\'' == strKWRT.c_str()[strKWRT.size() - 1])
  {
   strKWRT.erase(--strKWRT.end()) ;
  }
 }

 38.比较人工重置事件对象和自动重置时间对象:

当人工重置事件对象得到通知时,等待该事件对象的所有线程均变为可调度线程;当一个自动重置的事件对象得到通知时,等待该事件对象的线程中只有一个线程变成可调度的线程,同事操作系统会将该事件对象设置为无信号状态,这样,当对所保护的代码执行完成后,需要调用SetEvent函数将该事件对象设置为有信号状态。而人工重置的事件对象,在一个线程得到该事件对象之后,操作系统并不会将该事件对象设置为无信号状态,除非显式地调用ResetEvent函数将其设置为无信号状态,否则该对象会一直是有信号状态。

39.*.ncb is the appwizard used to show the method or attribute of the class or struct. del it and when you open the project again vc++ will make a new *.ncb file for you.
NCB是“No Compile Browser”的缩写,其中存放了供ClassView、WizardBar和Component Gallery使用的信息(记录了你所用过的联想记录),由VC开发环境自动生成。有时候,会因一些原因丢失联想功能(比如工程拷贝时VC会生成新的信息以适应新的环境变量,有时就造成函数变量等不能联想),这时可以删除NCB文件,因为该文件在重新打开工程时VC会自动重建。

40.被比尔盖茨称最为推崇的五位程序员:

Nightwish 
Sabine Edelsbacher
Tanja Turunen 
Edenbridge
Ray Ozzie 

41.Xerces是一个开放源代码的XML语法分析器。从JDK1.5以后,Xerces就成了JDK的XML默认实现

  Xerces-C++ 的前身是 IBM 的 XML4C 项目。XML4C 和 XML4J 是两个并列的项目,而 XML4J 是 Xerces-J——Java 实现——的前身。IBM 将这两个项目的源代码让与 Apache 软件基金会(Apache Software Foundation),他们将其分别改名为 Xerces-C++ 和 Xerces-J。注:“Xerces-C”和“Xerces-C++”是同一个东西。 

42.#define CAT3_RAW_NUMERIC_SEP_UNDERSCORE(a, b, c) a ## _ ## b ## _ ## c中的##表示连接前后两个符号。

43.看看下面写的bat内容:

echo off
set str1="%~dp"
rem 相对sln路径
set FName=ADArea_Filter\ADArea_Filter.sln
set NPath="%str1%%FName%"

devenv  %NPath% /Rebuild "Release|Win32"

解释:REM 是个注释命令一般是用来给程序加上注解的,该命令后的内容在程序执行的时候将不会被显示和执行。例:
REM 你现在看到的就是注解,这一句将不会被执行。

ECHO 是一个回显命令主要参数有OFF和 ON,一般用ECHO message来显示一个特定的消息 。例:
Echo off
Rem 以上代表关闭回显即不显示所执行的命令
Echo 这个就是消息。
Rem 以上代表显示“这就是消息”这列字符
执行结果:
C:\>ECHO.BAT
这个就是消息。

当前系统目录是%windir%或%SystemRoot%

44.
void CADFilter::InitModulePath(HINSTANCE hModule)
{
string::size_type pos;
char cArySrvPath[MAX_PATH] = {0};
GetModuleFileName(hModule, cArySrvPath, MAX_PATH);
m_strDLLPath = cArySrvPath;
pos = m_strDLLPath.rfind('\\');
m_strDLLPath = m_strDLLPath.substr(0, pos+1);
}

45.string类型的数据可以提前预留一部分的空间:strIniCfgFile.reserve(150);

46.在string的后面加上字符串除了用“+”,也可以:strIniCfgFile.append(cstrINIFileName);

47.看数字如何转化为枚举类型: m_stIniCfgData.stLogCfgInfo.enTaskLogMode = (GENERATE_MODE)atoi(strTmp.c_str());

48.string的erase函数如果内部没有参数的话,表示从头删到尾。

49.为了用 tr1:shared_ptr 需要函数头文件 #include <memory>

50.对于sscanf的用法,看如下就可以了:

void CADFilter::ChangeIPFormat(const string& strDotIP, string& strNumIP)
{
 if (!strDotIP.empty())
 {
  int nFirst = 0;
  int nSecond = 0;
  int nThird = 0;
  int nForth = 0;
  UINT32 un32IP = 0;
  char aryIPVal[20] = {0};
  ::sscanf(strDotIP.c_str(), "%d.%d.%d.%d", &nFirst, &nSecond, &nThird, &nForth); 
  un32IP = (UINT)((nFirst << 24) + (nSecond << 16) + (nThird << 8) + nForth);
  sprintf_s(aryIPVal, "%u", un32IP);
  strNumIP = aryIPVal;
 } 
}

51.判断一个文件是否存在的方法:

if( -1 == _access( TxtPath.c_str() ,0) )// 0 表示判断一个文件是否存在
 {
  return false;
 }

55.c代码,读取文件的一行:

FILE *pFileObj = fopen(strProCityMapFile.c_str(),"r" );
    while( NULL != fgets(aryLine, 100, pFileObj) )

56.有的时候在Freelibrary释放资源的时候,如果之前有启动线程,最好在资源释放之前将线程结束掉,因为Freelibrary将会把dll里的所有资源都析构掉,但是线程可能还没有结束。

57.pProvCityIDMap->insert( map<int,vector< int > >::value_type(nProvinceID, tmpCitySet ));

58.在关闭前要关闭线程

void CDBProxy::a_Close()
{

 m_bThreadClose = FALSE ;
 if(m_hThread)
 {
  WaitForSingleObject(m_hThread, INFINITE);
 }

 CNodeStore::instance()->Close();

}

 

59.二分算法的实现:

int CADFilter::FindAreaID(_int64 nIPVal, CIPTableVct *pIPTable)
{
 //查找ip相对应区域的id值,先查找市级IP,映射得到省级IP
 int nAreaID = 0;
 long low  = 0;
 long high = pIPTable->size()-1;
 long mid  = ( low + high )/2;

 // 不在IP段表中,认为区域ID为0
 if( (nIPVal<(*pIPTable)[0].IpFrom ) || (nIPVal>(*pIPTable)[high].IpTo))
  nAreaID = 0;
 else
 {
  while( low <= high )
  {
   //查找城市ip段;
   if( (*pIPTable)[mid].IpFrom > nIPVal )
    high = mid-1 ;
   else if( (*pIPTable)[mid].IpTo < nIPVal )
    low = mid+1 ;
   else
   {
    nAreaID = (*pIPTable)[mid].idnum;
    break;
   }
   mid = (low +high)/2;
  }
 }
 return nAreaID;

}

60.销毁线程参考

// 线程是否空闲
bool CWebThread::IsIdle(void)
{
 DWORD dwExitCode = 0;
 if(m_hThread == NULL)
 {
  return true;
 }

 if(GetExitCodeThread(m_hThread, &dwExitCode))
 {
  if(dwExitCode == STILL_ACTIVE)
  {
   return false;
  }
 }
 return true;
}

// 强制分析完成
void CWebThread::ForceAnalyzeDone(ANALYZE_URL_INFO *pAUrlInfo)
{
 if(m_hThread)
 {
  DWORD dwExitCode;
  g_Log.GetLog()->WriteDBGLog(IMP_RECORD, "CWebThread::ForceAnalyzeDone", "开始强制结束线程");
  TerminateThread(m_hThread, dwExitCode);
  g_Log.GetLog()->WriteDBGLog(IMP_RECORD, "CWebThread::ForceAnalyzeDone", "强制结束线程完成");
 }

 if(pAUrlInfo)
 {
  g_Log.GetLog()->WriteDBGLog(WHOLE_RECORD, "CWebThread::ForceAnalyzeDone", "开始插入线程处理的网页分析结果");
 // InsertDB(pAUrlInfo, "", "", 0, 5);
  pAUrlInfo->nAnalyzeStatus = 1;
  g_Log.GetLog()->WriteDBGLog(WHOLE_RECORD, "CWebThread::ForceAnalyzeDone", "插入线程处理的网页分析结果完成");
 }
}

 

void CAnalyzeUrl::DestroyWebThread(void)
{
 for(int i = 0; i < m_nWebThreadCount; i ++)
 {
  if(m_arWebThread[i])
  {
   if(!m_arWebThread[i]->IsIdle())
   {
    m_arWebThread[i]->ForceAnalyzeDone(NULL);
   }

   delete m_arWebThread[i];
   m_arWebThread[i]= NULL;
  }
 }

 std::vector<CWebThread *> arWebThread1;
 arWebThread1.swap(m_arWebThread);
 arWebThread1.clear();

 for(int i = 0; i < m_nEPWebThreadCount; i ++)
 {
  if(m_arEPWebThread[i])
  {
   if(!m_arEPWebThread[i]->IsIdle())
   {
    m_arEPWebThread[i]->ForceAnalyzeDone(NULL);
   }

   delete m_arEPWebThread[i];
   m_arEPWebThread[i]= NULL;
  }
 }

 std::vector<CWebThread *> arWebThread2;
 arWebThread2.swap(m_arEPWebThread);
 arWebThread2.clear();
}

 61.读取文件,写入文件参考

ifstream   myFile1 ;
  string    strTemp1 ;

  CStdString   strPreLog;
  strPreLog.Format("threadid:%d:", GetCurrentThreadId());

  CStdString strModulePathName ;
  if(!GetModulePathName(strModulePathName))
  {
   g_Log.GetLog()->WriteDBGLog(IMP_RECORD, "CWebPageAnalyze::GetArtical", strPreLog + "获取当前模块路径失败");
   return false;
  }

  CStdString strUrl = strModulePathName + "url.txt" ;
  myFile1.open(strUrl.c_str()) ;
  ofstream   myFileOut1;
  CStdString strExtractContent = strModulePathName + "ExtractContent.txt" ;
  myFileOut1.open(strExtractContent.c_str()) ;
  strTemp1.clear() ;
  while (getline(myFile1, strTemp1))
  {
   TestAnalyze.GetArtical(strTemp1, strMyTitle, strMyContent) ;
   myFileOut1<<strTemp1.c_str()<<endl<<strMyTitle.c_str()<<"##"<<endl<<strMyContent.c_str()<<endl<<"####"<<endl ;
   strTemp1.clear() ;
   strMyTitle.clear() ;
   strMyContent.clear() ;
  }

  myFile1.close() ;
  myFile1.clear() ;
  myFileOut1.close() ;
  myFileOut1.clear() ;
  getch() ;

62.boost库的使用:

首先从boost库的官方网站上下载最新的boost库,下的时候要注意了,要下载对应的操作系统的版本,下下来后进行解压,然后编译,编译好后,要在你的工程中关联对应的头文件目录和lib库的目录,注意lib库的目录是\stage\lib

对于正则匹配:

然后:#include <boost/regex.hpp>  

#ifdef _DEBUG
#pragma comment(lib, "libboost_regex-vc100-mt-gd-1_47.lib")
#else
#pragma comment(lib, "libboost_regex-vc100-mt-1_47.lib")
#endif

63.string::size_type md5pos  = msgString.rfind("\t")  ;

64.关于C++explicit的说明:

explicit
C++   Specific  

This   keyword   is   a   declaration   specifier   that   can   only   be   applied   to   in-class   constructor   declarations.   Constructors   declared   explicit   will   not   be   considered   for   implicit   conversions.   For   example:

class   X   {
public:
      explicit   X(int);             //legal
      explicit   X(double)   {       //legal
            //   ...
      }
};

explicit   X::X(int)   {}             //illegal
An   explicit   constructor   cannot   take   part   in   implicit   conversions.   It   can   only   be   used   to   explicitly   construct   an   object.   For   example,   with   the   class   declared   above:

void   f(X)   {}
void   g(int   I)   {
      f(i);             //   will   cause   error
}
void   h()   {
      X   x1(1);             //   legal
}
The   function   call   f(i)   fails   because   there   is   no   available   implicit   conversion   from   int   to   X.

Note       It   is   meaningless   to   apply   explicit   to   constructors   with   multiple   arguments,   since   such   constructors   cannot   take   part   in   implicit   conversions.

END   C++   Specific
65.对于string类型,如果时间以YYYYMMDDHHMMSS来表示的话,可以直接用<比较大小。真的非常好。

66.if (0 == NodeName2.compare("tech"))
     {
     }

67.火狐浏览器是完全开放源代码的。

68.对于定义默认的形参值,如果声明中已经指定了,则不能在定义中再次声明。

69.

string p("love") ;
CStdString pp ;
 pp.Format("I %s You", p.c_str()) ;// 注:这里不能写成pp.Format("I %s You", p) ;得到的将不是我们想要的结果
int i = 0 ;

70.//IP地址十进制
#define M_IP2UINT(mA, mB, mC, mD) ((DWORD)((mA << 24) + (mB << 16) + (mC << 8) + mD))
const TCHAR* const g_cszIPFmt = _T("%d.%d.%d.%d");

71.继承隐藏和覆盖:

覆盖:behavior depends solely on type of the object

隐藏:behavior depends on type of the pointer

隐藏存在的道理:隐藏可以把基类中的同名函数(覆盖的函数除外)给隐藏了,也就是派生类的指针不能调用那些函数,同时基类的指针也调用不了派生类的函数,这样可以避免提前在编译的时候发现某些错误:如派生类指针如果调用派生类的函数给错了参数,该参数不符合派生类的函数,但是符合基类的函数,这时会出错,因为此时调用不了基类的函数。另一个原因是:假如类 Derived 有多个基类(多重继承),有时搞不清楚哪些基类定义了函数f。如
果没有隐藏规则,那么pd->f(10)可能会调用一个出乎意料的基类函数f。

72.for (i = 0 ; i < 9 ; i++ )
      cout << setw(10) << va2 [ i ]
         << "  radians, which is  "
         << setw(11) << (180/pi) * va2 [ i ]
         << "  degrees" << endl;

3.14159  radians, which is          180  degrees
   2.41886  radians, which is       138.59  degrees
    2.0944  radians, which is          120  degrees
   1.82348  radians, which is      104.478  degrees
    1.5708  radians, which is           90  degrees
   1.31812  radians, which is      75.5225  degrees
    1.0472  radians, which is           60  degrees
  0.722734  radians, which is      41.4096  degrees
         0  radians, which is            0  degrees

73.string& 做形参时可以在函数内定义与不变量赋给它,函数返回时不会出现乱码:

void  GetIniVal(const char* szAppName,const char* szKey,string& strKeyVal)
{
char szIniFileName[MAX_PATH] = {0};
char szIniFilePath[MAX_PATH] = {0};
char szKeyVal[MAX_PATH] = {0};


GetModuleFileName( g_hModule , szIniFilePath , MAX_PATH );
getFolderAndFileName(szIniFilePath,szIniFileName,NULL);
strcat(szIniFileName,"MobileDev_TSA_Filter.ini");

GetPrivateProfileString(szAppName,szKey,"",szKeyVal,MAX_PATH,szIniFileName);
strKeyVal = szKeyVal;
}

74.等待输入的C++写法:

using namespace std ;

cin.get();
75.

BYTE(unsigned char) 和 char 是不可以默认转的,必须要强转一下。

76.%20 空格   %2c 逗号

77.函数名和变量名不支持 中划线,可用下划线代替

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值