3

i´m working on a Java program. It has to run an app as administrator and, when that execution ends, do another things. In the beginning, i used

String cmd[] = new String [3];
cmd [0] = "cmd";
cmd [1] = "/C";
cmd [2] = "runas /user:..."
Process p = Runtime.getRuntime().exec(cmd);

but the problem was i cannot enter the password (i tried it, but i didn´t find solution). After, i used JNA to make it, like this:

boolean CreateProcessWithLogonW
    (WString lpUsername,
     WString lpDomain,
     WString lpPassword,
     int dwLogonFlags,
     WString lpApplicationName,
     WString lpCommandLine,
     int dwCreationFlags,
     Pointer lpEnvironment,
     WString lpCurrentDirectory,
     STARTUPINFO  lpStartupInfo,
     PROCESS_INFORMATION lpProcessInfo);

Here, the problem is how make to wait the execution of the program until this process ends (maybe, in JNA, there are forms to do this, but is too big and i don´t know how do it...)

I thought another way to do it, but i don´t know if it´s possible... Like this:

Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);   

Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();          

WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
try  
{
    while (kernel32.Process32Next(snapshot, processEntry)) 
    {             
        System.out.println(processEntry.th32ProcessID + "\t" + Native.toString(processEntry.szExeFile));
    }
}

Whit this, i have the id and the name of the process. It would be possible find the name of the user that run this process?? If i succeed, i could be do it (the only process run as administrator would be this one)

Regards.

RMNBA
  • 41
  • 4
  • have you tried using the inputstream of the process? As for the part about native-code: if you're using system-dependent code, you'll have to use system-dependent methods as well. And the list of duplicates would be long, if you specified the OS you're writing the code for. –  Jul 06 '16 at 11:58
  • The OS is Windows 7. Yes, i´ve tried using InputStream and InputStreamReader and it doesn´t work... – RMNBA Jul 06 '16 at 14:22
  • Well, you have to understand first that JNA itself is only a utility to load and run code from shared libraries. The code you want to create has to run as native code on the OS you write it for. So this isn't a java-problem in the first place, but rather a problem specific to c++ and the OS you want to run the code on. [This](http://stackoverflow.com/questions/9454019/how-to-wait-for-a-process-to-finish-c) should do the trick –  Jul 06 '16 at 14:40
  • I succeed the name of all the processes. Is there any way to get the user name that launched the process i need? – RMNBA Jul 07 '16 at 08:32
  • Yup, there is. But honestly: just google it. There even exists a question on this topic here on SO. –  Jul 07 '16 at 09:32
  • I made it!! With the PID. In a loop until that PID doesn´t exits. Thank you for your time. Regards – RMNBA Jul 07 '16 at 10:50

1 Answers1

1

I solved it.

public boolean jambo_loco (int pid)
{

    Kernel32 kernel32 = (Kernel32) Native.loadLibrary(Kernel32.class, W32APIOptions.UNICODE_OPTIONS);
    Tlhelp32.PROCESSENTRY32.ByReference processEntry = new Tlhelp32.PROCESSENTRY32.ByReference();          

    WinNT.HANDLE snapshot = kernel32.CreateToolhelp32Snapshot(Tlhelp32.TH32CS_SNAPPROCESS, new WinDef.DWORD(0));
    try  
    {
        int i = 0;

        int size = processEntry.dwSize.intValue();

        while (kernel32.Process32Next(snapshot, processEntry) && i < size) 
        {             
            if (processEntry.th32ProcessID.intValue() == pid)
                return true;
            i++;
        }

    }
    finally 
    {
        kernel32.CloseHandle(snapshot);
    }
    return false;
}

A loop, in other side, invoques many times this method when it returns true.

Regards.

RMNBA
  • 41
  • 4