CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    Join Date
    May 2017
    Posts
    13

    Auto updater WinApi

    Hello!
    I'm trying to write an auto updater for my exe file, but I have face a problem when I trigger the auto update I want to remove the old version of my program by running an uninstaller, and here is the problem I can't do that because I need the AdminRights, is there a way I trigger the UAC window to prompt the users permission? or maybe there is another way of bypassing the UAC (giving addition rights for the process)?
    I use the CreateProcess function and it is vital to use it only, because I will need the handle of the created process. I tried to use the ShellExecute function it worked ok, everything run nicely but I could not get the handle for the process, the WaitForSingleObject function did not want to work with the ShellExecute, therefore I switched to CreateProcess, so In advance I need a smth like ShellExecute but with CreateProcess on board.
    Thank you!

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    I need a smth like ShellExecute... I will need the handle of the created process
    Have a look at ShellExecuteEx(). https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx. This uses a SHELLEXECUTEINFO structure which has as one of its members a handle to the newly created process.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by 2kaud View Post
    Have a look at ShellExecuteEx(). https://msdn.microsoft.com/en-us/lib...(v=vs.85).aspx. This uses a SHELLEXECUTEINFO structure which has as one of its members a handle to the newly created process.
    Yes I have fount this function but the WaitForSingleObject function does not wait for the process to be finished which is not good.
    Code:
    ShellExecuteEx(&ShellInfo);
    WaitForSingleObject(ShellInfo.hInstApp, INFINITE);
    CloseHandle(ShellInfo.hProcess);
    Last edited by 2kaud; May 26th, 2017 at 11:15 AM.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    Code:
    WaitForSingleObject(ShellInfo.hInstApp, INFINITE);
    hInstApp is not a true HINSTANCE but is really a return code. Try
    Code:
    WaitForSingleObject(ShellInfo.hProcess, INFINITE);
    Note that fMask member needs to include SEE_MASK_NOCLOSEPROCESS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by 2kaud View Post
    Code:
    WaitForSingleObject(ShellInfo.hInstApp, INFINITE);
    hInstApp is not a true HINSTANCE but is really a return code. Try
    Code:
    WaitForSingleObject(ShellInfo.hProcess, INFINITE);
    Note that fMask member needs to include SEE_MASK_NOCLOSEPROCESS
    The problem is still present
    The setup for ShellExecuteEx, and maybe this could be the issue, but from my main program I create an instance of the auto-updater with CreateProcess, and it does wait. I know that inside the auto-updater does not work because it calls uninstall and instanlty install exe, the WaitForSingleObject returns 0.

    Code:
    SHELLEXECUTEINFO ShellInfo = { 0 };
    ShellInfo.cbSize = sizeof(SHELLEXECUTEINFOA);
    ShellInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShellInfo.hwnd = NULL;
    ShellInfo.lpVerb = L"runas";
    ShellInfo.lpFile = L"path_to_installer.exe"; 
    ShellInfo.lpParameters = L"";
    ShellInfo.lpDirectory = L"path_to_installer's_folder";
    ShellInfo.nShow = SW_NORMAL;
    Last edited by 2kaud; May 26th, 2017 at 01:38 PM.

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    I have seen the first link, and I just copy pasted the answer code, and it still does not work

  8. #8
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    This test program works as expected. The message is displayed once the supplied command (testsh.exe) has completed execution after the User Account Control message window appears.

    Code:
    #include <Windows.h>
    #include <Shellapi.h>
    #include <iostream>
    
    int main()
    {
    	SHELLEXECUTEINFO ShExecInfo = { 0 };
    	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    	ShExecInfo.hwnd = NULL;
    	ShExecInfo.lpVerb = "runas";
    	ShExecInfo.lpFile = "testsh.exe";
    	ShExecInfo.lpParameters = "";
    	ShExecInfo.lpDirectory = NULL;
    	ShExecInfo.nShow = SW_SHOW;
    	ShExecInfo.hInstApp = NULL;
    	ShellExecuteEx(&ShExecInfo);
    	WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
    
    	std::cout << "after wait" << std::endl;
    }
    PS Tested on Windows 7
    Last edited by 2kaud; May 26th, 2017 at 01:48 PM. Reason: PS
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  9. #9
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by 2kaud View Post
    This test program works as expected. The message is displayed once the supplied command (testsh.exe) has completed execution after the User Account Control message window appears.

    Code:
    #include <Windows.h>
    #include <Shellapi.h>
    #include <iostream>
    
    int main()
    {
    	SHELLEXECUTEINFO ShExecInfo = { 0 };
    	ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    	ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    	ShExecInfo.hwnd = NULL;
    	ShExecInfo.lpVerb = "runas";
    	ShExecInfo.lpFile = "testsh.exe";
    	ShExecInfo.lpParameters = "";
    	ShExecInfo.lpDirectory = NULL;
    	ShExecInfo.nShow = SW_SHOW;
    	ShExecInfo.hInstApp = NULL;
    	ShellExecuteEx(&ShExecInfo);
    	WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
    
    	std::cout << "after wait" << std::endl;
    }
    PS Tested on Windows 7

    I have just tested the code snippet on W7 and it works good, but on Windows 10 the WaitForSingleObject is ignored

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    Test code works OK for me on Windows 10
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by 2kaud View Post
    Test code works OK for me on Windows 10
    That's really strange, don't really know what to do

  12. #12
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Auto updater WinApi

    Quote Originally Posted by mandruk1331 View Post
    That's really strange, don't really know what to do
    What type of test program do you have? Is it a console app? A windows app?

  13. #13
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Auto updater WinApi

    For my test program of post #8, this is a console prog and so is my testsh.exe that it calls.

    PS Also works as expected with my Windows 7 and Windows 10 when testsh.exe is a windows program.
    Last edited by 2kaud; May 26th, 2017 at 04:15 PM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  14. #14
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by Arjay View Post
    What type of test program do you have? Is it a console app? A windows app?
    The project type is Win32Console

  15. #15
    Join Date
    May 2017
    Posts
    13

    Re: Auto updater WinApi

    Quote Originally Posted by 2kaud View Post
    For my test program of post #8, this is a console prog and so is my testsh.exe that it calls.

    PS Also works as expected with my Windows 7 and Windows 10 when testsh.exe is a windows program.
    Ok, I now have an error after the WaitForSingleObject function, the GetLastError function returns 6 (ERROR_INVALID_HANDLE), but the process is creating, what can be the problem? if the handle was not good, then the process would not be created

Page 1 of 2 12 LastLast

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured