M
M
Maxim Kuprashevich2014-12-03 16:32:04
Microsoft
Maxim Kuprashevich, 2014-12-03 16:32:04

Why does CreateProcessWithLogonW Error 1783: The stub received bad data occur?

Hello. I have the following test code to run a process as a user:

int main(int argc, char *argv[])
{
    setlocale(LC_ALL, "");

    std::string user;
    std::string pass;
    std::cout << "user: ";
    getline(std::cin, user);
    std::cout << "\npass: ";
    getline(std::cin, pass);
    std::cout << std::endl;

    std::wstring suser = std::wstring(user.begin(), user.end());
    LPCWSTR su = suser.c_str();
    std::wstring spass = std::wstring(pass.begin(), pass.end());
    LPCWSTR sp = spass.c_str();

    DWORD     dwSize =  0;
    HANDLE    hToken ;
    LPVOID    lpvEnv =  0;
    PROCESS_INFORMATION pi = {0};
    STARTUPINFO         si = {0};
    WCHAR               szUserProfile[256] = L"";

    si.cb = sizeof(STARTUPINFO);

    if (!LogonUser(su, L".", sp, LOGON32_LOGON_INTERACTIVE,
            LOGON32_PROVIDER_DEFAULT, &hToken))
        qDebug() << "LogonUser";

    if (!CreateEnvironmentBlock(&lpvEnv, hToken, TRUE))
        qDebug() << "CreateEnvironmentBlock";

    dwSize = sizeof(szUserProfile)/sizeof(WCHAR);

    if (!GetUserProfileDirectory(hToken, szUserProfile, &dwSize))
        qDebug() << "GetUserProfileDirectory";

    WCHAR app[] = L"\"C:\\Program Files (x86)\\Adobe\\Reader 11.0\\Reader\\AcroRd32.exe\" \"C:\\Users\\UD\\Desktop\\insect immunity.pdf\"";
    if (!CreateProcessWithLogonW(su, L".", sp,
            LOGON_WITH_PROFILE, NULL, app,
            CREATE_UNICODE_ENVIRONMENT, lpvEnv, szUserProfile,
            &si, &pi)) {
            LPVOID lpMsgBuf;
            LPVOID lpDisplayBuf;
            DWORD dw = GetLastError();

            FormatMessage(
                FORMAT_MESSAGE_ALLOCATE_BUFFER |
                FORMAT_MESSAGE_FROM_SYSTEM |
                FORMAT_MESSAGE_IGNORE_INSERTS,
                NULL,
                dw,
                MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                (LPTSTR) &lpMsgBuf,
                0, NULL );

            // Display the error message and exit the process

            lpDisplayBuf = (LPVOID)LocalAlloc(LMEM_ZEROINIT,
                (lstrlen((LPCTSTR)lpMsgBuf) + 40) * sizeof(TCHAR));
            StringCchPrintf((LPTSTR)lpDisplayBuf,
                LocalSize(lpDisplayBuf) / sizeof(TCHAR),
                TEXT("failed with error %d: %s"), dw, lpMsgBuf);
            qDebug() << QString::fromWCharArray((LPTSTR)lpDisplayBuf);
    }

    if (!DestroyEnvironmentBlock(lpvEnv))
        qDebug() << "DestroyEnvironmentBlock";

    CloseHandle(hToken);
    CloseHandle(pi.hProcess);
    CloseHandle(pi.hThread);

    system("pause");
    return 0;
}

For me, it gives a floating error "Error 1783: The stub received bad data". Floating because sometimes it disappears and everything works. Relationships could not be identified. I tried to remove unnecessary services, as MSDN advises, but I could not remove a lot and did not change anything.
Everything is perfect on my friend's computer, they tested it as best they could, the error never got out.
Of course, I can work with this code from a virtual machine, but the question arises whether there will be such garbage when I transfer the project to the customer's machines. Therefore, you need to know why this occurs and how to deal with it. If who faced - I will be grateful for advice or council.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jcmvbkbc, 2014-12-03
@UndeadDragon

It seems to me that the call to LoadUserProfile is missing before the CreateEnvironmentBlock call.
Does success correlate with the load on the user profile in the registry?
What happens if you throw out all the dances with getting the user's home directory and call CreateProcessWithLogonW with a NULL environment and the current directory available to the user?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question