Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Remote rebooting using the API

Hi, I'm looking for a way to remotely reboot a series of servers on our LAN. I haven't found a way to do this via the API. If there is an API function that does this, would you please let me know. (We have both NT4's and W2K's on the LAN)

Regards
Chris

Comments

  • Stoic JokerStoic Joker Member Posts: 84
    : Hi, I'm looking for a way to remotely reboot a series of servers on our LAN. I haven't found a way to do this via the API. If there is an API function that does this, would you please let me know. (We have both NT4's and W2K's on the LAN)
    :
    : Regards
    : Chris
    :


    Greetings
    Granted this is a bit (Um...)unpolished But it's the code for something I was playing with a while back for remote reboot/shutdown.

    Hope it's usefull

    [CODE]
    #include "stdafx.h"
    #include "shlobj.h"
    #include "stdio.h"
    #include "commctrl.h"
    #include "resource.h"

    int slr=0;
    int Okay=0;
    int when=42;
    char Targ[42];
    char Message[130];
    HWND hDlg;
    char szFile[MAX_PATH];
    DWORD cchBuff=256;
    DWORD useBuff=256;
    char UserName[120];
    char CompName[MAX_COMPUTERNAME_LENGTH];
    char TitleBar[200];
    char MessageW[1024];
    HINSTANCE hInst;
    BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
    int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
    {
    DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)DlgProc,SW_SHOW);
    return TRUE;
    }
    //**********Well Here Goes...*******************************************
    void RebootSystem()
    {
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    BOOL fResult;

    if(!OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES|
    TOKEN_QUERY,&hToken))
    MessageBox(0,"Can't Open Process TOKEN","ERROR",MB_OK);

    LookupPrivilegeValue(Targ,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    tkp.PrivilegeCount=1;
    tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
    if(GetLastError() != ERROR_SUCCESS) MessageBox(0,"Can't Adjust TOKEN Privileges","ERROR",MB_OK);
    if(slr==0){
    fResult = InitiateSystemShutdown(Targ,
    Message,
    when,
    TRUE,
    FALSE);
    }
    if(slr==1){
    fResult = InitiateSystemShutdown(Targ,
    Message,
    when,
    TRUE,
    TRUE);
    }
    if(!fResult)MessageBox(0,"InitiateSystemShutdown FAILED","ERROR",MB_OK);
    }
    //??????????????????????????????????????????????????????????????????????
    LPITEMIDLIST PidlBrowse(int nCSIDL)
    {
    LPITEMIDLIST pidlRoot = NULL;
    LPITEMIDLIST pidlSelected = NULL;
    BROWSEINFO bi;// = {0};
    LPMALLOC pMalloc = NULL;

    SHGetMalloc(&pMalloc);
    SHGetFolderLocation(0, nCSIDL, NULL, NULL, &pidlRoot);

    bi.hwndOwner = hDlg;
    bi.pidlRoot = pidlRoot;
    bi.pszDisplayName = szFile;
    bi.lpszTitle = "Find (TargeT) Computer";
    bi.ulFlags = BIF_BROWSEFORCOMPUTER;
    bi.lpfn = NULL;
    bi.lParam = 0;

    pidlSelected = SHBrowseForFolder(&bi);

    pMalloc->Free(pidlRoot);
    pMalloc->Release();

    return pidlSelected;
    }
    //??????????????????????????????????????????????????????????????????????
    //**********************************************************************
    /* void PokeAndHope()
    {
    BROWSEINFO bi;
    bi.hwndOwner = hDlg;
    bi.pidlRoot = NULL;
    bi.pszDisplayName = szFile;
    bi.lpszTitle = "Find (TargeT) Computer";
    bi.ulFlags = BIF_BROWSEFORCOMPUTER;
    bi.lpfn = NULL;
    SHBrowseForFolder(&bi);
    } */
    //**********************************************************************
    //(((((((((((((((((((( ERROR CHECK (((((((((((((((((((((((((((((((((((((
    void ErrorCheck()
    {
    GetComputerName(CompName,&cchBuff);
    GetUserName(UserName,&useBuff);
    if((strcmpi(Targ,"")==0)||(strcmpi(Targ,CompName)==0))
    {
    sprintf(TitleBar,"WARNING %s",UserName);
    if(slr==0)sprintf(MessageW,"Your about to ShutDown %s(LocalMachine) in %d Seconds!!",CompName,when);
    if(slr==1)sprintf(MessageW,"Your about to ReBoot %s(LocalMachine) in %d Seconds!!",CompName,when);
    if(MessageBox(0,MessageW,TitleBar,MB_OKCANCEL)==IDCANCEL)Okay=1;
    }
    else{
    sprintf(TitleBar,"Confirmation");
    if(slr==0)sprintf(MessageW,"ShutDown %s in %d Seconds?",Targ,when);
    if(slr==1)sprintf(MessageW,"ReBoot %s in %d Seconds?",Targ,when);
    if(MessageBox(0,MessageW,TitleBar,MB_OKCANCEL)==IDCANCEL)Okay=1;
    else Okay=2;
    }
    }
    //)))))))))))))))))) END ERROR CHECK )))))))))))))))))))))))))))))))))))
    //>>>>>>>>>>>>>>>>>>>>>>>> ABORT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    HANDLE hToken;
    TOKEN_PRIVILEGES tkp;
    BOOL fResult;

    void AborT()
    {
    if (!OpenProcessToken(GetCurrentProcess(),
    TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
    {
    MessageBox(0,"Can't Open Process TOKEN","ERROR",MB_OK);
    }
    //........................................................
    if(strcmpi(Targ,"")==0)MessageBox(0,"Can't Abort, No Name Specified!","ERROR",MB_OK);
    if(strcmpi(Targ,CompName)==0)
    {
    LookupPrivilegeValue(Targ,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    }
    else{
    LookupPrivilegeValue(Targ,SE_REMOTE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    }
    //........................................................
    tkp.PrivilegeCount = 1;
    tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

    AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);

    if(GetLastError() != ERROR_SUCCESS)
    {
    MessageBox(0,"Can't Adjust TOKEN Privileges","ERROR",MB_OK);
    }

    fResult = AbortSystemShutdown(Targ);
    if(!fResult)
    {
    MessageBox(0,"Abort ShutDown FAILED","ERROR",MB_OK);
    }
    }
    //<<<<<<<<<<<<<<<<<<<<<< END ABORT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    BOOL CALLBACK DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM)
    {
    switch(uMsg)
    {
    case WM_INITDIALOG:
    { slr=0;
    CreateUpDownControl(
    WS_CHILD|WS_VISIBLE|UDS_SETBUDDYINT,
    240,49,17,19,hDlg,IDC_SPIN1,hInst,
    GetDlgItem(hDlg,IDC_WHEN),999,0,42);
    CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT1);
    SetFocus(GetDlgItem(hDlg,IDC_TARGET));
    SetDlgItemText(hDlg,IDC_TARGET,"Okay Who Dies??");
    SendMessage(GetDlgItem(hDlg,IDC_TARGET), EM_SETSEL, 0,-1);
    }
    case WM_COMMAND:
    switch(wParam)
    {
    case IDC_BROWSE:
    {
    PidlBrowse(CSIDL_NETWORK);
    //PokeAndHope();
    SetDlgItemText(hDlg,IDC_TARGET,szFile);
    break;
    }
    case IDC_RABUTT1:
    { slr=0;
    CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT1);
    SetDlgItemText(hDlg,IDOK,"Okay ShutDown");break;
    }
    case IDC_RABUTT2:
    { slr=1;
    CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT2);
    SetDlgItemText(hDlg,IDOK,"Okay ReBoot");break;
    }
    case IDOK:
    { Okay=0;
    GetDlgItemText(hDlg,IDC_TARGET,Targ,42);
    GetDlgItemText(hDlg,IDC_WHY,Message,130);
    when=GetDlgItemInt(hDlg,IDC_WHEN,NULL,FALSE);
    if(strcmp(Targ,"Okay Who Dies??")==0)
    {
    GetUserName(UserName,&useBuff);
    sprintf(TitleBar,"Hello!? %s",UserName);
    MessageBox(0,"You MUST Select a Target First.",TitleBar,MB_OK);
    break;
    }
    ErrorCheck();
    if(Okay==0)
    {
    RebootSystem();
    EndDialog(hDlg,0);
    }
    if(Okay==2)RebootSystem();
    if(slr==0)SetDlgItemText(hDlg,IDC_ABORT,"Abort ShutDown");
    if(slr==1)SetDlgItemText(hDlg,IDC_ABORT,"Abort ReBoot");
    break;
    }
    case IDC_ABORT:
    {
    AborT(); break;
    }
    case IDCANCEL:
    {
    EndDialog(hDlg,0);
    }
    }
    }
    return FALSE;
    }
    [/CODE]
  • Anjuna MoonAnjuna Moon Member Posts: 89
    Hi, thanks a million. I needed to get this up and running quickly and you made it possible.
    Cheers
    /Chris


    : : Hi, I'm looking for a way to remotely reboot a series of servers on our LAN. I haven't found a way to do this via the API. If there is an API function that does this, would you please let me know. (We have both NT4's and W2K's on the LAN)
    : :
    : : Regards
    : : Chris
    : :
    :
    :
    : Greetings
    : Granted this is a bit (Um...)unpolished But it's the code for something I was playing with a while back for remote reboot/shutdown.
    :
    : Hope it's usefull
    :
    : [CODE]
    : #include "stdafx.h"
    : #include "shlobj.h"
    : #include "stdio.h"
    : #include "commctrl.h"
    : #include "resource.h"
    :
    : int slr=0;
    : int Okay=0;
    : int when=42;
    : char Targ[42];
    : char Message[130];
    : HWND hDlg;
    : char szFile[MAX_PATH];
    : DWORD cchBuff=256;
    : DWORD useBuff=256;
    : char UserName[120];
    : char CompName[MAX_COMPUTERNAME_LENGTH];
    : char TitleBar[200];
    : char MessageW[1024];
    : HINSTANCE hInst;
    : BOOL CALLBACK DlgProc(HWND,UINT,WPARAM,LPARAM);
    : int APIENTRY WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
    : {
    : DialogBoxParam(hInst,MAKEINTRESOURCE(IDD_DIALOG1),NULL,(DLGPROC)DlgProc,SW_SHOW);
    : return TRUE;
    : }
    : //**********Well Here Goes...*******************************************
    : void RebootSystem()
    : {
    : HANDLE hToken;
    : TOKEN_PRIVILEGES tkp;
    : BOOL fResult;
    :
    : if(!OpenProcessToken(GetCurrentProcess(),
    : TOKEN_ADJUST_PRIVILEGES|
    : TOKEN_QUERY,&hToken))
    : MessageBox(0,"Can't Open Process TOKEN","ERROR",MB_OK);
    :
    : LookupPrivilegeValue(Targ,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    : tkp.PrivilegeCount=1;
    : tkp.Privileges[0].Attributes=SE_PRIVILEGE_ENABLED;
    : AdjustTokenPrivileges(hToken,FALSE,&tkp,0,(PTOKEN_PRIVILEGES)NULL,0);
    : if(GetLastError() != ERROR_SUCCESS) MessageBox(0,"Can't Adjust TOKEN Privileges","ERROR",MB_OK);
    : if(slr==0){
    : fResult = InitiateSystemShutdown(Targ,
    : Message,
    : when,
    : TRUE,
    : FALSE);
    : }
    : if(slr==1){
    : fResult = InitiateSystemShutdown(Targ,
    : Message,
    : when,
    : TRUE,
    : TRUE);
    : }
    : if(!fResult)MessageBox(0,"InitiateSystemShutdown FAILED","ERROR",MB_OK);
    : }
    : //??????????????????????????????????????????????????????????????????????
    : LPITEMIDLIST PidlBrowse(int nCSIDL)
    : {
    : LPITEMIDLIST pidlRoot = NULL;
    : LPITEMIDLIST pidlSelected = NULL;
    : BROWSEINFO bi;// = {0};
    : LPMALLOC pMalloc = NULL;
    :
    : SHGetMalloc(&pMalloc);
    : SHGetFolderLocation(0, nCSIDL, NULL, NULL, &pidlRoot);
    :
    : bi.hwndOwner = hDlg;
    : bi.pidlRoot = pidlRoot;
    : bi.pszDisplayName = szFile;
    : bi.lpszTitle = "Find (TargeT) Computer";
    : bi.ulFlags = BIF_BROWSEFORCOMPUTER;
    : bi.lpfn = NULL;
    : bi.lParam = 0;
    :
    : pidlSelected = SHBrowseForFolder(&bi);
    :
    : pMalloc->Free(pidlRoot);
    : pMalloc->Release();
    :
    : return pidlSelected;
    : }
    : //??????????????????????????????????????????????????????????????????????
    : //**********************************************************************
    : /* void PokeAndHope()
    : {
    : BROWSEINFO bi;
    : bi.hwndOwner = hDlg;
    : bi.pidlRoot = NULL;
    : bi.pszDisplayName = szFile;
    : bi.lpszTitle = "Find (TargeT) Computer";
    : bi.ulFlags = BIF_BROWSEFORCOMPUTER;
    : bi.lpfn = NULL;
    : SHBrowseForFolder(&bi);
    : } */
    : //**********************************************************************
    : //(((((((((((((((((((( ERROR CHECK (((((((((((((((((((((((((((((((((((((
    : void ErrorCheck()
    : {
    : GetComputerName(CompName,&cchBuff);
    : GetUserName(UserName,&useBuff);
    : if((strcmpi(Targ,"")==0)||(strcmpi(Targ,CompName)==0))
    : {
    : sprintf(TitleBar,"WARNING %s",UserName);
    : if(slr==0)sprintf(MessageW,"Your about to ShutDown %s(LocalMachine) in %d Seconds!!",CompName,when);
    : if(slr==1)sprintf(MessageW,"Your about to ReBoot %s(LocalMachine) in %d Seconds!!",CompName,when);
    : if(MessageBox(0,MessageW,TitleBar,MB_OKCANCEL)==IDCANCEL)Okay=1;
    : }
    : else{
    : sprintf(TitleBar,"Confirmation");
    : if(slr==0)sprintf(MessageW,"ShutDown %s in %d Seconds?",Targ,when);
    : if(slr==1)sprintf(MessageW,"ReBoot %s in %d Seconds?",Targ,when);
    : if(MessageBox(0,MessageW,TitleBar,MB_OKCANCEL)==IDCANCEL)Okay=1;
    : else Okay=2;
    : }
    : }
    : //)))))))))))))))))) END ERROR CHECK )))))))))))))))))))))))))))))))))))
    : //>>>>>>>>>>>>>>>>>>>>>>>> ABORT >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    : HANDLE hToken;
    : TOKEN_PRIVILEGES tkp;
    : BOOL fResult;
    :
    : void AborT()
    : {
    : if (!OpenProcessToken(GetCurrentProcess(),
    : TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
    : {
    : MessageBox(0,"Can't Open Process TOKEN","ERROR",MB_OK);
    : }
    : //........................................................
    : if(strcmpi(Targ,"")==0)MessageBox(0,"Can't Abort, No Name Specified!","ERROR",MB_OK);
    : if(strcmpi(Targ,CompName)==0)
    : {
    : LookupPrivilegeValue(Targ,SE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    : }
    : else{
    : LookupPrivilegeValue(Targ,SE_REMOTE_SHUTDOWN_NAME,&tkp.Privileges[0].Luid);
    : }
    : //........................................................
    : tkp.PrivilegeCount = 1;
    : tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    :
    : AdjustTokenPrivileges(hToken, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES)NULL, 0);
    :
    : if(GetLastError() != ERROR_SUCCESS)
    : {
    : MessageBox(0,"Can't Adjust TOKEN Privileges","ERROR",MB_OK);
    : }
    :
    : fResult = AbortSystemShutdown(Targ);
    : if(!fResult)
    : {
    : MessageBox(0,"Abort ShutDown FAILED","ERROR",MB_OK);
    : }
    : }
    : //<<<<<<<<<<<<<<<<<<<<<< END ABORT <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
    : BOOL CALLBACK DlgProc(HWND hDlg,UINT uMsg,WPARAM wParam,LPARAM)
    : {
    : switch(uMsg)
    : {
    : case WM_INITDIALOG:
    : { slr=0;
    : CreateUpDownControl(
    : WS_CHILD|WS_VISIBLE|UDS_SETBUDDYINT,
    : 240,49,17,19,hDlg,IDC_SPIN1,hInst,
    : GetDlgItem(hDlg,IDC_WHEN),999,0,42);
    : CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT1);
    : SetFocus(GetDlgItem(hDlg,IDC_TARGET));
    : SetDlgItemText(hDlg,IDC_TARGET,"Okay Who Dies??");
    : SendMessage(GetDlgItem(hDlg,IDC_TARGET), EM_SETSEL, 0,-1);
    : }
    : case WM_COMMAND:
    : switch(wParam)
    : {
    : case IDC_BROWSE:
    : {
    : PidlBrowse(CSIDL_NETWORK);
    : //PokeAndHope();
    : SetDlgItemText(hDlg,IDC_TARGET,szFile);
    : break;
    : }
    : case IDC_RABUTT1:
    : { slr=0;
    : CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT1);
    : SetDlgItemText(hDlg,IDOK,"Okay ShutDown");break;
    : }
    : case IDC_RABUTT2:
    : { slr=1;
    : CheckRadioButton(hDlg,IDC_RABUTT1,IDC_RABUTT2,IDC_RABUTT2);
    : SetDlgItemText(hDlg,IDOK,"Okay ReBoot");break;
    : }
    : case IDOK:
    : { Okay=0;
    : GetDlgItemText(hDlg,IDC_TARGET,Targ,42);
    : GetDlgItemText(hDlg,IDC_WHY,Message,130);
    : when=GetDlgItemInt(hDlg,IDC_WHEN,NULL,FALSE);
    : if(strcmp(Targ,"Okay Who Dies??")==0)
    : {
    : GetUserName(UserName,&useBuff);
    : sprintf(TitleBar,"Hello!? %s",UserName);
    : MessageBox(0,"You MUST Select a Target First.",TitleBar,MB_OK);
    : break;
    : }
    : ErrorCheck();
    : if(Okay==0)
    : {
    : RebootSystem();
    : EndDialog(hDlg,0);
    : }
    : if(Okay==2)RebootSystem();
    : if(slr==0)SetDlgItemText(hDlg,IDC_ABORT,"Abort ShutDown");
    : if(slr==1)SetDlgItemText(hDlg,IDC_ABORT,"Abort ReBoot");
    : break;
    : }
    : case IDC_ABORT:
    : {
    : AborT(); break;
    : }
    : case IDCANCEL:
    : {
    : EndDialog(hDlg,0);
    : }
    : }
    : }
    : return FALSE;
    : }
    : [/CODE]
    :

  • Shawn CarterShawn Carter Member Posts: 0

    _ \ http://forcoder.org \ free ebooks and video tutorials about ( Objective-C, Ruby, JavaScript, PL/SQL, C#, Java, MATLAB, PHP, R, Visual Basic, C, C++, Swift, Perl, Visual Basic .NET, Scratch, Assembly, Go, Delphi, Python F#, Kotlin, D, Logo, Fortran, Lisp, Ada, Scheme, VBScript, COBOL, Scala, Erlang, Transact-SQL, SAS, Clojure, Prolog, Crystal, FoxPro, Julia, LabVIEW, Awk, Dart, Apex, Alice, ML, Rust, Bash, Hack, ABAP, Lua ) _

Sign In or Register to comment.