Tech Off Thread

4 posts

Forum Read Only

This forum has been made read only by the site admins. No new threads or comments can be added.

Very odd LoadLibrary problem

Back to Forum: Tech Off
  • User profile image
    spribyl

    Can someone please tell me why this program generates the following error.
        User Name:Administrator
        Pid=764
        UserName:steve
        Unable to load the Java virtual machine: A dynamic link library (DLL) initialization routine failed.

    And how I might be able to fix or work around this problem.
    BTW the DLL is just a random library used to duplicate the problem.  This happens with any hand loaded DLL.

    Thanks
    Steve

    Here is the source.
    #include <windows.h>

    int main(int argc, char** argv)
    {
        HANDLE htoken = NULL;
            char *cmd=NULL;
        PROCESS_INFORMATION pi;
        STARTUPINFO si = {0};
        HANDLE hWritePipe;
        SECURITY_ATTRIBUTES sattr;
            char buffer[2048];
            int s=sizeof(buffer);
            GetUserName(buffer,&s);
        printf("UserName:%s\n",buffer);

            if(LoadLibrary("c:\\winnt\\twain_32.dll")==NULL) {
           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
                             FORMAT_MESSAGE_ARGUMENT_ARRAY,
                             NULL,
                             GetLastError(),
                             LANG_NEUTRAL,
                             buffer,
                             sizeof(buffer),
                             NULL);
                    printf("Unable to load the Java virtual machine: %s",buffer);
                    return 1;
            }

        if (!LogonUser("steve", NULL, "stuff4me",
            LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, &htoken)) {
           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
                             FORMAT_MESSAGE_ARGUMENT_ARRAY,
                             NULL,
                             GetLastError(),
                             LANG_NEUTRAL,
                             buffer,
                             sizeof(buffer),
                             NULL);
            printf("Unable to start user ARE:%s \n",buffer);
            return 1;
        }

        si.cb = sizeof(si);
        si.dwFlags |= STARTF_USESTDHANDLES;
        si.wShowWindow = SW_HIDE;
        // Get the handle to the current STDOUT.
        sattr.nLength = sizeof(SECURITY_ATTRIBUTES);
        sattr.lpSecurityDescriptor = NULL;
        sattr.bInheritHandle = TRUE;

        if (!CreatePipe(&(si.hStdInput), &hWritePipe,  &sattr, 0)) {
            printf("Unable to create input pipe !!!\n");
            return 1;
        }

        if(!DuplicateHandle(GetCurrentProcess(),
            GetStdHandle(STD_OUTPUT_HANDLE),
            GetCurrentProcess(),
            &(si.hStdOutput),
            0,
            TRUE,
            DUPLICATE_SAME_ACCESS)) {
            printf("Unable to duplicate file handle !!!\n");
            return 1;
        }
        if(!DuplicateHandle(GetCurrentProcess(),
            GetStdHandle(STD_ERROR_HANDLE),
            GetCurrentProcess(),
            &(si.hStdError),
            0,
            TRUE,
            DUPLICATE_SAME_ACCESS)) {
            printf("Unable to duplicate file handle !!!\n");
            return 1;
        }

            if(argc >1 && argv[1][0]=='1') {
                    return 0;
            }
        cmd="childprocess 1";
        ImpersonateLoggedOnUser(htoken);
        if (CreateProcessAsUser(htoken, NULL, cmd,
    //    if (CreateProcess(NULL, cmd,
            NULL, NULL,
            TRUE,
            NORMAL_PRIORITY_CLASS , //FLAGS
            NULL,
            NULL,
            &si, &pi)  == TRUE) {
            printf("Pid=%d\n", pi.dwProcessId);
            CloseHandle(pi.hThread);
        } else {
           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
                             FORMAT_MESSAGE_ARGUMENT_ARRAY,
                             NULL,
                             GetLastError(),
                             LANG_NEUTRAL,
                             buffer,
                             sizeof(buffer),
                             NULL);
            printf("Unable to start user ARE, %s\n",buffer);
        }
        RevertToSelf();

            return 0;
    }

  • User profile image
    Rossj

    spribyl wrote:
    Can someone please tell me why this program generates the following error.
        User Name:Administrator
        Pid=764
        UserName:steve
        Unable to load the Java virtual machine: A dynamic link library (DLL) initialization routine failed.

            if(LoadLibrary("c:\\winnt\\twain_32.dll")==NULL) {
           FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM |
                             FORMAT_MESSAGE_ARGUMENT_ARRAY,
                             NULL,
                             GetLastError(),
                             LANG_NEUTRAL,
                             buffer,
                             sizeof(buffer),
                             NULL);
                    printf("Unable to load the Java virtual machine: %s",buffer);
                    return 1;
            }


    I didn't bother looking at the rest of your code so there may be other issues. First you are losing the return (HMODULE) from LoadLibrary, which you'll probably need at some point for the call to GetProcAddress (and almost certainly for FreeLibrary).

    The reason for the error is that the DllMain is probably returning FALSE, meaning (most probably) that something it needed is not available - write your own stub dll where the DllMain returns TRUE and try again. The alternative is that you are trying to load more than 64 DLLs.


  • User profile image
    footballism

    vistawillship wrote:
    Excellent. Niners helping Niners. This is the community Charles and Lenn envisioned.

        If you want your technical questions to be answered, you should use MSDN forums or news groups instead, those are the preferred way to get helpful answers and solutions.

    Sheva

  • User profile image
    spribyl

    An excellent and useful answer.

    Thank you very much.

Conversation locked

This conversation has been locked by the site admins. No new comments can be made.