【问题标题】:ShellExecute a cmd prompt with a specific working directoryShell 使用特定工作目录执行 cmd 提示符
【发布时间】:2016-06-25 23:20:28
【问题描述】:

我想在 Windows 中将提升的命令提示符启动到特定的工作目录。例如,我试过这个:

ShellExecute(
    hWnd,
    L"runas",
    L"cmd.exe",
    NULL,
    m_szSelectedFile,
    SW_SHOW
);

在哪里m_szSelectedFile = L"C:\\Users\\User\\Desktop"

ShellExecute 记录为

HINSTANCE ShellExecute(
  _In_opt_ HWND    hwnd,
  _In_opt_ LPCTSTR lpOperation,
  _In_     LPCTSTR lpFile,
  _In_opt_ LPCTSTR lpParameters,
  _In_opt_ LPCTSTR lpDirectory,
  _In_     INT     nShowCmd
);

不幸的是,它总是启动到C:\WINDOWS\system32。我做错了什么?

【问题讨论】:

  • 你做错的主要是在C++中做这个。只需创建一个快捷方式。
  • @Cheersandhth.-Alf 那没有提供我想要的功能。我可以在.NET中做到这一点没问题,我认为cmd支持这种无关的语言
  • “不提供功能”是无稽之谈。但无论如何,对于参数(当前为 NULL),您可以添加例如"/k cd \" + path + "\""
  • @Cheersandhth.-Alf 谢谢!

标签: c++ windows visual-c++ cmd


【解决方案1】:

Microsoft 从 Windows 8 开始将此作为一项安全功能添加。每当 cmd.exe 检测到它以提升的方式运行时,它都会忽略其启动参数并始终以 %SystemRoot%\System32 启动。您不能覆盖此行为。

但是,您可以将目录更改为提示符中的第一个命令。为此,请照常将lpFile 设置为"cmd.exe"。然后将lpParameters 设置为"/k cd /d d:\your\path"。 CMD 将在启动时立即更改目录,然后保持打开状态以执行更多命令。

【讨论】:

    【解决方案2】:

    我测试了解决方案,但任何一个都不起作用,但最后我搜索了一个解决方案:

    //-----------------------------------------------------------------------
        TCHAR szPath[_MAX_PATH];
        VERIFY(::GetModuleFileName(AfxGetApp()->m_hInstance, szPath, _MAX_PATH));
        CString csPath(szPath);
        CString csParameter;
        int nIndex = csPath.ReverseFind(_T('\\'));
        if (nIndex > 0)
        {
            csPath = csPath.Left(nIndex);
        }
        else
        {
            csPath.Empty();
        }
    
        if (IsWow64())
        {
            m_StatusText += "The process is running under Windows 64\r\n";
            UpdateData(false);
            csPath += "\\driverKey64";
            csParameter += "/c install.cmd";
            if (ShellExec(csParameter, csPath, true))
            {
                m_StatusText += "Drivers Installed\r\n";
            }
            else
            {
                m_StatusText += "Drivers were not Installed\r\n";
            }
            UpdateData(false);
        }
    
    int CDriverKeyDlg::ShellExec(LPCTSTR lpApplicationName,LPCTSTR lpDirectory, bool bWait)
    {
        int iReturn = -1;
        SHELLEXECUTEINFO shExInfo = { 0 };
        shExInfo.cbSize = sizeof(shExInfo);
        shExInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
        shExInfo.hwnd = 0;
        shExInfo.lpVerb = _T("runas");          // Operation to perform
        shExInfo.lpFile = _T("cmd.exe");        // Application to start    
        shExInfo.lpParameters = lpApplicationName;    // Additional parameters
        shExInfo.lpDirectory = lpDirectory;
        shExInfo.nShow = SW_SHOW;
        shExInfo.hInstApp = 0;
    
        if (ShellExecuteEx(&shExInfo))
        {
            if (bWait)
            {
                WaitForSingleObject(shExInfo.hProcess, INFINITE);
            }
            iReturn = 1;
        }
        CloseHandle(shExInfo.hProcess);
        return iReturn;
    }
    
    //-----------------------------------------------------------------------
    

    文件“install.cmd”是批处理文件,存储在文件夹:路径可执行模块+“driverKey64”,本示例的驱动程序存储在此文件夹中,并从该文件夹批量执行没有错误.. 在 windows 64 位 7,8,10,xp 中的测试与 32 位类似 ... 编码 MFC C++

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-16
      • 1970-01-01
      相关资源
      最近更新 更多