-
May 26th, 2017, 10:22 AM
#1
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!
-
May 26th, 2017, 10:29 AM
#2
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.2)
-
May 26th, 2017, 11:11 AM
#3
Re: Auto updater WinApi
 Originally Posted by 2kaud
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.
-
May 26th, 2017, 11:19 AM
#4
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.2)
-
May 26th, 2017, 11:39 AM
#5
Re: Auto updater WinApi
 Originally Posted by 2kaud
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.
-
May 26th, 2017, 12:56 PM
#6
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.2)
-
May 26th, 2017, 01:06 PM
#7
Re: Auto updater WinApi
I have seen the first link, and I just copy pasted the answer code, and it still does not work
-
May 26th, 2017, 01:44 PM
#8
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.2)
-
May 26th, 2017, 01:55 PM
#9
Re: Auto updater WinApi
 Originally Posted by 2kaud
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
-
May 26th, 2017, 02:30 PM
#10
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.2)
-
May 26th, 2017, 02:43 PM
#11
Re: Auto updater WinApi
 Originally Posted by 2kaud
Test code works OK for me on Windows 10 
That's really strange, don't really know what to do
-
May 26th, 2017, 03:08 PM
#12
Re: Auto updater WinApi
 Originally Posted by mandruk1331
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?
-
May 26th, 2017, 04:06 PM
#13
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.2)
-
May 27th, 2017, 05:48 PM
#14
Re: Auto updater WinApi
 Originally Posted by Arjay
What type of test program do you have? Is it a console app? A windows app?
The project type is Win32Console
-
May 29th, 2017, 07:04 AM
#15
Re: Auto updater WinApi
 Originally Posted by 2kaud
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
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|