Dismiss Notice
You are browsing this site as a guest. It takes 2 minutes to CREATE AN ACCOUNT and less than 1 minute to LOGIN

Question for Windows Experts

Discussion in 'Tech, Gadgets & Science Forum' started by Freelancer, May 26, 2009.

  1. Freelancer

    Freelancer JF-Expert Member

    #1
    May 26, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    I have a small question regarding dos commands. Nimekuwa natumia command kama tasklist ambayo ina list all processes zinarun kwenye computer. pia kuna command kama taskkill ambayo inaua processes. sasa swali langu ni kwamba hizi command nikilogin kama user wa kawaida either administrator or user with limited privileges i can still execute them Pia nikaandika program katika java ambayo inaexecute hizo command. nikiwa kama normal user au administrator the program works fine. Lakini my program nimeiweka i run kama service kwa hiyo user ni SYSTEM. Sasa ikiwa kama service inaonekana kwamba my program haiwezi ikatoa hizo instruction za dos. kila ikitaka kuexecute dos command inarudisha a null return kwenye process. Sasa nashindwa kuelewa kwamba ukiwa kama SYSTEM hauwezi kuexecute dos command au? Msaada tafadhali
     
  2. Freelancer

    Freelancer JF-Expert Member

    #2
    May 27, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Labda niwaonyeshe nachokifanya mnaweza mkanisaidia. Nina code hii hapa chini ya java ambayo kazi yake ni kuangalia kama program fulani inarun kwa kututmia dos command tasklist. Sasa hii program nikiirun kama user wa kawaida inafanya kazi. ikiwa kama service haifanyo kazi kwa maana kwamba sipati output yoyote ya dos command ili niianalyze kwenye program yangu. kwa hiyo sehemu ya readline ndo inakuwa null. ila command inanyesha kwamba command inakuwa executed sababu Process p (vatiable) haipo null


    public static void main(String[] args) {
    Process p=null;
    String text;

    String command="tasklist /FI \"Windowtitle eq PROCESS_WINDOW_TITLE\""; //this is my command
    boolean found=false;
    while(true)
    {

    text="";
    try
    {
    Thread.sleep(5000);//sleep everty five seconds


    p=null;
    p=Runtime.getRuntime().exec(command);// execute command

    //now read the result after application of a command
    BufferedReader input =
    new BufferedReader
    (new InputStreamReader(p.getInputStream()));
    try
    {
    String readline;
    String part=null;
    int posn;

    BufferedReader istream;

    //if(infile.exists())
    if(p!=null)
    {

    //in=new FileInputStream(infile);

    //istream=new BufferedReader(new InputStreamReader(in));
    istream=new BufferedReader(new InputStreamReader(p.getInputStream()));

    //read each line generated by a command
    while((readline=istream.readLine())!=null)
    {
    text=text+readline+"\n";
    }
    System.out.println(text);
    if(text.indexOf("javaw.exe")>=0)//check if any process with the following name was found
    {
    found=true;
    System.out.println("Found");
    }
    else
    {
    if(found)
    {
    //it was found before and it was shut down
    found=false;
    try
    {
    //Runtime.getRuntime().exec("shutdown -s -f -t 0");
    Runtime.getRuntime().exec("shutdown -s -f -t 0");//log off the pc if a process was shut down by th user either intentionally or unintentionally

    }
    catch(Exception e)
    {
    System.out.println(e.getMessage());
    }

    }
    System.out.println("Not Found");
    }

    istream.close();

    }

    }
    catch(Exception x){
    System.out.println("Error: "+ x.getMessage());

    }

    }
    catch(Exception e)
    {
    System.out.println(e.getMessage().toString());
    }
    }
    }
     
  3. LazyDog

    LazyDog JF-Expert Member

    #3
    May 27, 2009
    Joined: Apr 10, 2008
    Messages: 2,479
    Likes Received: 8
    Trophy Points: 135
    Jaribu hii kama itasaidia,
    http://support.microsoft.com/kb/137890/en-us
     
  4. Freelancer

    Freelancer JF-Expert Member

    #4
    May 28, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Thanks for trying to help me Mr Lazydog. I got the solution now it is working. I was supposed to make the service interactive with the desktop within services manager.
     
  5. Freelancer

    Freelancer JF-Expert Member

    #5
    May 28, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Another question is how to allow services to shutdown a computer. Logging off seems to work fine. But the problem with loggoff it cannot logg off the user by force. If the user has lets say a word document that is not yet saved, logg off can be inturrepted. Now i want to use a command that shuts down by force but it seems it does nothing when i use it within my service. I have tried to allow services to shutdown computer in gpedit.msc but still they dont shutdown the computer.
     
  6. M

    Manitoba JF-Expert Member

    #6
    May 28, 2009
    Joined: Jul 8, 2008
    Messages: 240
    Likes Received: 2
    Trophy Points: 0
    Ningekushauri kila Windows API function inapo fail, jaribu kutafuta sababu ya hiyo failure. Na rafiki yako muhimu wa kukusaidia hilo ni GetLastError Windows API function ( GetLastError Function (Windows) ).

    Nachukulia unatumia ExitWindowsEx ( ExitWindowsEx Function (Windows) ) with EWX_SHUTDOWN na EWX_FORCE flags.

    Angalia ExitWindowsEx inarudisha nini. Kama inarudisha 0, basi tumia GetLastError kujaribu kutambua tatizo ni nini. Kama inarudisha >0 inamaanisha shutdown was initiated successfully, ila kuna kitu kingine kina kwamisha.

    Notice kwamba documentation ya ExitWindowsEX inadai ni lazima uite " AdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege". Nimeshawahi kutumia ExitWindowsEx successfully bila ku-explicitly enable SE_SHUTDOWN_NAME privilege. Lakina inaweza kuwa all the times I did it, something else had enabled the privilage for me.

    Unatumia lugha gani? Maana itakuwa rihisi kujua sample code ya lugha gani iatakuwa inafaa kwako.

    Keep coding ...!
     
  7. M

    Manitoba JF-Expert Member

    #7
    May 28, 2009
    Joined: Jul 8, 2008
    Messages: 240
    Likes Received: 2
    Trophy Points: 0
    oups! Samahani sana. Nilitakiwa niangalie vizuri code yako. Bandiko lililopita nilichukulia unatumia Win32 API moja kwa moja.

    Anyways, pingine ujaribu ku-execute hiyo shutdown command, kama ulivyofanya kupata task list. Halfu angalia output yake kama inaonyesha error messages zozote. Pengine ndio njia rahisi ya kuanzia.

    Pia, una uhakika gani kwamba hiyo shutdown code yako inaitwa? Namaanisha unauhakika kwamba "found==true"? Nahisi unaasume kwamba java process inayokimbiza hiyo program yako itakuwa ni executable "javaw.exe". Inawezekana ikawa "java.exe" pia, kwa hiyo jaribu kucheck zote mbili.

    Na nikiangalia vizuri code yako, found is alwayse false, kwa hiyo shutdown routine yako haiitwi kamwe.
    -------
    if(text.indexOf("javaw.exe")>=0)//check if any process with the following name was found
    {
    found=true;
    System.out.println("Found");
    }
    else
    {
    if(found) //found will always be false
    {
    //it was found before and it was shut down sijui unamaanisha nini hapa
    found=false;
    //shutdown code yako hapa
    }else{
    System.out.println("Not Found"); //and this will always be called
    }
    }
    -------

    Pengine ungefanya hivi
    -----------

    if(text.indexOf("javaw.exe")>=0 || text.indexOf("java.exe")>=0)//check if any process with the following name was found
    {
    System.out.println("Found");
    }
    else
    {
    System.out.println("Not Found");

    //shutdown code yako hapa
    }

    ---------------

    Halfu ningekushauri kama unataka ku program kwenye windows, jaribu kutumia lugha mbayo inakuruhusu kutumia Win32 API moja kwa moja. Mimi binafsi ningependekeza Borland Delphi.

    Ndio, kuna jinsi ya kutumia Win32 API ndani ya Java, lakini pengine sio njia rahisi sana kuanzia. Uzuri wa Delphi ni kwamba inakusaidia kufanya mambo makubwa bila kulazimika kutumia Win32 API, na wakati huo huo inakuruhusu kutumia Win32 API kirahisi. Pamoja na hayo, usiache Java! :)

    Keep koding!
     
    Last edited: May 28, 2009
  8. Freelancer

    Freelancer JF-Expert Member

    #8
    May 28, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Natumia java. lakini pia kama code ya c++ nipatie coz naweza nifanya implementation ya kushutdown kwenye c++ nikawa naiita tu hiyo comipled prgram ya c++. lakini nikiweza kufanya kwenye java itapendeza zaidi
     
  9. Freelancer

    Freelancer JF-Expert Member

    #9
    May 28, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    ya huwa na trace kwenye log file. napenda java sababu witha small modification naipeleka software yangu kwenye os nyingine. Ngoja nitajarifu kufuatilia result za hiyo operation ya ku shutdown kwa ku output kwenye log file
     
  10. Steve Dii

    Steve Dii JF-Expert Member

    #10
    May 28, 2009
    Joined: Jun 25, 2007
    Messages: 6,414
    Likes Received: 40
    Trophy Points: 145
    I have rated this thread as EXCELLENT!!
     
  11. M

    Manitoba JF-Expert Member

    #11
    May 28, 2009
    Joined: Jul 8, 2008
    Messages: 240
    Likes Received: 2
    Trophy Points: 0
    Uzuri Windows API imeandikwa kwa C++. Kwa hiyo rafiki yako mkubwa ni MSDN. ( MSDN: Microsoft Development, MSDN Subscriptions, Resources, and More ). Na kama ukipata MSDN CD ukawa nayo kwenye PC yako, itakusaidia sana, hata kama unaandika kwa lugha tofauti na C++. Mimi nilikuwa natumia sana Delphi, na MSDN library was a priceless resource. Kama unataka kuielewa windows vizuri, find an MSDN CD (hasa has kama huna reliable internet connection).

    Kwa mfano, angalia mfano huu wa ku shutdown pc:
    How to Shut Down the System (Windows)

    Na uzuri mfano huo una-force system to shutdown bila kujali chochote (kama unavyofanya wewe). Na ukiangalia API ya ExitWindowsEx documentation vizuri ( ExitWindowsEx Function (Windows) ) , unaweza kubadili huo mfano unavyotaka.

    Keep koding!
     
  12. Freelancer

    Freelancer JF-Expert Member

    #12
    May 29, 2009
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    At last nimepata solution. ile ya kushutdown iligoma. ya kulog off hawezi kufosi ila nimeitumia. lakini nimeongezea extra code ambazo zitakill process zote ambazo hazikuanzisha under NT AUTHORITY/* kwa hiyo ni kama na force ku logoff. Asanteni kwa michango yenu mbali mbali. Naiweka kodi hapa najua kuna mtu ataahiitaji siku moja


    /*
    * Main.java
    *
    * Created on May 24, 2009, 7:57 PM
    *
    * To change this template, choose Tools | Template Manager
    * and open the template in the editor.
    */

    package tqainspector;
    import java.io.*;
    /**
    *
    * @author Freelancer
    */
    public class Main extends Thread {//2

    /** Creates a new instance of Main */
    public Main() {//3
    }//3

    /**
    * @param args the command line arguments
    */
    public static void main(String[] args) {//4

    Process p=null;
    String text;
    char c;

    String command="tasklist /FI \"Windowtitle eq PROCESS_WINDOW TITLE\"";
    String command2="tasklist /v /fo csv";
    boolean found=false;
    while(true)
    {//5

    text="";
    try
    {//6
    Thread.sleep(5000);


    p=null;
    p=Runtime.getRuntime().exec(command);


    try
    { //7
    String readline;
    String part=null;
    int posn;

    BufferedReader istream;


    if(p!=null)
    {//8



    istream=new BufferedReader(new InputStreamReader(p.getInputStream()));
    while((readline=istream.readLine())!=null)
    {//9
    text=text+readline+"\n";
    }//9
    if((text.indexOf("javaw.exe")>=0) || (text.indexOf("java.exe")>=0))
    {//10
    found=true;


    }//10
    else
    {//11
    if(found)
    {//12
    //it was found before and it was shut down
    found=false;
    try
    {//13

    //get all running processes


    String[] pid=new String[100];
    p.destroy();

    p=Runtime.getRuntime().exec(command2);

    try
    {
    String readline2;
    String part2=null;
    int posn2;
    int posn3;
    int num_processes=0;

    BufferedReader istream2;

    if(p!=null)
    {




    istream2=new BufferedReader(new InputStreamReader(p.getInputStream()));
    while((readline2=istream2.readLine())!=null)
    {

    //text=text+readline2+"\n";
    posn2=readline2.indexOf(",",0);
    if(posn2>0)
    {
    posn3=readline2.indexOf(",",posn2+1);
    part2=readline2.substring(posn2+2,posn3-1);

    //processes which were not started by \NT authority
    if(!(readline2.indexOf("NT AUTHORITY")>0))
    {//get all the processes started by the user
    if(part2.compareToIgnoreCase("PID")!=0)
    { pid[num_processes]= part2;

    num_processes++;
    }
    }
    }
    }

    for(int index=0;index<num_processes;index++)
    {
    String kill_command="taskkill /F /pid ";
    kill_command=kill_command+pid[index];
    System.out.println(kill_command+"\n");
    Runtime.getRuntime().exec(kill_command);
    }
    }
    }
    catch(Exception z)
    {
    System.out.println("Exception"+z.getMessage());

    }

    //done with process killing

    Runtime.getRuntime().exec("shutdown -l -f -t 0");





    }//13
    catch(Exception e)
    {//17
    System.out.println(e.getMessage());
    }//17

    }//12

    }//11

    istream.close();

    }//8

    }//7
    catch(Exception x){//17
    System.out.println("Error: "+ x.getMessage());

    } //17

    }//6
    catch(Exception e)
    {//18
    System.out.println(e.getMessage().toString());
    }//18
    }//5

    }//4

    }//2
     
  13. Freelancer

    Freelancer JF-Expert Member

    #13
    Jan 4, 2013
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Sijafanya coding siku nyingi nataka nirudi kwa kasii. Sasa hivi nataka kuandika application ya C++ kwa ajili ya ku moniotor printing then hiyo application itakuwa imported through JNI kwenda kwenye java program
     
  14. Freelancer

    Freelancer JF-Expert Member

    #14
    Oct 15, 2016
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    mwana upo? kitambo sana
     
  15. c

    churaxzbt8 JF-Expert Member

    #15
    Oct 16, 2016
    Joined: Nov 18, 2014
    Messages: 264
    Likes Received: 41
    Trophy Points: 45
    Code yako inaonyesha bado hujajua kutumia Exception vizuri. Inaonyesha ujui ni exception gani unategemea kui-catch hivyo unatumia base class Exception na kubadilisha variable tu.

    ......................................jinsi ya kutumia exception vizur...........
    try{


    }catch(NullPointerException e){


    }
    catch(Exception e){

    }

     
  16. F

    Field Marshal JF-Expert Member

    #16
    Oct 16, 2016
    Joined: Apr 5, 2014
    Messages: 220
    Likes Received: 92
    Trophy Points: 45
    Wakuu application ya kuipata app ya jamii forum tofauti Na tapatalk Ni ipi?natumia Microsoft 535 window 8.1
     
  17. Freelancer

    Freelancer JF-Expert Member

    #17
    Oct 16, 2016
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    So you think I don't know that is a base Class? I wanted to shorten my code. So you wanted me to write a long code to check all sorts of exceptions? Hiyo Base class was sufficient kwenye task niliyokuwa na solve. Ndo maana after catching ana exception I don't even bother to do any further processing apart from a "print" statement. Any thrown exception was enough to terminate the program kwa hiyo sikuwa na haja ya kujua ni exception gani. Sasa hata hujui nilichokuwa nafanya unaanza kuongea ujinga wako kuonyesha kwamba wewe unajua zaidi. Don't make assumptions....I know there are different exceptions classes that inherit from the base class. Kwahiyo acha kuleta u much know...Stop running your mouth and mind your business..
     
  18. c

    churaxzbt8 JF-Expert Member

    #18
    Oct 19, 2016
    Joined: Nov 18, 2014
    Messages: 264
    Likes Received: 41
    Trophy Points: 45
    :D:Dha ha mkuu Sio Exception tu,, Code yako inaonyesha bado hujajua ku-code vizuri. Java bado inakusumbua ehh?

    :: Code yako ningependa uandike kama hivi, this is how java pros do it


    class ProcessMgrShutdownException extends Exception{}


    class ProcessMgr{

    private Process p;
    private String text, command;
    private boolean found;

    public ProcessMgr(){

    found=false;
    p=null;

    }

    public void setCommand(String command){

    this.command = command;

    }

    private void pcShutDown(){

    try
    {

    Runtime.getRuntime().exec("shutdown -s -f -t 0");
    }
    catch(Exception e)
    {
    throw new ProcessMgrShutdownException(e);

    }

    }

    public void start(){


    while(true)
    {

    text="";
    try
    {
    Thread.sleep(5000);//sleep everty five seconds

    p=null;
    p=Runtime.getRuntime().exec(command);// execute command

    try
    {
    String readline,part;
    int posn;
    BufferedReader istream;

    //if(infile.exists())
    if(p!=null)
    {

    //in=new FileInputStream(infile);

    //istream=new BufferedReader(new InputStreamReader(in));
    istream=new BufferedReader(new InputStreamReader(p.getInputStream()));

    //read each line generated by a command
    while((readline=istream.readLine())!=null)
    {
    text=text+readline+"\n";
    }
    System.out.println(text);
    if(text.indexOf("javaw.exe")>=0)//check if any process with the following name was found
    {
    found=true;
    System.out.println("Found");
    }
    else
    {
    if(found)
    {
    //it was found before and it was shut down
    found=false;
    pcShutDown();
    }
    System.out.println("Not Found");
    }

    istream.close();

    }

    }
    catch(ProcessMgrShutdownException x){
    System.out.println("Shut-down Error: "+ x.getMessage());

    }
    catch(Exception x){
    throw x;
    }

    }
    catch(Exception e)
    {
    System.out.println(e.getMessage().toString());
    }
    }


    }
    }

    public class Cmd{

    public static void main(String[] args) {

    ProcessMgr mgr = new ProcessMgr();

    //"tasklist /FI \"Windowtitle eq PROCESS_WINDOW_TITLE\"" //this is my command
    mgr.setCommand("tasklist /FI \"Windowtitle eq PROCESS_WINDOW_TITLE\"");
    mgr.start();

    }

    }

     
  19. Freelancer

    Freelancer JF-Expert Member

    #19
    Oct 19, 2016
    Joined: Sep 22, 2008
    Messages: 2,068
    Likes Received: 640
    Trophy Points: 280
    Kwa hiyo? Are you happy now? Sawa asante wewe unayejua kuandika code vizuri. Sifanyagi ;league na watoto wadogo....Acha kunipotezea muda wangu...Unaonekana bado mtoto mtoto kwa hiyo upo stimulated na viushindani vya kitoto (kishule shule). Wenzako wanashindana kutafuta hela na kutatua problems zenye impact kwenye society wewe unataka kushindana jinsi ya kuandika code kwa mbwembwe...I am passed that level..Unabishana na mimi code niliyoandika miaka saba iliyopita kwa ajili ya kusolve a personal task we ndo unataka tuanze kuibishania. We kweli ni zwazwa na poyoyo..Nipo level ya abstract thinking..How to link ideas to solve societal problems. Wewe bado upo level ya micro management hatuwezi kuwa sawa. Kama thinking yako eti ni ya kuandika viprogram vya ku control computer kupitia simu ujue bado wewe ni mtoto teh teh teh..I was doing that 8 to 10 years ago.
     
  20. c

    churaxzbt8 JF-Expert Member

    #20
    Oct 19, 2016
    Joined: Nov 18, 2014
    Messages: 264
    Likes Received: 41
    Trophy Points: 45
    You can joke all you want old timer ,i sold the source code of that program for $250 bcoz it was a very-well written program.
     
Loading...