2019年7月

CSharedCriticalSection 实现临界区的访问:加锁、去锁
CSharedMutex 实现临时窃取临界区对象,然后加锁,对象销毁时又会去锁,然后归还对象

classCSharedCriticalSection {public:
CSharedCriticalSection() {
InitializeCriticalSection(
&critical_section_);
}
~CSharedCriticalSection() {
DeleteCriticalSection(
&critical_section_);
}
void lock() {
EnterCriticalSection(
&critical_section_);
}
voidunlock() {
LeaveCriticalSection(
&critical_section_);
}
private:
CRITICAL_SECTION critical_section_;
};
classCSharedMutex {public:explicit CSharedMutex(CSharedCriticalSection&critical_section)
: is_lock_(
false)
, critical_section_(critical_section)
{
if(is_lock_ == false) {
is_lock_
= true;
critical_section_.
lock();
}
}
~CSharedMutex(){if(is_lock_) {
is_lock_
= false;
critical_section_.unlock();
}
}
void lock() {if(is_lock_ == false) {
is_lock_
= true;
critical_section_.
lock();
}
}
voidunlock() {if(is_lock_) {
is_lock_
= false;
critical_section_.unlock();
}
}
protected:
CSharedMutex(
const CSharedMutex&rhs)
: critical_section_(rhs.critical_section_)
{

}

CSharedMutex
& operator=(const CSharedMutex&rhs) {return *this;
}
private:volatile boolis_lock_;
CSharedCriticalSection
&critical_section_;
};

 

beyond compare 4.2.9桌面右键集成的问题修复

安装后,发现在WIN64时,注册表中注册的DLL库有问题

出错处:

[HKEY_CLASSES_ROOT\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx.dll"
"ThreadingModel"="Apartment"

修改为:

[HKEY_CLASSES_ROOT\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx64.dll"
"ThreadingModel"="Apartment"

如果还不行,注意去这儿删除相应的{57FA2D12-D22D-490A-805A-5CB48E84F12A}信息

[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Cached]

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Cached]

修复右键的完整的注册表项见下,注意修改为你电脑上BC4的安装位置(目录需要双斜杠)

Windows Registry Editor Version 5.00[HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CLASSES_ROOT\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_CLASSES_ROOT\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx64.dll" "ThreadingModel"="Apartment"[HKEY_CLASSES_ROOT\Directory\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CLASSES_ROOT\Folder\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CLASSES_ROOT\lnkfile\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_CLASSES_ROOT\WOW6432Node\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx.dll" "ThreadingModel"="Apartment"[HKEY_CURRENT_USER\Software\Classes\*\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CURRENT_USER\Software\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_CURRENT_USER\Software\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx64.dll" "ThreadingModel"="Apartment"[HKEY_CURRENT_USER\Software\Classes\Directory\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CURRENT_USER\Software\Classes\Folder\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CURRENT_USER\Software\Classes\lnkfile\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved]"{57FA2D12-D22D-490A-805A-5CB48E84F12A}"="Beyond Compare 4 Shell Extension"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\*\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx64.dll" "ThreadingModel"="Apartment"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Directory\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Folder\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\lnkfile\shellex\ContextMenuHandlers\CirrusShellEx]
@
="{57FA2D12-D22D-490A-805A-5CB48E84F12A}"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\WOW6432Node\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\WOW6432Node\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx.dll" "ThreadingModel"="Apartment"[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved]"{57FA2D12-D22D-490A-805A-5CB48E84F12A}"="Beyond Compare 4 Shell Extension"[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}]
@
="CirrusShellEx"[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Classes\CLSID\{57FA2D12-D22D-490A-805A-5CB48E84F12A}\InProcServer32]
@
="D:\\05.Tools\\BeyondCompare4.2.9\\BCShellEx.dll" "ThreadingModel"="Apartment"[HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Shell Extensions\Approved]"{57FA2D12-D22D-490A-805A-5CB48E84F12A}"="Beyond Compare 4 Shell Extension"

 

递归创建目录1:

需要#include <shlwapi.h>,并且配置好shlwapi.ib

BOOL CreateDirTree( LPCTSTR lpPath )
{
if( NULL == lpPath || _tcslen(lpPath)==0)
{
returnFALSE;
}
if( ::PathFileExists( lpPath) ||::PathIsRoot(lpPath) )returnTRUE;

TCHAR szParentpath[MAX_PATH]
= _T("");
::lstrcpy( szParentpath, lpPath );

::PathRemoveBackslash( szParentpath );
//去除路径最后的反斜杠 ::PathRemoveFileSpec( szParentpath );//将路径末尾的文件名或文件夹和反斜杠去掉 if(0 ==_tcscmp(lpPath, szParentpath))returnFALSE;

assert(
0 !=_tcscmp(lpPath, szParentpath));if( CreateDirTree( szParentpath) )//递归创建直到上一层存在或是根目录 {return::CreateDirectory(lpPath, NULL);
}
else{returnFALSE;
}
returnTRUE;
}

递归创建目录2:

void __fastcall RecursiveDirectory(CString cstrDir) //递归创建目录
{if (cstrDir.GetLength() <= 3)//是根目录,无需创建目录
{return;
}
if (cstrDir[cstrDir.GetLength()-1] == '\\') //将路径改为目录 {
cstrDir.Delete(cstrDir.GetLength()
-1, 1);
}
//修改文件属性 WIN32_FIND_DATA wfd;
HANDLE hFind
= FindFirstFile(cstrDir, &wfd); //查找 if (hFind !=INVALID_HANDLE_VALUE)
{
FindClose(hFind);
if (wfd.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY)return;
}
//创建当前目录的地目录失败 if (CreateDirectory(cstrDir,NULL) == false)
{
//退到上一级目录 CString wstrNewDir =cstrDir;int n = wstrNewDir.ReverseFind('\\');
wstrNewDir
=cstrDir.Left(n);//递归进入 RecursiveDirectory(wstrNewDir); //递归本函数,再创建目录//递归退出后创建之前失败的目录 CreateDirectory(cstrDir,NULL); //递归返回,在存在的目录上再建目录 }//多级目录创建成功 }

递归创建目录3:

BOOL RecursiveDirectory(wstring wstrDir)
{
if (wstrDir.length() <= 3)
{
returnFALSE;
}
if (wstrDir[wstrDir.length() - 1] == '\\')
{
wstrDir.erase(wstrDir.end()
- 1);
}
if(PathFileExists(wstrDir.c_str()))returnTRUE;if (CreateDirectory(wstrDir.c_str(), NULL) == false)
{
wstring wstrNewDir
=wstrDir;while (wstrNewDir[wstrNewDir.length() - 1] != '\\')
{
wstrNewDir.erase(wstrNewDir.length()
- 1);
}
//delete '\\' wstrNewDir.erase(wstrNewDir.length() - 1);

RecursiveDirectory(wstrNewDir);
CreateDirectory(wstrDir.c_str(), NULL);
}
if (!PathFileExists(wstrDir.c_str()))returnFALSE;returnTRUE;
}

 递归创建目录4:

bool createDirectory(const char*pathName)
{
charpath[MAX_PATH];
memset(path,
0x00, MAX_PATH);const char* pos =pathName;while ((pos = strchr(pos, '\\')) !=NULL)
{
memcpy(path, pathName, pos
- pathName + 1);
pos
++;if (_access(path, 0) == 0)
{
continue;
}
else{int ret =_mkdir(path);if (ret == -1)
{
return false;
}
}
}
return true;
}

 

 

递归删除目录1:

system("rmdir /s /q dirname");  //dirname是要删除的目录名称,这种方式,在使用MFC程序的时候出闪过一个CMD的窗口
/s是级联删除  /q 是不提示(在命令行下操作的话,如果不加这个开关,会有提示确认是否删除目录,而在程序中不允许停下)

递归删除目录2:

SHFILEOPSTRUCT FileOp;
FileOp.fFlags
=FOF_NOCONFIRMATION;
FileOp.hNameMappings
=NULL;
FileOp.hwnd
=NULL;
FileOp.lpszProgressTitle
=NULL;
FileOp.pFrom
= ".\\tempDir";
FileOp.pTo
=NULL;
FileOp.wFunc
=FO_DELETE;
SHFileOperation(
&FileOp);

此处有一个地方要留心一下,就是FileOp.pFrom这个参数,它使用的字符串一定是要'\0'结尾的,这个地方使用".\\tempDir",这个字符串默认的结束字符就是'\0',所以如果存在这个目录或者文件的话,一定可以将其删除,如果像下面这样写的话就会出错:

std::string delPath = ".\\tempDir";

FileOp.pFrom = delPath.c_str();  // 此时字符串没有以'\0'结尾,所以删除的时候会出错

递归删除目录3:

bool deleteDirectory( char*pathName)
{
struct_finddata_t fData;
memset(
&fData, 0, sizeof(fData));if (_chdir(pathName) != 0) //_chdir函数设置当前目录 {
printf(
"chdir failed: %s\n",pathName);return false;
}

intptr_t hFile
= _findfirst("*",&fData); //参数1:char *类型,"*"表示通配符,可以查找文件、文件夹 if(hFile == -1)
{
printf(
"_findfirst error!\n");return false;
}
do{if(fData.name[0] == '.')continue;if(fData.attrib == _A_SUBDIR) //子文件夹 {chardirPath[MAX_PATH];
memset(dirPath,
0,sizeof(pathName));
strcpy_s(dirPath,pathName);
strcat_s(dirPath,
"\\");
strcat_s(dirPath,fData.name);

deleteDirectory(dirPath);
//recursion subdir printf("remove dir: %s\n",dirPath);
_chdir(
"..");
_rmdir(dirPath);
}
else{charfilePath[MAX_PATH];
memset(filePath,
0,sizeof(filePath));
strcpy_s(filePath,pathName);
strcat_s(filePath,
"\\");
strcat_s(filePath,fData.name);

remove(filePath);
printf(
"remove file: %s\n",filePath);
}
}
while(_findnext(hFile,&fData) == 0);

_findclose(hFile);
//close return true;
}

 

代码参考了如下网页的实现:

https://www.cnblogs.com/zy791976083/p/9921069.html

整理成一个函数:

BOOL GetResGifSize(long nResId, LPCTSTR name, long *lnWidth, long *lnHeight)
{
HRSRC hRsrc
=FindResource(NULL, MAKEINTRESOURCE(nResId), name);if (NULL ==hRsrc) {returnFALSE;
}

DWORD dwSize
=SizeofResource(NULL, hRsrc);
HGLOBAL hGlobal
=LoadResource(NULL, hRsrc);if (NULL ==hGlobal) {
CloseHandle(hRsrc);
returnFALSE;
}

unsigned
char* pBuffer = (unsigned char*)LockResource(hGlobal);if (NULL ==pBuffer) {
CloseHandle(hRsrc);
FreeResource(hGlobal);
returnFALSE;
}
//判断是否为GIF文件 if(pBuffer[0] != 0x47 && pBuffer[1] != 0x49 && pBuffer[2] != 0x46 && pBuffer[3] != 0x38){returnFALSE;
}
//读取宽高 for(int i = 4; i < dwSize ; i++)
{
if(pBuffer[i] == 0x00 && pBuffer[i+1] == 0x2c)
{
*lnWidth = (pBuffer[i+7]<<8) | pBuffer[i+6];*lnHeight = (pBuffer[i+9]<<8) | pBuffer[i+8];
UnlockResource(hGlobal);
FreeResource(hGlobal);
returnTRUE;
}
}

UnlockResource(hGlobal);
FreeResource(hGlobal);
returnFALSE;
}

 

在关于对话框中,自动获取当前程序的版本,以显示

在CAboutDlg的OnInitDialog接口添加如下代码,将自动从当前程序EXE文件中读取版本信息,并显示在指定CStatic中

BOOL CAboutDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
//读取EXE文件所在路径 auto handle =GetModuleHandle(NULL);if (NULL !=handle) {//获得EXE文件路径 std::wstring install_dir_;
WCHAR dir[MAX_PATH]
= {0};
GetModuleFileName(handle, dir, MAX_PATH);

DWORD dwHandle;
DWORD dwDataSize
= ::GetFileVersionInfoSize(dir, &dwHandle);if (dwDataSize != 0) {//BYTE *strBuff = new BYTE[dwDataSize]; std::string strBuff(dwDataSize + 1, '\0');//读取文件的版本号信息 if (GetFileVersionInfo(dir, NULL, dwDataSize, &strBuff[0])){//获取文件版本号 UINT nQuerySize;
VS_FIXEDFILEINFO
* pVsffi =NULL;if ( ::VerQueryValue((void **)&strBuff[0], _T("\\"), (void**)&pVsffi, &nQuerySize) )
{
//获取到界面数据 CString strText;
m_file_version_.GetWindowText(strText);
//格式化输出 CString strResult;
strResult.Format(strText.GetBuffer(),
HIWORD(pVsffi
->dwFileVersionMS),
LOWORD(pVsffi
->dwFileVersionMS),
HIWORD(pVsffi
->dwFileVersionLS),
LOWORD(pVsffi
->dwFileVersionLS) );//重新设置下去 m_file_version_.SetWindowText(strResult);
}
}
}
}
return TRUE; //return TRUE unless you set the focus to a control//异常: OCX 属性页应返回 FALSE }