Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ Shell使用特定的工作目录执行cmd提示符_C++_Windows_Visual C++_Cmd - Fatal编程技术网

C++ Shell使用特定的工作目录执行cmd提示符

C++ Shell使用特定的工作目录执行cmd提示符,c++,windows,visual-c++,cmd,C++,Windows,Visual C++,Cmd,我想在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

我想在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
。我做错了什么?

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


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

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

//-----------------------------------------------------------------------
    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”是批处理文件,存储在文件夹:path executable module+“driverKey64”中,此示例的驱动程序存储在该文件夹中,并从该文件夹中批处理执行,不会出错。。 在windows 64位7、8、10中测试,xp类似于32位。。。编码MFC++


看待

你做错了什么主要是在C++中这样做。只需创建一个快捷方式。@cheers-sandhth.-Alf它不能提供我想要的功能。我可以在.NET中做到这一点没问题,我认为
cmd
支持这种无关的语言“不提供功能”是胡说八道。但是无论如何,对于参数(当前为空),您可以添加例如
“/kcd\”+path+“\”
@cheers-sandhth.-Alf谢谢!