的ShellExecute bat文件升高(FMX的Win32)

问题描述 投票:-2回答:2

我想从我的FMX应用程序(在Win32)使用提升的权限生成一个批处理文件。从雷米的在上ShellExecute的底部of this thread答案我发现如何启动该批处理文件。现在,我无法弄清楚如何使用提升的权限启动它。下面是我的代码:

String Prog = "c:\\Users\\rwp\\Desktop\\test.bat";
int nErrorCode = (int) ShellExecute(NULL, L"runas", Prog.c_str(), NULL, NULL, SW_SHOWNORMAL);
if (nErrorCode <= 32) {
 ShowMessage("an error occured");
}

我补充说:“运行方式”的第二个参数读数this无果后。手动运行该批处理文件(右键单击并以管理员身份运行)的作品。下面是FYI(只是踢的系统成像的)的批处理文件的内容:

c:\Windows\system32\wbAdmin.exe start backup -backupTarget:D: -include:C: -allCritical -quiet

我怎样才能的ShellExecute这个批处理文件管理?

更新1:我试图用每雷米建议CreateProcess的。这里是我的代码(基于this example):

//Code is inside a __fastcall button click
    PROCESS_INFORMATION     piProcInfo;
    STARTUPINFO             siStartInfo;
    siStartInfo.cb          = sizeof(STARTUPINFO);
    siStartInfo.lpReserved  = NULL;
    siStartInfo.lpReserved2 = NULL;
    siStartInfo.cbReserved2 = 0;
    siStartInfo.lpDesktop   = NULL;
    siStartInfo.dwFlags     = 0;

   //   String strCmdLine = "C:\\Users\\rwpatter\\Desktop\\test.bat";
    String strCmdLine = "C:\\Windows\\System32\\wbAdmin.exe start backup -backupTarget:T: -include:C: -allCritical -quiet";

    // Create the child process.
    int rtrn =  CreateProcess(

        NULL,
        strCmdLine.c_str(),
        NULL,           // process security attributes
        NULL,           // primary thread security attributes
        0,              // handles are inherited
        0,              // creation flags
        0,              // use parent's environment
        0,           // use parent's current directory
        &siStartInfo,   // STARTUPINFO pointer
        &piProcInfo);   // receives PROCESS_INFORMATION

        // Wait for the processs to finish
        DWORD rc = WaitForSingleObject(
                      piProcInfo.hProcess, // process handle
                      INFINITE);
        ShowMessage(IntToStr(rtrn));

如果我运行如图所示(exe文件上单击鼠标右键,并以管理员身份运行),则返回0,这意味着它failed。如果我通过把在test.bat的文件WBADMIN命令行(见注释行权在上面的代码String strCmdLine),然后CreateProcess的返回1(成功),但WBADMIN仍没有运行运行它。它闪DOS窗口和我捕获它作为显示在下面的图片。它显示在标题栏中东方字符,说无法识别为内部或外部命令。但是,如果我运行直接的test.bat(高架),它运行WBADMIN没有问题。

关于什么是错的任何想法?除了我显然是无知的。 (附注:我会得到在此之后测试上的ShellExecute Golvind的答案...)

enter image description here

winapi firemonkey c++builder
2个回答
1
投票

手动运行该批处理文件(右键单击并以管理员身份运行)的作品。

因为你正在运行CMD的64位版本,当你手动启动它。

它显示在标题栏中东方字符,说无法识别为内部或外部命令。

因为你的应用程序是32位。一个32位的应用程序不看到相同System32文件夹作为64位应用程序。你可以用虚拟sysnative文件夹访问32位应用程序的64位System32文件夹中。

#include <shellapi.h>
...
    String strCmdLine = "wbAdmin.exe start backup -backupTarget:T: -include:C: -allCritical -quiet";
    int rtrn = CreateProcess(
        NULL,
        strCmdLine.c_str(),
        NULL,           // process security attributes
        NULL,           // primary thread security attributes
        0,              // handles are inherited
        0,              // creation flags
        0,              // use parent's environment
        0,           // use parent's current directory
        &siStartInfo,   // STARTUPINFO pointer
        &piProcInfo);   // receives PROCESS_INFORMATION
    if (!rtrn)
    {
        String newCmdLine = "c:\\windows\\sysnative\\wbAdmin.exe start backup -backupTarget:T: -include:C: -allCritical -quiet";
        rtrn = CreateProcess(
            NULL,
            newCmdLine.c_str(),
            NULL,           // process security attributes
            NULL,           // primary thread security attributes
            0,              // handles are inherited
            0,              // creation flags
            0,              // use parent's environment
            0,           // use parent's current directory
            &siStartInfo,   // STARTUPINFO pointer
            &piProcInfo);   // receives PROCESS_INFORMATION
    }

或者你的应用程序编译成64位。


0
投票

你需要启动cmd.exe与"runas"管理员,并指定批处理文件作为“运行-ME-然后退出”(即/c)参数命令提示符下,像这样:

WCHAR wszCmdPath[MAX_PATH];
GetEnvironmentVariableW(L"ComSpec", wszCmdPath, MAX_PATH);
ShellExecuteW(NULL, L"runas", wszCmdPath, L"/c \"C:\\Path\\BatchFile.bat\"", L"", SW_SHOW);

这里所说的两种功能可能会失败,并在继续之前健壮的代码会测试成功。

© www.soinside.com 2019 - 2024. All rights reserved.