最終更新日: 1999年 03月 10日 水曜日
今回はCExtExecクラスを公開します。このクラスは、以前公開しましたReg32 (レジストリー操作クラス)を使い、拡張子情報をレジストリーから修得しアプリケーションを 決定しすことで、pathの通っていないアプリケーションに対して起動する情報を修得し利用 できるようにします。
インターネットのURLを指定しInternet ExplorerやNetscape
Navigatorなどで表示するための関数も準備しておきました。
この関数は、Internet Explorer 4.01SP1及びNetscape
Communicator 4.5 で動作確認を行いました。
通常アプリケーションの起動にはWinExecまたはCreateProcess関数で起動しますが、 データファイルの場合アプリケーションが判定できなければ起動できません。また、アプリケーション のインストール場所がユーザーによって異なる場合があるため、また、アプリケーションに環境変数 でPATHが指定されていない場合にアプリケーションが実行加納ファイル名のみでは実行できない ため、レジストリーに登録されている拡張子との関連付けを修得し、アプリケーションの絶対位置を 修得することで対応するようにしました。
#include "Reg32.h"
class CExtExec : public CObject{
private:
RegKey rk;
public:
CExtExec();
virtual ~CExtExec();
public:
LPCTSTR GetAppName(LPCTSTR szExtName); //
拡張子情報 (".htm"など)からアプリケーション情報を修得します
LPCTSTR GetAppDefstr(LPCTSTR szExtName); //
拡張子情報から、アプリケーション名(表示用)を修得します
UINT Exec(LPCTSTR filename,int nCmdShow=SW_NORMAL);
//
指定したデータファイルを拡張子情報からアプリケーションを決定し実行します
UINT ExecUrl(LPCTSTR url,int nCmdShow=SW_NORMAL);
// 指定したURLをブラウザーを起動し表示します
};
#include "CExtExec.h"
CExtExec::CExtExec(){
};
CExtExec::~CExtExec(){
};
UINT CExtExec::Exec(LPCTSTR filename,int nCmdShow){
char exec[_MAX_PATH*4],ext[128],*p;
ZeroMemory(exec,_MAX_PATH*4);
ZeroMemory(ext ,128);
strcpy(ext,strrchr(filename,'.'));
p=(LPSTR)GetAppName((LPCTSTR)ext); if(p==NULL) return
ERROR_FILE_NOT_FOUND;
wsprintf(exec,"%c%s%c
%c%s%c",0x22,p,0x22,0x22,filename,0x22);
return ::WinExec(exec,nCmdShow);
}
LPCTSTR CExtExec::GetAppDefstr(LPCTSTR szExtName){
static CString s; s="";
RegKey rk;
char sClsName[512];
ZeroMemory(sClsName,512);
static char buf[512];
ZeroMemory(buf,512);
if(rk.Open("HKEY_LOCAL_MACHINE\\Software\\CLASSES")==FALSE)return NULL;
rk.QueryKey((LPSTR)szExtName,(LPSTR)sClsName);
rk.QueryKey((LPSTR)sClsName,(LPSTR)buf);
rk.Close();
return (LPCTSTR)buf;
};
LPCTSTR CExtExec::GetAppName(LPCTSTR szExtName){ // ".htm" and other ext string
static CString s; s="";
RegKey rk;
char sClsName[512];
ZeroMemory(sClsName,512);
static char sCommand[512];
ZeroMemory(sCommand,512);
char buf[512];
if(rk.Open("HKEY_LOCAL_MACHINE\\Software\\CLASSES")==FALSE)return NULL;
rk.QueryKey((LPSTR)szExtName,(LPSTR)sClsName);
wsprintf(buf,"HKEY_LOCAL_MACHINE\\Software\\CLASSES\\%s\\shell\\open",sClsName);
rk.Close();
if(rk.Open(buf)==FALSE)return NULL;
rk.QueryKey((LPSTR)"command",(LPSTR)sCommand);
for(unsigned int i=0;i<strlen(sCommand);i++){
if(sCommand[i]=='"'){strcpy(sCommand+i,sCommand+i+1); i--;}
}
while(1){
if( sCommand[strlen(sCommand)-2]!='%') break;
sCommand[strlen(sCommand)-2]=0;
}
while(1){
if( sCommand[strlen(sCommand)-1]!=0x20) break;
sCommand[strlen(sCommand)-1]=0;
}
return LPCTSTR(sCommand);
};
UINT CExtExec::ExecUrl(LPCTSTR url,int nCmdShow){
char *p,com[_MAX_PATH*4];
p=(LPSTR)GetAppName(".htm");
if(p==NULL) return ERROR_FILE_NOT_FOUND;
char *r;
r=strrchr(p,'-'); if(r!=NULL)*r=0;
wsprintf(com,"%c%s%c %c%s%c",0x22,p,0x22,0x22,url,0x22);
return ::WinExec(com,nCmdShow);
};
#include <afxwin.h>
#ifndef _MAX_PATH
#include <stdlib.h>
#endif
/*
-------------------------------
class RegKey ----- Type 1 -----
RegKey *pRegKey;
CHAR szPath[_MAX_PATH];
pRegKey = new RegKey();
// create subkey
pRegKey->Open("HKEY_LOCAL_MACHINE\\Software");
pRegKey->CreateKey("kitaro");
pRegKey->CreateKey("Equip95");
pRegKey->SetValue("Sample Direct","Data-1");
pRegKey->Close();
//
pRegKey->Open("HKEY_LOCAL_MACHINE\\Software\\kitaro\\equip95");
pRegKey->SetValue("Sample","Data");
pRegKey->GetValue("Samlpe Direct", szPath);
pRegKey->Close();
delete(pRegKey);
-------------------------------
class RegKey ----- Type 2 -----
RegKey cRegKey;
HKEY hk;
cRegKey.Open("HKEY_LOCAL_MACHINE\\Software",&hk);
cRegKey.Close(hk);
*/
class RegKey : public CObject
{
private :
HKEY hk; // current key
public :
RegKey();
virtual ~RegKey();
public : // Initialize
BOOL Open (HKEY hKey,LPSTR
subName);
BOOL Open (LPSTR keyName);
//
HKEY_LOCAL_MACHINE\Software\...
void Close (void);
public :
HKEY HKey (void){return hk;};
public : // Current Key Value
BOOL GetValue (LPSTR
subKey,CHAR szPath[_MAX_PATH]);
BOOL GetValue (LPSTR
subKey,WCHAR wszPath[_MAX_PATH]);
BOOL SetValue (LPSTR
subKey,LPCSTR lpData);
BOOL SetValue (LPSTR
subKey,DWORD dwData);
BOOL SetValue (LPSTR
subKey,DWORD dwType,void* lpData,DWORD dwSize);
BOOL CreateKey (LPSTR
subKey); // create and change current to subKey
BOOL DeleteKey (LPSTR
subKey);
BOOL EnumKey (DWORD
dwIndex,LPSTR lpValueName, LPDWORD lpcbValueName,LPSTR lpData,LPDWORD lpType,PFILETIME
pFileTime);
BOOL EnumValue (DWORD
dwIndex,LPTSTR lpValueName, LPDWORD lpcbValueName, LPDWORD lpType, LPBYTE lpData, LPDWORD
lpcbData );
BOOL QueryKey (LPCTSTR
lpSubKey,LPDWORD lpType, LPBYTE lpData, LPDWORD lpcbData );
// Win16 API should not use this function
(Compatibility function)
BOOL EnumKey (DWORD
dwIndex,LPSTR lpValueName);
BOOL EnumKey (DWORD
dwIndex,LPSTR lpValueName,DWORD dwLength);
BOOL QueryKey (LPCTSTR
lpSubKey,LPTSTR lpValue);
BOOL QueryKey (LPCTSTR
lpSubKey,LPTSTR lpValue,PLONG lpcbValue);
BOOL SetValue16 (LPSTR
subKey,LPCSTR lpData);
BOOL SetValue16 (LPSTR
subKey,DWORD dwData);
BOOL SetValue16 (LPSTR
subKey,DWORD dwType,void* lpData,DWORD dwSize);
//--------------------------------------------------
// Function with OpenKey&Close in Local handle
public :
BOOL GetValue (HKEY
hKey,LPSTR subKey1,LPSTR subKey2,WCHAR szPath[_MAX_PATH]);
BOOL GetValue (HKEY
hKey,LPSTR subKey1,LPSTR subKey2,CHAR szPath[_MAX_PATH]);
BOOL SetValue (HKEY
hKey,LPSTR subKey1,LPSTR subKey2,LPCSTR lpData);
BOOL SetValue (HKEY
hKey,LPSTR subKey1,LPSTR subKey2,DWORD dwData);
BOOL CreateKey (HKEY
hKey,LPSTR subKey1,LPSTR subKey2);
BOOL DeleteKey (HKEY
hKey,LPSTR subKey1,LPSTR subKey2);
//--------------------------------------------------
// Function with HKEY
public :
BOOL Open
(LPSTR subKey,HKEY *phKey);
// HKEY_LOCAL_MACHINE\Software\...
BOOL Open
(HKEY hKey,LPSTR subName,HKEY *phKey);
void Close
(HKEY hKey);
BOOL DeleteKey (HKEY
hKey,LPSTR subKey);
BOOL CreateKey (HKEY
*phKey,LPSTR subKey);
BOOL CreateKey (HKEY
hKey,LPSTR subKey,HKEY *phNewKey);
BOOL CreateKey (HKEY
hKey,LPSTR subKey,HKEY *phNewKey,LPDWORD lpdwFlg);
// Win16 Api
BOOL EnumKey (HKEY hKey,DWORD
dwIndex,LPSTR lpValueName);
BOOL EnumKey (HKEY hKey,DWORD
dwIndex,LPSTR lpValueName,DWORD dwLength);
BOOL QueryKey (HKEY
hKey,LPCTSTR lpSubKey,LPTSTR lpValue);
BOOL QueryKey (HKEY
hKey,LPCTSTR lpSubKey,LPTSTR lpValue,PLONG lpcbValue);
};
// EOF
//--------------------------------------------------------------------
#include "Reg32.h"
RegKey::RegKey(){hk=NULL;};
RegKey::~RegKey(){Close();};
void RegKey::Close(void){
if(hk!=NULL){
::RegFlushKey(hk);
::RegCloseKey(hk); hk=NULL;
}
}
BOOL RegKey::Open(HKEY hKey,LPSTR subKey){
if(hk!=NULL){
::RegFlushKey(hk);
::RegCloseKey(hk); hk=NULL;
}
if(Open(hKey,subKey,&hk)==FALSE) { hk=NULL; return FALSE; }
return TRUE;
}
BOOL RegKey::Open(LPSTR subKey){
if(hk!=NULL){
::RegFlushKey(hk);
::RegCloseKey(hk); hk=NULL;
}
if(Open(subKey,&hk)==FALSE) { hk=NULL; return FALSE; }
return TRUE;
}
BOOL RegKey::GetValue(LPSTR subKey,WCHAR wszPath[_MAX_PATH]){
CHAR szPath[_MAX_PATH];
BOOL bReg;
bReg = GetValue(subKey,szPath);
mbstowcs(wszPath, szPath,_MAX_PATH);
return bReg;
}
BOOL RegKey::GetValue(LPSTR subKey,CHAR szPath[_MAX_PATH])
{
DWORD cbPath = _MAX_PATH;
DWORD pdwType;
szPath[0]=0;
if(hk==NULL)return FALSE; // not opened error
if(::RegQueryValueEx(hk,subKey,NULL,&pdwType,(LPBYTE)szPath,&cbPath)==ERROR_SUCCESS)
return TRUE;
return FALSE;
}
BOOL RegKey::SetValue(LPSTR subKey,LPCSTR lpData)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(SetValue(subKey,REG_SZ,(void*)lpData,strlen(lpData))==TRUE)bRet=TRUE;
return bRet;
}
BOOL RegKey::SetValue(LPSTR subKey,DWORD dwData)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(SetValue(subKey,REG_DWORD,&dwData,sizeof(dwData))==TRUE)bRet=TRUE;
return bRet;
}
BOOL RegKey::SetValue (LPSTR subKey,DWORD dwType,void* lpData,DWORD
dwSize){
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(::RegSetValueEx(hk,subKey,0, // reserved
dwType,(CONST
BYTE*)lpData,dwSize)==ERROR_SUCCESS)
bRet=TRUE;
return bRet;
}
BOOL RegKey::SetValue16(LPSTR subKey,LPCSTR lpData)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(SetValue16(subKey,REG_SZ,(void*)lpData,strlen(lpData))==TRUE)bRet=TRUE;
return bRet;
}
BOOL RegKey::SetValue16(LPSTR subKey,DWORD dwData)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(SetValue16(subKey,REG_DWORD,&dwData,sizeof(dwData))==TRUE)bRet=TRUE;
return bRet;
}
BOOL RegKey::SetValue16 (LPSTR subKey,DWORD dwType,void* lpData,DWORD
dwSize){
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(::RegSetValue(hk,subKey,
dwType,(LPSTR)lpData,dwSize)==ERROR_SUCCESS)
bRet=TRUE;
return bRet;
}
BOOL RegKey::CreateKey(LPSTR subKey)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
HKEY phk;
DWORD dw;
if(hk==NULL)return FALSE; // not opened error
if(::RegCreateKeyEx(hk,subKey,0, // reserved
"LPSTR",REG_OPTION_NON_VOLATILE ,
KEY_ALL_ACCESS ,NULL,&phk,&dw
)==ERROR_SUCCESS)bRet=TRUE;
if(bRet==TRUE) {
::RegCloseKey(hk);
hk=phk;
}
return bRet;
}
BOOL RegKey::DeleteKey(LPSTR subKey)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(::RegDeleteKey(hk,subKey)==ERROR_SUCCESS)bRet=TRUE;
return bRet;
}
BOOL RegKey::EnumKey(DWORD dwIndex,LPSTR lpValueName){
return(EnumKey(dwIndex,lpValueName,_MAX_PATH));
}
BOOL RegKey::EnumKey(DWORD dwIndex,LPSTR lpValueName, DWORD lpcbValueName){
BOOL bRet = FALSE;
if(hk==NULL)return FALSE; // not opened error
if(::RegEnumKey(hk,dwIndex,lpValueName,lpcbValueName)==ERROR_SUCCESS)bRet=TRUE;
return bRet;
}
BOOL RegKey::EnumKey(DWORD dwIndex,LPSTR lpValueName, LPDWORD lpcbValueName,LPSTR
lpData,LPDWORD lpType,PFILETIME pFileTime)
{
if(hk==NULL)return FALSE; // not opened error
if(::RegEnumKeyEx(hk,dwIndex,lpValueName,lpcbValueName,
NULL,lpData,lpType,pFileTime)==ERROR_SUCCESS)return TRUE;
else return FALSE;
}
BOOL RegKey::EnumValue(DWORD dwIndex,LPTSTR lpValueName, LPDWORD lpcbValueName, LPDWORD
lpType, LPBYTE lpData, LPDWORD lpcbData ){
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(hk==NULL)return FALSE; // not opened error
if(::RegEnumValue(HKEY_LOCAL_MACHINE,
dwIndex,lpValueName,
lpcbValueName,NULL,
lpType, lpData,
lpcbData)==ERROR_SUCCESS)bRet=TRUE;
return bRet;
}
BOOL RegKey::QueryKey (LPCTSTR lpSubKey,LPTSTR lpValue){
LONG lpcbValue = _MAX_PATH;
return(QueryKey(lpSubKey,lpValue, &lpcbValue ));
}
BOOL RegKey::QueryKey (LPCTSTR lpSubKey,LPTSTR lpValue,PLONG lpcbValue){
BOOL bRet = FALSE;
if(hk==NULL)return FALSE; // not opened error
if(::RegQueryValue(hk,lpSubKey,lpValue, lpcbValue )==ERROR_SUCCESS)
bRet=TRUE;
return bRet;
}
BOOL RegKey::QueryKey (LPCTSTR lpSubKey,LPDWORD lpType, LPBYTE lpData,
LPDWORD lpcbData )
{
if(hk==NULL)return FALSE; // not opened error
if(::RegQueryValueEx(hk,lpSubKey,0,
lpType, lpData, lpcbData )==ERROR_SUCCESS)
return(TRUE);
return FALSE;
}
//---------------------------------
// Function with OpenKey&Close in Local handle
BOOL RegKey::DeleteKey(HKEY hKey,LPSTR subKey1,LPSTR subKey2){
HKEY hk;
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(Open(hKey,subKey1,&hk)==FALSE) { return FALSE; }
if(::RegDeleteKey(hk,subKey1)==ERROR_SUCCESS)bRet=TRUE;
::RegCloseKey(hk);
return bRet;
}
BOOL RegKey::CreateKey(HKEY hKey,LPSTR subKey1,LPSTR subKey2)
{
HKEY hk;
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
HKEY phk;
DWORD dw;
if(Open(hKey,subKey1,&hk)==FALSE) { return FALSE; }
if(::RegCreateKeyEx(hk,subKey2,0, // reserved
"LPSTR",REG_OPTION_NON_VOLATILE ,
KEY_ALL_ACCESS ,NULL,&phk,&dw
)==ERROR_SUCCESS)bRet=TRUE;
if(bRet==TRUE) ::RegCloseKey(phk);
::RegCloseKey(hk);
return bRet;
}
BOOL RegKey::SetValue(HKEY hKey,LPSTR subKey1,LPSTR subKey2,LPCSTR lpData)
{
HKEY hk;
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(Open(hKey,subKey1,&hk)==FALSE) { return FALSE; }
if(::RegSetValueEx(hk,subKey2,0, // reserved
REG_SZ,(CONST
BYTE*)lpData,strlen(lpData))==ERROR_SUCCESS)bRet=TRUE;
::RegCloseKey(hk);
return bRet;
}
BOOL RegKey::SetValue(HKEY hKey,LPSTR subKey1,LPSTR subKey2,DWORD dwData)
{
HKEY hk;
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(Open(hKey,subKey1,&hk)==FALSE) { return FALSE; }
if(::RegSetValueEx(hk,subKey2,0, // reserved
REG_DWORD,(CONST
BYTE*)&dwData,sizeof(dwData))==ERROR_SUCCESS)bRet=TRUE;
::RegCloseKey(hk);
return bRet;
}
BOOL RegKey::GetValue(HKEY hKey,LPSTR subKey1,LPSTR subKey2,CHAR szPath[_MAX_PATH])
{
HKEY hk;
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(Open(hKey,subKey1,&hk)==FALSE) { return FALSE; }
if(::RegQueryValueEx(hk,subKey2,NULL,NULL,(LPBYTE)szPath,&cbPath)==ERROR_SUCCESS)return
TRUE;
::RegCloseKey(hk);
return bRet;
}
BOOL RegKey::GetValue(HKEY hKey,LPSTR subKey1,LPSTR subKey2,WCHAR wszPath[_MAX_PATH]){
CHAR szPath[_MAX_PATH];
BOOL bReg;
bReg = GetValue(hKey,subKey1,subKey2,szPath);
mbstowcs(wszPath, szPath,_MAX_PATH);
return bReg;
}
//---------------------------------
// Function with HKEY
BOOL RegKey::Open(LPSTR subKey,HKEY *phKey){
HKEY hKey;
char buf1[256],*p,*p2;
strcpy(buf1,subKey);
p=buf1;
p2=strchr(p,'\\'); *p2=0; p2++;
if(strcmpi(p,"HKEY_CLASSES_ROOT" )==0)
hKey=HKEY_CLASSES_ROOT;
if(strcmpi(p,"HKEY_CURRENT_USER" )==0)
hKey=HKEY_CURRENT_USER;
if(strcmpi(p,"HKEY_LOCAL_MACHINE" )==0)
hKey=HKEY_LOCAL_MACHINE;
if(strcmpi(p,"HKEY_USERS"
)==0) hKey=HKEY_USERS;
if(strcmpi(p,"HKEY_PERFORMANCE_DATA")==0)
hKey=HKEY_PERFORMANCE_DATA;
if(strcmpi(p,"HKEY_CURRENT_CONFIG" )==0)
hKey=HKEY_CURRENT_CONFIG;
if(strcmpi(p,"HKEY_DYN_DATA"
)==0) hKey=HKEY_DYN_DATA;
return Open(hKey,p2,phKey);
}
BOOL RegKey::Open(HKEY hKey,LPSTR subKey,HKEY *phKey){
if(::RegOpenKeyEx(hKey,subKey,0,KEY_ALL_ACCESS,phKey)!=ERROR_SUCCESS) {
hk=NULL; return FALSE; }
return TRUE;
}
BOOL DeleteKey (HKEY hKey,LPSTR subKey){
if(hKey!=NULL){
if(::RegDeleteKey(hKey,subKey)!=ERROR_SUCCESS)
{ return FALSE; }
else TRUE;
}
return FALSE;
}
void RegKey::Close(HKEY hk){
if(hk!=NULL){
::RegCloseKey(hk); hk=NULL;
}
}
BOOL RegKey::CreateKey (HKEY *phKey,LPSTR subKey){
DWORD dw;
HKEY hk;
BOOL b;
hk = *phKey;
b = CreateKey(hk,subKey,phKey,&dw);
if(b==TRUE)Close(hk);
return( b );
}
BOOL RegKey::CreateKey(HKEY hKey,LPSTR subKey,HKEY *phNewKey)
{
DWORD dw;
return(CreateKey(hKey,subKey,phNewKey,&dw));
}
BOOL RegKey::CreateKey(HKEY hKey,LPSTR subKey,HKEY *phNewKey,LPDWORD lpdwFlg)
{
BOOL bRet = FALSE;
DWORD cbPath = _MAX_PATH;
if(::RegCreateKeyEx(hKey,subKey,0,"LPSTR",REG_OPTION_NON_VOLATILE ,
KEY_ALL_ACCESS ,NULL,phNewKey,lpdwFlg
)==ERROR_SUCCESS)bRet=TRUE;
return bRet;
}
BOOL RegKey::EnumKey(HKEY hk,DWORD dwIndex,LPSTR lpValueName){
return(EnumKey(dwIndex,lpValueName,_MAX_PATH));
}
BOOL RegKey::EnumKey(HKEY hk,DWORD dwIndex,LPSTR lpValueName, DWORD lpcbValueName){
BOOL bRet = FALSE;
if(hk==NULL)return FALSE; // not opened error
if(::RegEnumKey(hk,dwIndex,lpValueName,lpcbValueName)==ERROR_SUCCESS)bRet=TRUE;
return bRet;
}
BOOL RegKey::QueryKey (HKEY hk,LPCTSTR lpSubKey,LPTSTR lpValue){
LONG lpcbValue = _MAX_PATH;
return(QueryKey(hk,lpSubKey,lpValue, &lpcbValue ));
}
BOOL RegKey::QueryKey (HKEY hk,LPCTSTR lpSubKey,LPTSTR lpValue,PLONG
lpcbValue){
BOOL bRet = FALSE;
if(hk==NULL)return FALSE; // not opened error
if(::RegQueryValue(hk,lpSubKey,lpValue, lpcbValue )==ERROR_SUCCESS)
bRet=TRUE;
return bRet;
}
// EOF
//--------------------------------------------------------------------
Copyright (C) Kitaro 1999