代码如下,有两个函数无法理解,
CString sPath;
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
int nPos;
nPos=sPath.ReverseFind('\\');
sPath=sPath.Left(nPos);
CString lpszFile=sPath + "\\study.mdb";
——————————————————————————
问题:
查了一下MSDN,对于
GetModuleFileName(NULL,sPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
sPath.ReleaseBuffer();
这两个函数无法理解,GetModuleFileName是获得文件路径并放到sPath中吧?
sPath.ReleaseBuffer()应该是释放sPath的缓存区吧?可下面怎么还在使用sPath呢?
不解?
谢谢帮忙:)

解决方案 »

  1.   

    ReleaseBuffer 是因为前面调用了 GetBufferSetLength
      

  2.   

    MSDN的原话ResIf you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString methods.The address returned by GetBufferSetLength may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. If you do not change the length of the Cstring, the buffer will not be reassigned.The buffer memory will be freed automatically when the CString object is destroyed.Note that, if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character when you call ReleaseBuffer, you should pass –1 (the default) for the length. ReleaseBuffer will perform a _tcslen on the buffer to determine its length.