C++ 应用程序无法正确启动0xc0000142 CreateProcessWithLogonW

C++ 应用程序无法正确启动0xc0000142 CreateProcessWithLogonW,c++,winapi,startup-error,C++,Winapi,Startup Error,我有一段代码 if(!CreateProcessWithLogonW( szUserName, NULL, szPassword, LOGON_WITH_PROFILE, L"C:\\Windows\\System32\\cmd.exe", // file to execute NULL, NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB, // creati

我有一段代码

if(!CreateProcessWithLogonW(
    szUserName,
    NULL,
    szPassword,
    LOGON_WITH_PROFILE,
    L"C:\\Windows\\System32\\cmd.exe", // file to execute
    NULL,              
    NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB,   // creation flags
    NULL,              // pointer to new environment block 
    NULL,              // name of current directory 
    &si,               // pointer to STARTUPINFO structure
    &pi                // receives information about new process
    )){
        ReportError(L"Create Process");
    }
不会调用ReportError,但会弹出csrss.exe并显示

我做错了什么

用户名和密码正确无误

整个文件:

// cmd.cpp : Defines the entry point for the console application.
//

#include <Windows.h>
#include <Lmcons.h>
#include <iostream>
#include <ctype.h>
#include <string>
#include <stdio.h>

#define winstring LPWSTR
#define stcas(x) static_cast<x>
#define INFO_BUFFER_SIZE    260 

using namespace std;

void ReportError(LPCWSTR pszFunction, DWORD dwError = GetLastError()) 
{ 
    wprintf(L"%s failed w/err 0x%08lx\n", pszFunction, dwError); 
} 

int main()
{
    TCHAR un[UNLEN+1];
    DWORD size = UNLEN + 1;
    GetUserName(un, &size);

    string una(un);

    bool sys = !una.compare("SYSTEM");

    /*
    if(!sys) {
        system("cls");
        system("title Command Prompt");
        system("cmd");
        return 0;
    }
    */


    wchar_t szUserName[INFO_BUFFER_SIZE] = {}; 
    wchar_t szPassword[INFO_BUFFER_SIZE] = {}; 
    wchar_t *pc = NULL; 
    HANDLE hToken = NULL; 
    BOOL fSucceeded = FALSE; 
    BOOL logon = FALSE;


    printf("Enter the username: "); 
    fgetws(szUserName, ARRAYSIZE(szUserName), stdin); 
    pc = wcschr(szUserName, '\n'); 
    if (pc != NULL) *pc = '\0';  // Remove the trailing L'\n' 

    cout << endl;
    //string un(szUserName);

    printf("Enter the password: "); 
    fgetws(szPassword, ARRAYSIZE(szPassword), stdin); 
    pc = wcschr(szPassword, '\n'); 
    if (pc != NULL) *pc = '\0';  // Remove the trailing L'\n'



    if (!LogonUserW(szUserName, NULL, szPassword,  LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, &hToken)) 
    {
        ReportError(L"Logon");
        goto Cleanup; 
    } 
    else logon = true;

    HANDLE phToken = NULL;

    BOOL dup = FALSE;

    if(!DuplicateTokenEx(hToken, TOKEN_DUPLICATE|TOKEN_IMPERSONATE|TOKEN_QUERY, NULL, SecurityImpersonation, TokenPrimary, &phToken)){
        ReportError(L"DUPLICATE TOKEN");
    }

    else dup = TRUE;

    // Impersonate the logged on user. 
    if (!ImpersonateLoggedOnUser(phToken)) 
    { 

        ReportError(L"imp");
        goto Cleanup; 
    } 

    fSucceeded = true;

    Cleanup: 

    // Clean up the buffer containing sensitive password. 


    LPTSTR szCmdline[] = {"cmd"};
    STARTUPINFOW si;
    PROCESS_INFORMATION pi;

    TCHAR uni[UNLEN+1];
    DWORD sizei = UNLEN + 1;
    GetUserName(uni, &sizei);

    string unai(uni);
    cout << unai << endl;

    memset(&si, 0, sizeof(si));
    si.cb = sizeof(si);



    system("pause");

    // If the impersonation was successful, undo the impersonation. 
    if (fSucceeded && logon) 
    { 
        system("cls");
        system("title Command Prompt");
        //system("cmd");

        if(!CreateProcessWithLogonW(
        szUserName,
        NULL,
        szPassword,
        LOGON_WITH_PROFILE,
        L"cmd.exe", // file to exec
        NULL,              
        NORMAL_PRIORITY_CLASS | CREATE_BREAKAWAY_FROM_JOB,   // creation flags
        NULL,              // pointer to new environment block 
        NULL,              // name of current directory 
        &si,               // pointer to STARTUPINFO structure
        &pi                // receives information about new process
        )){
            ReportError(L"Create Process");
        }
        if (!RevertToSelf()) 
        {  
            ReportError(L"Undo Imp");
        } 

    }
    SecureZeroMemory(szPassword, sizeof(szPassword));
    system("pause");
}
//cmd.cpp:定义控制台应用程序的入口点。
//
#包括
#包括
#包括
#包括
#包括
#包括
#定义环LPWSTR
#定义STCA(x)静态\u转换
#定义信息缓冲区大小260
使用名称空间std;
void ReportError(LPCWSTR pszFunction,DWORD dwError=GetLastError())
{ 
wprintf(L“%s失败,错误0x%08lx\n”,pszFunction,dwError);
} 
int main()
{
TCHAR un[UNLEN+1];
DWORD大小=UNLEN+1;
获取用户名(un和大小);
字符串una(un);
bool sys=!una.compare(“系统”);
/*
如果(!sys){
系统(“cls”);
系统(“标题命令提示符”);
系统(“cmd”);
返回0;
}
*/
wchar_t szUserName[INFO_BUFFER_SIZE]={};
wchar_t szPassword[INFO_BUFFER_SIZE]={};
wchar_t*pc=NULL;
句柄hToken=NULL;
BOOL fSucceeded=假;
BOOL logon=FALSE;
printf(“输入用户名:”);
fgetws(szUserName,ARRAYSIZE(szUserName),stdin);
pc=wcschr(szUserName,'\n');
如果(pc!=NULL)*pc='\0';//删除尾部的L'\n'

如果我跳过对
LogonUser
DuplicateToken
ImpersonateLoggedOnUser
的调用,那么你发布的代码对我来说就行了。你不想为了调用
CreateProcessWithLogon
而模拟另一个用户,因为它基本上是为你做的,所以只要删除所有进行模拟的逻辑即可。


如果目标应用程序需要比用户更高级别的权限,则模拟用户也可能导致程序根本无法启动应用程序。

该代码块可能从未达到过。其他错误与该代码无关。当我输入无效的pas时,它始终达到Swarm,CreateProcessWithLogonW函数返回无效的用户名/密码错误代码。代码运行的是什么上下文?桌面应用程序?哦,调用
CreateProcessWithLogonW
,你会发现这个错误?我感觉你在启动应用程序时得到了这个错误。这个应用程序是作为系统服务运行的吗?它会的是的,但我目前正在使用我自己的提升帐户(以管理员身份运行)进行测试,并键入同一帐户的密码。@CareyGregory