作为开发人员总是要时不时使用命令行,为此用Win+R热键输入cmd的方式,然后在dos窗体通过
CD命令一步步转到命令所在目录,这系列操作多了就有点繁琐了,有没有简单的方式呢?
网上搜了一把,有在注册表加入几项即可的办法,方法如下:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT/Directory/shell/cmd]
@="进入DOS"
[HKEY_CLASSES_ROOT/Directory/shell/cmd/command]
@="cmd.exe /k /"cd %L/""
[HKEY_CLASSES_ROOT/Drive/shell/cmd]
@="进入DOS"
[HKEY_CLASSES_ROOT/Drive/shell/cmd/command]
@="cmd.exe /k /"cd %L/""
把以上保存为reg文件,然后导入注册表中即可,刚开始用的挺欢快,省了很多事,但是用了一段时间后,还是不够爽,为何?
原来这个咚咚不能在我所浏览的当前目录的视图背景上让我进入dos,非要在目录上右键菜单才可以。
想了想看来图方便还不能图省事,只能写个简单的COM插件了,下面为插件执行效果,这下方便了。哈
这里好像不能上传代码,只好列出来了,主要代码如下:
atl工程
//
OpenDos.h : Declaration of the COpenDos
#ifndef __OPENDOS_H_
#define __OPENDOS_H_
#include " resource.h " // main symbols
/**/ /////
// COpenDos
class ATL_NO_VTABLE COpenDos :
public CComObjectRootEx < CComSingleThreadModel > ,
public CComCoClass < COpenDos, & CLSID_OpenDos > ,
public IOpenDos,
public IShellExtInit,
public IContextMenu
... {
public:
COpenDos()
...{
}
DECLARE_REGISTRY_RESOURCEID(IDR_OPENDOS)
DECLARE_NOT_AGGREGATABLE(COpenDos)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(COpenDos)
COM_INTERFACE_ENTRY(IOpenDos)
COM_INTERFACE_ENTRY(IShellExtInit)
COM_INTERFACE_ENTRY(IContextMenu)
END_COM_MAP()
// IOpenDos
public:
// IShellExtInit
STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);
// IContextMenu
STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT);
STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);
protected:
TCHAR m_szFile [MAX_PATH];
} ;
#endif // __OPENDOS_H_
/**/ //
// OpenDos.cpp : Implementation of COpenDos
#include " stdafx.h "
#include " DosHere.h "
#include " OpenDos.h "
/**/ /////
// COpenDos
/**/ /////
// COpenDos
STDMETHODIMP COpenDos::Initialize (
LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID )
... {
FORMATETC fmt = ...{ CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = ...{ TYMED_HGLOBAL };
HDROP hDrop;
if( !pDataObj )
...{
if( pidlFolder )
...{
TCHAR tcPath[ MAX_PATH ];
if( !SHGetPathFromIDList( pidlFolder , tcPath ))
...{
return E_INVALIDARG;
}
else
...{
lstrcpy(m_szFile, tcPath);
return S_OK;
}
}
else
...{
return E_INVALIDARG;
}
}
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
...{
// Nope! Return an "invalid argument" error back to Explorer.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
// Make sure it worked.
if ( NULL == hDrop )
return E_INVALIDARG;
// Sanity check - make sure there is at least one filename.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
HRESULT hr = S_OK;
if ( 0 == uNumFiles )
...{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
// Get the name of the first file and store it in our member variable m_szFile.
if ( 0 == DragQueryFile ( hDrop, 0, m_szFile, MAX_PATH ) )
hr = E_INVALIDARG;
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
STDMETHODIMP COpenDos::QueryContextMenu (
HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd,
UINT uidLastCmd, UINT uFlags )
... {
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd, _T("Open &Dos Here") );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 1 );
}
STDMETHODIMP COpenDos::GetCommandString (
UINT idCmd, UINT uFlags, UINT * pwReserved, LPSTR pszName, UINT cchMax )
... {
USES_CONVERSION;
// Check idCmd, it must be 0 since we have only one menu item.
if ( 0 != idCmd )
return E_INVALIDARG;
// If Explorer is asking for a help string, copy our string into the
// supplied buffer.
if ( uFlags & GCS_HELPTEXT )
...{
LPCTSTR szText = _T("Open dos console here.");
if ( uFlags & GCS_UNICODE )
...{
// We need to cast pszName to a Unicode string, and then use the
// Unicode string copy API.
lstrcpynW ( (LPWSTR) pszName, T2CW(szText), cchMax );
}
else
...{
// Use the ANSI string copy API to return the help string.
lstrcpynA ( pszName, T2CA(szText), cchMax );
}
return S_OK;
}
return E_INVALIDARG;
}
STDMETHODIMP COpenDos::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
... {
// If lpVerb really points to a string, ignore this function call and bail out.
if ( 0 != HIWORD( pCmdInfo->lpVerb ) )
return E_INVALIDARG;
// Get the command index - the only valid one is 0.
switch ( LOWORD( pCmdInfo->lpVerb) )
...{
case 0:
...{
TCHAR szMsg [MAX_PATH + 32];
wsprintf ( szMsg, _T("cmd.exe /k "%c: && cd %s""), m_szFile[0],m_szFile );
//MessageBox ( pCmdInfo->hwnd, szMsg, _T("SimpleShlExt"),
// MB_ICONINFORMATION );
WinExec(szMsg,SW_SHOW);
return S_OK;
}
break;
default:
return E_INVALIDARG;
break;
}
}
/**/ ////
// DosHere.cpp : Implementation of DLL Exports.
// Note: Proxy/Stub Information
// To build a separate proxy/stub DLL,
// run nmake -f DosHereps.mk in the project directory.
#include " stdafx.h "
#include " resource.h "
#include < initguid.h >
#include " DosHere.h "
#include " DosHere_i.c "
#include " OpenDos.h "
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_OpenDos, COpenDos)
END_OBJECT_MAP()
/**/ /////
// DLL Entry Point
extern " C "
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /**/ /*lpReserved*/ )
... {
if (dwReason == DLL_PROCESS_ATTACH)
...{
_Module.Init(ObjectMap, hInstance, &LIBID_DOSHERELib);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}
/**/ /////
// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow( void )
... {
return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}
/**/ /////
// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
... {
return _Module.GetClassObject(rclsid, riid, ppv);
}
/**/ /////
// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer( void )
... {
// registers object, typelib and all interfaces in typelib
return _Module.RegisterServer(FALSE););//注意改为FALSE了
}
/**/ /////
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer( void )
... {
return _Module.UnregisterServer(FALSE);//注意改为FALSE了
}
rgs文件内容:
HKCR
... {
NoRemove CLSID
...{
ForceRemove ...{F617AA3A-D9A7-4B09-AF59-6E036732FCD8} = s 'InterFaceCls Class'
...{
InprocServer32 = s '%MODULE%'
...{
val ThreadingModel = s 'Apartment'
}
}
}
NoRemove Directory
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Folder
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Drive
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Directory
...{
NoRemove Background
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
}
}
#ifndef __OPENDOS_H_
#define __OPENDOS_H_
#include " resource.h " // main symbols
/**/ /////
// COpenDos
class ATL_NO_VTABLE COpenDos :
public CComObjectRootEx < CComSingleThreadModel > ,
public CComCoClass < COpenDos, & CLSID_OpenDos > ,
public IOpenDos,
public IShellExtInit,
public IContextMenu
... {
public:
COpenDos()
...{
}
DECLARE_REGISTRY_RESOURCEID(IDR_OPENDOS)
DECLARE_NOT_AGGREGATABLE(COpenDos)
DECLARE_PROTECT_FINAL_CONSTRUCT()
BEGIN_COM_MAP(COpenDos)
COM_INTERFACE_ENTRY(IOpenDos)
COM_INTERFACE_ENTRY(IShellExtInit)
COM_INTERFACE_ENTRY(IContextMenu)
END_COM_MAP()
// IOpenDos
public:
// IShellExtInit
STDMETHODIMP Initialize(LPCITEMIDLIST, LPDATAOBJECT, HKEY);
// IContextMenu
STDMETHODIMP GetCommandString(UINT, UINT, UINT*, LPSTR, UINT);
STDMETHODIMP InvokeCommand(LPCMINVOKECOMMANDINFO);
STDMETHODIMP QueryContextMenu(HMENU, UINT, UINT, UINT, UINT);
protected:
TCHAR m_szFile [MAX_PATH];
} ;
#endif // __OPENDOS_H_
/**/ //
// OpenDos.cpp : Implementation of COpenDos
#include " stdafx.h "
#include " DosHere.h "
#include " OpenDos.h "
/**/ /////
// COpenDos
/**/ /////
// COpenDos
STDMETHODIMP COpenDos::Initialize (
LPCITEMIDLIST pidlFolder, LPDATAOBJECT pDataObj, HKEY hProgID )
... {
FORMATETC fmt = ...{ CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
STGMEDIUM stg = ...{ TYMED_HGLOBAL };
HDROP hDrop;
if( !pDataObj )
...{
if( pidlFolder )
...{
TCHAR tcPath[ MAX_PATH ];
if( !SHGetPathFromIDList( pidlFolder , tcPath ))
...{
return E_INVALIDARG;
}
else
...{
lstrcpy(m_szFile, tcPath);
return S_OK;
}
}
else
...{
return E_INVALIDARG;
}
}
// Look for CF_HDROP data in the data object.
if ( FAILED( pDataObj->GetData ( &fmt, &stg ) ))
...{
// Nope! Return an "invalid argument" error back to Explorer.
return E_INVALIDARG;
}
// Get a pointer to the actual data.
hDrop = (HDROP) GlobalLock ( stg.hGlobal );
// Make sure it worked.
if ( NULL == hDrop )
return E_INVALIDARG;
// Sanity check - make sure there is at least one filename.
UINT uNumFiles = DragQueryFile ( hDrop, 0xFFFFFFFF, NULL, 0 );
HRESULT hr = S_OK;
if ( 0 == uNumFiles )
...{
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return E_INVALIDARG;
}
// Get the name of the first file and store it in our member variable m_szFile.
if ( 0 == DragQueryFile ( hDrop, 0, m_szFile, MAX_PATH ) )
hr = E_INVALIDARG;
GlobalUnlock ( stg.hGlobal );
ReleaseStgMedium ( &stg );
return hr;
}
STDMETHODIMP COpenDos::QueryContextMenu (
HMENU hmenu, UINT uMenuIndex, UINT uidFirstCmd,
UINT uidLastCmd, UINT uFlags )
... {
// If the flags include CMF_DEFAULTONLY then we shouldn't do anything.
if ( uFlags & CMF_DEFAULTONLY )
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 0 );
InsertMenu ( hmenu, uMenuIndex, MF_BYPOSITION, uidFirstCmd, _T("Open &Dos Here") );
return MAKE_HRESULT ( SEVERITY_SUCCESS, FACILITY_NULL, 1 );
}
STDMETHODIMP COpenDos::GetCommandString (
UINT idCmd, UINT uFlags, UINT * pwReserved, LPSTR pszName, UINT cchMax )
... {
USES_CONVERSION;
// Check idCmd, it must be 0 since we have only one menu item.
if ( 0 != idCmd )
return E_INVALIDARG;
// If Explorer is asking for a help string, copy our string into the
// supplied buffer.
if ( uFlags & GCS_HELPTEXT )
...{
LPCTSTR szText = _T("Open dos console here.");
if ( uFlags & GCS_UNICODE )
...{
// We need to cast pszName to a Unicode string, and then use the
// Unicode string copy API.
lstrcpynW ( (LPWSTR) pszName, T2CW(szText), cchMax );
}
else
...{
// Use the ANSI string copy API to return the help string.
lstrcpynA ( pszName, T2CA(szText), cchMax );
}
return S_OK;
}
return E_INVALIDARG;
}
STDMETHODIMP COpenDos::InvokeCommand ( LPCMINVOKECOMMANDINFO pCmdInfo )
... {
// If lpVerb really points to a string, ignore this function call and bail out.
if ( 0 != HIWORD( pCmdInfo->lpVerb ) )
return E_INVALIDARG;
// Get the command index - the only valid one is 0.
switch ( LOWORD( pCmdInfo->lpVerb) )
...{
case 0:
...{
TCHAR szMsg [MAX_PATH + 32];
wsprintf ( szMsg, _T("cmd.exe /k "%c: && cd %s""), m_szFile[0],m_szFile );
//MessageBox ( pCmdInfo->hwnd, szMsg, _T("SimpleShlExt"),
// MB_ICONINFORMATION );
WinExec(szMsg,SW_SHOW);
return S_OK;
}
break;
default:
return E_INVALIDARG;
break;
}
}
/**/ ////
// DosHere.cpp : Implementation of DLL Exports.
// Note: Proxy/Stub Information
// To build a separate proxy/stub DLL,
// run nmake -f DosHereps.mk in the project directory.
#include " stdafx.h "
#include " resource.h "
#include < initguid.h >
#include " DosHere.h "
#include " DosHere_i.c "
#include " OpenDos.h "
CComModule _Module;
BEGIN_OBJECT_MAP(ObjectMap)
OBJECT_ENTRY(CLSID_OpenDos, COpenDos)
END_OBJECT_MAP()
/**/ /////
// DLL Entry Point
extern " C "
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /**/ /*lpReserved*/ )
... {
if (dwReason == DLL_PROCESS_ATTACH)
...{
_Module.Init(ObjectMap, hInstance, &LIBID_DOSHERELib);
DisableThreadLibraryCalls(hInstance);
}
else if (dwReason == DLL_PROCESS_DETACH)
_Module.Term();
return TRUE; // ok
}
/**/ /////
// Used to determine whether the DLL can be unloaded by OLE
STDAPI DllCanUnloadNow( void )
... {
return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
}
/**/ /////
// Returns a class factory to create an object of the requested type
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
... {
return _Module.GetClassObject(rclsid, riid, ppv);
}
/**/ /////
// DllRegisterServer - Adds entries to the system registry
STDAPI DllRegisterServer( void )
... {
// registers object, typelib and all interfaces in typelib
return _Module.RegisterServer(FALSE););//注意改为FALSE了
}
/**/ /////
// DllUnregisterServer - Removes entries from the system registry
STDAPI DllUnregisterServer( void )
... {
return _Module.UnregisterServer(FALSE);//注意改为FALSE了
}
rgs文件内容:
HKCR
... {
NoRemove CLSID
...{
ForceRemove ...{F617AA3A-D9A7-4B09-AF59-6E036732FCD8} = s 'InterFaceCls Class'
...{
InprocServer32 = s '%MODULE%'
...{
val ThreadingModel = s 'Apartment'
}
}
}
NoRemove Directory
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Folder
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Drive
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
NoRemove Directory
...{
NoRemove Background
...{
NoRemove shellex
...{
NoRemove ContextMenuHandlers
...{
ForceRemove OpenDosHere = s '{F617AA3A-D9A7-4B09-AF59-6E036732FCD8}'
}
}
}
}
}