当前位置: 编程技术>综合
本页文章导读:
▪用自己指定的模板创建ahk脚本 在windows右键弹出菜单的新建菜单中加入“AutoHotkey 脚本”
1.首先写好模板文件,随便保存在一个地方,比如我是“X:\AutoHotkey\AutoHotkey\SHELLNEW\Template.ahk”;
2.打开注册表(regedit),找到 [HKE.........
▪遍历当前目录下所有的.h文件,并将其路径保存到文件中 void CFindAllFilesInDirDlg::OnBnClickedOk()
{
CString strCurrentPath;
GetModuleFileName(AfxGetInstanceHandle(),strCurrentPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
int iPos = strCurrentPath.ReverseFind('\\');
strCurrentPath = strCurrentPath.Lef.........
▪怎么有效的防止内存泄漏 首先说说标题可能取得有些大,但是可以理解为编程过程中有效的防止写的代码中有内存泄漏。好了废话不多说了,首先看下面一段代码。
......
[1]用自己指定的模板创建ahk脚本
来源: 互联网 发布时间: 2013-11-07
在windows右键弹出菜单的新建菜单中加入“AutoHotkey 脚本”
1.首先写好模板文件,随便保存在一个地方,比如我是“X:\AutoHotkey\AutoHotkey\SHELLNEW\Template.ahk”;
2.打开注册表(regedit),找到 [HKEY_CLASSES_ROOT] -> [.ahk] (没有的话,自己新建项.ahk);
3.在 [.ahk] 下新建项 [ShellNew] (已经有的话就删掉重建);
4.在 [ShellNew] 下新建 字符串值 ,名称为 FileName ,键值为模板文件的绝对路径,比如我的是 X:\AutoHotkey\AutoHotkey\SHELLNEW\Template.ahk ;
好啦,在右键->新建菜单中就会出现"AutoHotkey 脚本"项,新建以后还会自动将模板内容复制过来,是不是很爽?
附我的注册表导出文件:
Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\.ahk\ShellNew] "FileName"="X:\\AutoHotkey\\AutoHotkey\\SHELLNEW\\Template.ahk"
Template.ahk文件:
/* AutoHotkey 版本: 操作系统: Windows XP/Vista/7 作者: sunwind <1576157@qq.com> 博客: http://blog.csdn.net/liuyukuan 脚本说明: 脚本版本: v1.0 */ #NoEnv #SingleInstance SendMode Input SetWorkingDir %A_ScriptDir% ; [ALT]+[R]: 重启程序 !r:: Reload ; 重启 -- DEBUG: optional Return ; [ALT]+[ESC]: 退出程序 !ESC:: Suspend ; exempt from suspension -- DEBUG: optional ExitApp
作者:liuyukuan 发表于2013-1-6 22:56:24 原文链接
阅读:71 评论:0 查看评论
[2]遍历当前目录下所有的.h文件,并将其路径保存到文件中
来源: 互联网 发布时间: 2013-11-07
void CFindAllFilesInDirDlg::OnBnClickedOk()
{
CString strCurrentPath;
GetModuleFileName(AfxGetInstanceHandle(),strCurrentPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
int iPos = strCurrentPath.ReverseFind('\\');
strCurrentPath = strCurrentPath.Left(iPos);
CFile file;
file.Open(L"h.txt",CFile::modeCreate | CFile::modeWrite);
Recurse(file, strCurrentPath);
file.Close();
CString strPath("D:");
}
void CFindAllFilesInDirDlg::Recurse(CFile &file, LPCTSTR pstr)
{
CFileFind finder;
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
BOOL bWorking = finder.FindFile(strWildcard);
bWorking=finder.FindNextFile();
CString temp=finder.GetFilePath();
if (finder.IsDots())
continue;
if(finder.IsDirectory())
{
CString str = finder.GetFilePath();
Recurse(file,str);
}
if(temp.Right(2) == ".h")
{
temp += L"\r\n";
file.Write(temp,temp.GetLength()*sizeof(TCHAR));
}
}
{
CString strCurrentPath;
GetModuleFileName(AfxGetInstanceHandle(),strCurrentPath.GetBufferSetLength(MAX_PATH+1),MAX_PATH);
int iPos = strCurrentPath.ReverseFind('\\');
strCurrentPath = strCurrentPath.Left(iPos);
CFile file;
file.Open(L"h.txt",CFile::modeCreate | CFile::modeWrite);
Recurse(file, strCurrentPath);
file.Close();
CString strPath("D:");
}
void CFindAllFilesInDirDlg::Recurse(CFile &file, LPCTSTR pstr)
{
CFileFind finder;
CString strWildcard(pstr);
strWildcard += _T("\\*.*");
BOOL bWorking = finder.FindFile(strWildcard);
while(bWorking)
{bWorking=finder.FindNextFile();
CString temp=finder.GetFilePath();
if (finder.IsDots())
continue;
if(finder.IsDirectory())
{
CString str = finder.GetFilePath();
Recurse(file,str);
}
if(temp.Right(2) == ".h")
{
temp += L"\r\n";
file.Write(temp,temp.GetLength()*sizeof(TCHAR));
}
}
finder.Close();
}
** 源程序 免积分下载地址: http://download.csdn.net/detail/moonshine99/4967281
作者:moonshine99 发表于2013-1-6 22:32:20 原文链接
阅读:67 评论:0 查看评论
[3]怎么有效的防止内存泄漏
来源: 互联网 发布时间: 2013-11-07
首先说说标题可能取得有些大,但是可以理解为编程过程中有效的防止写的代码中有内存泄漏。好了废话不多说了,首先看下面一段代码。
class Image
{
public:
Image(const std::string& imgFileName);
...
}
class Voice
{
public:
Voice(const std::string& vFileName);
...
}
class People
{
public:
People(const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName);
~People( );
private:
string name;
int age;
int height;
Image *pImg; //图像
Voice *pVoi; //声音
};
People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
:name(n),age(a),height(h),pImg(0),pVoi(0)
{
if(imgFileName != "")
{
pImg = new Image( imgFileName ) ;
}
if(vFileName != " ")
{
pVoi = new Voice ( vFileName );
}
}
People::~People( )
{
delete pImg;
delete pVoi;
}上面代码粗略看似没有问题,但是有没有想到如果People构造函数出错(内存不足,无法分配内存)怎么办?其结果可以预见,就是一个异常抛出来。但是我们仔细想想此时如果已经构造了Image类对象,而在构造Voice类对象时抛出的错误,这个情况会怎么办?程序会因为异常而停止,后面代码不会执行,那么pImg指针所指向的内存就不会得到正确的释放,那么内存就泄漏了。情况如下面代码:...
People *p =NULL;
try{
p=new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat");
...
catch( ... ){
delete p;
throw;
}
delete p;
}
仔细想想 new People("test",20,170,".../images/image01.jpg","../voices/voice01.dat")里,如果最后为Image分配的内存被丢失,因为new操作没有成功完成,程序不会p进行赋值操作。所以catch中是没有任何操作的,已被分配的内存就丢失了。
因为对象在构造中抛出异常后C++不负责清除对象,所以我们需要重新设计构造函数让它们在运到异常的时候自己能清除所占用的内存。
People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
:name(n),age(a),height(h),pImg(0),pVoi(0)
{
try {
if(imgFileName != "")
{
pImg = new Image( imgFileName ) ;
}
if(vFileName != " ")
{
pVoi = new Voice ( vFileName );
}
}
catch( ...) {
delete pImg ;
delete pVoi;
throw ;
}这样就行了,解决上面的情况。让成员变量成为const指针,这样设计也合理,避免指针无意被改变。 Image * const pImg; //图像
Voice * const pVoi; //声音那么这样就只能用成员初始化列表为指针初始化,就没有其他地方可以给const指针赋值了。People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
:name(n),age(a),height(h),
pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),
pVoi( vFileName != "" ? new Voice( vFileName ) : 0)
{}如果这样就重新回到上面所遇到的问题,即构造过程中抛出异常,指针可能无法正确的释放所占内存。那么我们可以进一步对代码进行改进,如下:People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
:name(n),age(a),height(h),
pImg( initImage( imgFileName ) ),
pVoi( initVoice( vFileName ) )
{}
Image* People::initImage(const string& imgFileName)
{
if(imgFileName !="") return new Image(imgFileName);
else return 0;
}
Voice* People::initVoice(const string& vFileName)
{
try
{
if(vFileName !="")return new Voice(vFileName)
esle return 0;
}
catch(... )
{
delete pImg;
throw;
}
}这样在调用构建Voice对象中加入try...catch...用于释放pImg所占用的内存空间。其实有一个比其更简单的方法就是使用智能指针。const auto_ptr<Image> pImg;
const auto_ptr<Voice> pVoi;
People::People( const std::string& n,const int& a,const int& h,const std::stirng& imgFileName,const std::string& vFileName )
:name(n),age(a),height(h),
pImg( imgFileName !="" ? new Image( imgFileName ) : 0 ),
pVoi( vFileName != "" ? new Voice( vFileName ) : 0)
{}那么问题就算解决了,因为当其中有一个创建失败,离开函数的时候,智能指针会自动删除已经创建的空间,防止内存泄漏了。
作者:couhujia 发表于2013-1-6 22:29:35 原文链接
阅读:85 评论:0 查看评论
最新技术文章: