Window 注册程序关联后缀文件,双击运行
一般来说,很多软件都会有自定义后缀的文件,比如.cpp、.doc等,那么如果我们想把这些后缀与我们的软件关联起来,如何做呢
#pragma once #include "StdAfx.h" class CRegExtension
{
public:
CRegExtension();
~CRegExtension();
void SetExtension( LPCTSTR szExtension );
void SetShellOpenCommand( LPCTSTR szShellOpenCommand );
void SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand );
void SetDocumentClassName( LPCTSTR szDocumentClassName );
void SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon );
BOOL SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData);
BOOL RegSetExtension(void);
BOOL RegSetDocumentType(void);
BOOL RegSetAllInfo(void);
void RegisterFileAndProgram(); private:
CStdString m_csExtension;
CStdString m_csShellOpenCommand;
CStdString m_csDocumentShellOpenCommand;
CStdString m_csDocumentClassName;
CStdString m_csDocumentDefaultIcon;
CStdString m_csDocumentDescription;
};
#include "StdAfx.h"
#include "RegExtension.h" CRegExtension::CRegExtension()
{ } CRegExtension::~CRegExtension()
{ } void CRegExtension::SetExtension( LPCTSTR szExtension )
{
m_csExtension = szExtension;
}
void CRegExtension::SetShellOpenCommand( LPCTSTR szShellOpenCommand )
{
m_csShellOpenCommand = szShellOpenCommand;
}
void CRegExtension::SetDocumentShellOpenCommand( LPCTSTR szDocumentShellOpenCommand )
{
m_csDocumentShellOpenCommand = szDocumentShellOpenCommand;
}
void CRegExtension::SetDocumentClassName( LPCTSTR szDocumentClassName )
{
m_csDocumentClassName = szDocumentClassName;
}
void CRegExtension::SetDocumentDefaultIcon( LPCTSTR szDocumentDefaultIcon )
{
m_csDocumentDefaultIcon = szDocumentDefaultIcon;
} BOOL CRegExtension::SetRegistryValue(HKEY hOpenKey, LPCTSTR szKey, LPCTSTR szValue, LPCTSTR szData
){
// validate input
if( !hOpenKey || !szKey || !szKey[] ||
!szValue || !szData ){
::SetLastError(E_INVALIDARG);
return FALSE;
}
BOOL bRetVal = FALSE;
DWORD dwDisposition;
DWORD dwReserved = ;
HKEY hTempKey = (HKEY);
// length specifier is in bytes, and some TCHAR
// are more than 1 byte each
DWORD dwBufferLength = lstrlen(szData) * sizeof(TCHAR);
// Open key of interest
// Assume all access is okay and that all keys will be stored to file
// Utilize the default security attributes
if( ERROR_SUCCESS == ::RegCreateKeyEx(hOpenKey, szKey, dwReserved,
(LPTSTR), REG_OPTION_NON_VOLATILE, KEY_SET_VALUE, ,
&hTempKey, &dwDisposition) ){ // dwBufferLength must include size of terminating nul
// character when using REG_SZ with RegSetValueEx function
dwBufferLength += sizeof(TCHAR); if( ERROR_SUCCESS == ::RegSetValueEx(hTempKey, (LPTSTR)szValue,
dwReserved, REG_SZ, (LPBYTE)szData, dwBufferLength) ){
bRetVal = TRUE;
}
}
// close opened key
if( hTempKey ){
::RegCloseKey(hTempKey);
}
return bRetVal;
} BOOL CRegExtension::RegSetExtension(void)
{
if( m_csExtension.IsEmpty() )
{
return FALSE;
}
CStdString csKey = CStdString(_T(".")) + m_csExtension;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentClassName.GetData());
if( !m_csShellOpenCommand.IsEmpty() )
{
csKey += _T("\\shell\\open\\command");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
}
return TRUE;
} BOOL CRegExtension::RegSetDocumentType(void)
{
if( m_csDocumentClassName.IsEmpty())
{
return FALSE;
}
CStdString csKey = m_csDocumentClassName;
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDescription.GetData());
// DefaultIcon
if( !m_csDocumentDefaultIcon.IsEmpty() )
{
csKey = m_csDocumentClassName;
csKey += _T("\\DefaultIcon");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csDocumentDefaultIcon.GetData());
}
// shell\open\command
if( !m_csShellOpenCommand.IsEmpty() )
{
csKey = m_csDocumentClassName;
csKey += _T("\\shell\\open\\command");
SetRegistryValue(HKEY_CLASSES_ROOT, csKey.GetData(), _T(""), m_csShellOpenCommand.GetData());
}
return TRUE;
} BOOL CRegExtension::RegSetAllInfo(void)
{
RegSetExtension();
RegSetDocumentType();
return TRUE;
} void CRegExtension::RegisterFileAndProgram()
{
////一个应用程序与多个文件后缀关联////
#define strExternsionLength 1
LPCTSTR strExtension[] =
{
_T("myproc")
};
//CGCFileTypeAccess TheFTA;
TCHAR szProgPath[MAX_PATH * ];
//获取程序路径
::GetModuleFileName(NULL, szProgPath, sizeof(szProgPath)/sizeof(TCHAR));
CStdString csTempText;
for(int i = ; i < strExternsionLength; ++i)
{
//设置程序需要关联的后缀名,如"txt" "doc" 等
SetExtension(strExtension[i]);
csTempText.Format(_T("\"%s\" %s"),szProgPath,_T("\"%1\""));
SetShellOpenCommand(csTempText.GetData());
SetDocumentShellOpenCommand(csTempText.GetData());
//设置注册表中文件类的别名,例如可以是程序名称:MyProc.exe
SetDocumentClassName(_T("MyProc")); // use first icon in program
csTempText = szProgPath;
csTempText += _T(",0");
SetDocumentDefaultIcon(csTempText.GetData());
RegSetAllInfo();
}
}
调用:
一般是在初始化HINSTANCE 的地方:
如bool CWndShadow::Initialize(HINSTANCE hInstance)
我这里使用的是CWndShadow类,这是一个开源的阴影类,可以google一下
//注册程序与文件后缀名的关联
CRegExtension reg;
reg.RegisterFileAndProgram();
// 分析标准外壳命令、DDE、打开文件操作的命令行
if (__argc > )
{
m_csFilePath = __targv[];//我使用这种方式获取双击的后缀文件的所在路径
}
这样就可以双击运行软件了
Window 注册程序关联后缀文件,双击运行的更多相关文章
- WIN7/XP用注册表关联指定后缀名和打开程序(手动【图文】和C编程两种实现)
前言: 本文是基本原理介绍和手动的操作.程序实现该功能在http://blog.csdn.net/arvon2012/article/details/7839556,同时里面有完整代码的下载. 今天在 ...
- win10家庭版,双击bat文件无法运行(double click bat file does not execute)
win10家庭版,双击bat文件无法运行,弹出文件打开方式选择框. 在网上搜索处理办法,试了以下方法1-5都没有成功,用方法6规避. 方法1:打开一个驱动器,点“工具-文件夹选项→文件类型→新建→扩展 ...
- 把 Python 脚本打包成可以直接双击运行的 .exe 文件 【转】
因为最近要用到 Python 脚本,所以自己学习了一下,顺便学习如何把它打包成 .exe 可执行文件,达到双击运行的效果,网上找了资料,保存下来学习用,原文出处:https://baijiahao.b ...
- 在windows中:双击运行Python程序、后台运行Python程序
在windows中:双击运行Python程序.后台运行Python程序 安装Python解释器的windows环境,如果双击运行*.py的文件,会闪退.怎样避免闪退呢? 我们用python的日志输出程 ...
- Win7不能用鼠标双击运行jar文件怎么办?
Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件,其跨平台特性使得很多工具软件都用jar方式来部署分发,比如用于H ...
- Windows不能用鼠标双击运行jar文件
Java应用程序jar文件可以由 JVM(Java虚拟机)直接执行,只要操作系统安装了JVM便可以运行作为Java应用程序的jar文件.可是,很多朋友遇到一个难题,那就是下载了jar文件以后在Wind ...
- 直接双击运行PowerShell的脚本文件
原来的不支持有空格的路径,由dugu的批处理中找到方法了,利用windows路径的另外的另外表达方式即可,".\路径" 这个东西用的不多啊,使用这个格式后powershell就能不 ...
- bat文件无法双击运行
问题: win7系统下新建txt文件,编辑脚本内容后,保存为test.bat.每次双击它,只会默认以txt格式打开它,而不是运行它. 解决: 1. 双击打开“我的电脑”,然后在“工具”下选择“文件夹选 ...
- centos/ubuntu 双击运行 .sh(shell)文件
centos 创建桌面双击启动程序(更改图标) - Feythin Lau - 博客园http://www.cnblogs.com/feiyuliu/archive/2012/11/29/279503 ...
随机推荐
- Design and Analysis of Algorithms_Brute Froce
I collect and make up this pseudocode from the book: <<Introduction to the Design and Analysis ...
- 点餐系统3个sprint的团队贡献分
第一次冲刺贡献分 团员名字 贡献分 麦锦俊 18分 冯婉莹 21分 李康梅 19分 张鑫相 20分 曹嘉琪 22分 第二次冲刺贡献分 团员名字 贡献分 麦锦俊 19分 冯婉莹 20分 ...
- C/C++ 结构体 数组 函数传递
#include <stdio.h> #include <stdlib.h> struct student{ int num; ]; double dec; }; void s ...
- 目标检测方法——SSD
SSD论文阅读(Wei Liu--[ECCV2016]SSD Single Shot MultiBox Detector) 目录 作者及相关链接 文章的选择原因 方法概括 方法细节 相关背景补充 实验 ...
- Matlab基本函数-conj函数
Matlab基本函数-conj函数 1.conj函数:用于计算复数的共轭值 2.用法说明:y=conj(x)函数计算复数x的共轭值.输出结果y的维数跟输入x的维数一致,返回值为:real(y)-i*i ...
- 【原创】如何用Android Studio断点安卓自带Service或Bind类型的Service
很久以来,我一直想找一种方法来断点调试安卓系统自身的Service,或者bind类型的Service,比如我想看WifiManager里面的getWifiApConfiguration函数是如何实现的 ...
- owin建控制台应用程序步骤
1. Install-Package Microsoft.AspNet.WebApi.OwinSelfHost 2. 建立OWIN Startup类 public void Configurati ...
- spring 笔记1: mvn 中Controller方法的参数不能是嵌套类(内部类)。
最近做spring开发,个人认为,Controller和客户端js通讯时传递的参数类 只使用某几个方法,为了减少对其他功能的影响,想把参数类定义为Controller类的 嵌套类(内部类).但是实践发 ...
- 简单谈谈eclipse下搭建PhoneGap环境来开发Android程序 - linux86(转)
原来在逛园子的时候一不小心发现了一个新概念“PhoneGap”简称PG,我一直都喜欢追逐新事物,自然就产生了好奇心.于是乎我就在百度上面Google了一下PhoneGap是什么东西.简单的说就是用另一 ...
- Python—sqlalchemy
SQLAlchemy SQLAlchemy是Python编程语言下的一款ORM框架,该框架建立在数据库API之上,使用关系对象映射进行数据库操作. #Dialect用于和数据API进行交流,根据配置文 ...