Run the Qt application as administrator on Windows - qt

Is there a way I can run the Qt application as an administrator? I have an auto-updater for my application. It needs administrator privileges to replace the files in Program Files folder and hence it requires administrator privileges.

Running your application with administrator privileges does not have a whole lot to do with Qt. There are two approaches.
The "simple" one is to manually set your application to run with administrator privileges. You can do so by right-clicking on the executable. Then on the "Compatibilty" tab, you can choose to "Run this application as an administrator" under "Privilege level".
However, if you automatically want to achieve the same, you will have to embed a manifest into your application. What you're looking for is to set the requestedExecutionLevel to requireAdministrator. A bit more information can be found on MSDN or in this Wikipedia entry on UAC.
For your application as built in Qt Creator, it means you will need to embed the manifest by including a reference to it in a Resource (.rc) file. This resource file can then be added to your .pro file by specifying RC_FILE = myapp.rc. An informative blog post on this very issue is this one, as well as this post on the QtCentre forum.

A very simple solution for this, if you're using MSVC toolkit, is to add the following into the project file:
QMAKE_LFLAGS_WINDOWS += "/MANIFESTUAC:\"level='requireAdministrator' uiAccess='false'\""
I am using Qt 5.12 msvc2017.
I've found this to be quite neat, as from what I see in generated Makefile, Qt is already adding some manifest related link flags, and this approach wouldn't interfere with already embedding manifest, as manually adding manifest from existing file.
Other manifest link options can be easily added. You can read the docs for VS compiler, and/or you can check what flags/options Visual Studio IDE has to offer in Project properties/Linker/Manifest File and then check Command Line section of the Linker to see how it adds them.
Not sure how would this be done in gcc or clang builds, it would probably require solutions provided by #H Aßdøµ, and #Bart.

From the article that referred to Mr #Bart:
Application Manifest
First, we have to prepare an application manifest file. This one below is for application that does not require administrator rights:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity version="2.0.2.0" processorArchitecture="X86" type="win32"/>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
<security>
<requestedPrivileges>
<requestedExecutionLevel level="asInvoker" uiAccess="false"/>
</requestedPrivileges>
</security>
</trustInfo>
</assembly>
Second, we need the MT.exe tool from the Microsoft Windows SDK to embed this XML in our executable. To do it use the following command:
mt.exe –manifest MyApp.exe.manifest -outputresource:MyApp.exe;1
Automatic Manifest Embedding
Manually executing the mt command after each compilation is a tedious task. What about convincing qmake to do it for us? After studying the docs it looks like the following line should do the trick:
win32 {
WINSDK_DIR = C:/Program Files (x86)/Microsoft SDKs/Windows/v7.0A
WIN_PWD = $$replace(PWD, /, \\)
OUT_PWD_WIN = $$replace(OUT_PWD, /, \\)
QMAKE_POST_LINK = "$$WINSDK_DIR/bin/x64/mt.exe -manifest $$quote($$WIN_PWD\\$$basename(TARGET).manifest) -outputresource:$$quote($$OUT_PWD_WIN\\${DESTDIR_TARGET};1)"
}
The above code will automatically execute the mt.exe program from WINSDK_DIR and embed a manifest file that is located in the project root directory and named after project's target (ie. MyApp.manifest). That's all to adding a manifest, now let's move on and specify the version information.
Orginal post: http://blog.strixcode.com/2010/08/embedding-application-manifest-and.html

Related

Can .Net Core 3 self-contained single executable be decompiled?

I tried using Dotpeek and ILSpy.Net to decompile (my own code), they failed.
Do I need special obfuscation on distributed binaries of .Net Core 3 self-contained single executable ?
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<PublishTrimmed>true</PublishTrimmed>
<PublishReadyToRun>true</PublishReadyToRun>
<PublishSingleFile>true</PublishSingleFile>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
</PropertyGroup>
</Project>
The single-file exe is really an unmanaged wrapper and ILSpy doesn't support decompiling this. But when you run the exe, it unwraps its contents to a temp folder. So you can find the managed dll there and decompile it using ILSpy.
To find the temp folder, you can use any tool that shows locations of assemblies loaded by a process. SysInternals Process Monitor (procmon) is a good one.
You can setup procmon to filter by your exe name, and when you launch your exe, procmon should show some events for assemblies being loaded from a temp folder:
You can browse to that folder and find your managed dll there. And you can decompile using ILSpy from that location.
I wrote a blog entry: https://eersonmez.blogspot.com/2020/02/ilspy-decompiling-net-core-self.html
I wrote a small dotnet tool after I stumbled upon this question and couldn't find a lightweight tool myself other than ILSpy.
You can install it using the following dotnet command: dotnet tool install -g sfextract.
After installing it, run it using the following command: sfextract application.exe -o output-dir
The bundle format for .NET 5.0 (bundle version 2) is identical to previous versions. .NET 6.0 (bundle version 6) has an additional field for each file entry containing the compressed size, since single-file applications can now be compressed using gzip by setting EnableCompressionInSingleFile to true.
https://www.nuget.org/packages/sfextract/
https://github.com/Droppers/SingleFileExtractor
Update 07/2022: .Net 5 single-file does not automatically unpack to the same temporary location as before. to force it to be unpacked you would need to add the following:
in the project file add these properties (according to theseMicrosoft docs):
<PublishSingleFile>true</PublishSingleFile>
<IncludeAllContentForSelfExtract>true</IncludeAllContentForSelfExtract>
Add an environment variable DOTNET_BUNDLE_EXTRACT_BASE_DIR with the location you want the files extracted to.
Update: One of the announcements made regarding .Net 5 states that the way single-file executables will be made would change, so this method will not work for them.
I wanted to add on #Eren Ersönmez's answer, that while ILSpy DotPeek don't support this at the time, since the self-contained single file is just a wrapper that contains all your DLLs and gets extracted on runtime, simply knowing where it is extracted to can save you using ProcMon or ProExp or windbg.
If you use windows you can go to c:\Users\{Local Username}\AppData\local\temp\.net\{Name of executable}
which should lead to somewhere similar to
c:\Users\alenros\AppData\Local\Temp.net\MyTestApplication
Launch your exe, and a folder with the same name will be created in that location.
The folder will contain randomly named folders. open the latest one and there you will find all your extracted DLLs, which can then be decompiled.

Removing comments from web.config on build

Is it possible to remove the commented lines from a web.config on build?
xml transform is fine to remove some elements but I couldn't find any syntax to clean the comments from the file.
We are using TFS 2010 build server for our builds.
<add xdt:Transform="RemoveAll" xdt:Locator="XPath(//comment())" />
Put this node within the root node of your transform file. "XPath(//comment())" selects all XML comment nodes to delete.
UPDATE: See actual and working answer below.
It's not possible to do with xml transformation.
But you can do it with your own console app or msbuild task.
See example code here Remove XML comments using Visual Studio 2010 Web Config Transformation
I had a similar problem where I wanted to remove dev comments from the config files before I published to the web site. I wrote an app that will recursively remove comments from config files in the directory I specify on the command line. The sample below assumes YourCommentRemover will do the same.
I included the comment remover project as part of my solution and referenced it in the web app I plan to deploy. You can just add the executable as a reference if you want. Since I didn't want the comment remover to get deployed, I added a task to delete it from the bin directory where it was being staged for deploy, (ProjectDir)obj\$(Configuration)\Package\PackageTmp\.
Open your project file in a text editor (You can right-click on the project file in the solution explorer and select 'Edit Project File').
Go to the very end of the project file and insert the following before </Project>:
<Target Name="BeforePublish" BeforeTargets="MSDeployPublish">
<Exec Command="$(ProjectDir)bin\YourCommentRemover $(ProjectDir)obj\$(Configuration)\Package\PackageTmp" />
<Exec Command="del $(ProjectDir)obj\$(Configuration)\Package\PackageTmp\bin\YourCommentRemover.*" />
</Target>
This target will run before any files are copied to the web application location on publish.

mt.exe in Qt StandAlone .exe

I finally built a static version of Qt 5.1.1 using microsoft visual studio. I created my .exe standalone file using this code:
qmake Hello.pro
nmake release
cd release
mt.exe -manifest Hello.exe.manifest -outputresource: Hello.exe;1
what is mt.exe and what does the last line do with the "Hello.exe" file?
You could just use CONFIG += embed_manifest_exe though, but in essence you need to put the manifest file beside your executable and the last line seems to take care of that.
That is, it is adding a manifest to your "Hello.exe" executable file.
If you do not happen to know what manifest files are, then you can read the MSDN documentation below, but in short: they are carrying run time information for your executable in this particular case:
http://msdn.microsoft.com/en-us/library/aa374191(v=vs.85).aspx

How To Run MSBuild scripts in .wixproj?

Im trying to learn to make a web installer using Windows Installer XML (WIX 3.5). I found this blog about using msbuild in .wixproj files to avoid the scenario where the installer ends up dropping the web project assemblies right in the root of the app instead of keeping them in the bin folder like they're supposed to be.
Here is the link to that:
<http://www.paraesthesia.com/archive/2010/07/30/how-to-consume-msdeploy-staged-web-site-output-in-a.aspx>
But after adding the MSBuild scripts in the .wixproj file, I don't know what to do anymore. According to the instruction after adding the MSBuild script:
"When that target runs, you'll see a .wxs file pop out in the .wixproj project folder. Add the generated .wxs to your .wixproj project so it knows to include it in the build."
I really don7t know what this means. How can I run the target? I tried to build it but there was no .wxs file generated in the .wixproj folder.
Am I missing something? Please help...
Assuming you have added the section from the tutorial:
<Target Name="BeforeBuild">
...
</Target>
The target will be run automatically when you build the project. The "BeforeBuild" target is one of the standard entry-points to add your own modifications to the build. The target will then generate a file (named [WebProjectName].wxs that is placed in the same directory as your wixproj file. Click on the show all files button in visual studio and right-click on the file and "Include in project" That will then include the wxs is your installer and when you next build it will have the correct folder/file structure.

Specifying a flex-config.xml with Maven / Flex Mojos

I have to port an existing project to Maven, and it includes a resource called "config.xml" that is copied to the deploy directory alongside the SWF and HTML, and loaded at run-time to locate a bunch of WSDLs.
Flex Mojos has taken it upon itself to assume that my xml file is a flex-config file with instructions for the compiler, which of course promptly gives up the ghost.
The question is: How do I specify a named config file for the compiler so that Maven stops this nonsense (as well as specifying my compile-time options)?
<configuration>
<configFile>path/to/yourConfigFile.xml</configFile>
</configuration>
https://docs.sonatype.org/display/FLEXMOJOS/compile-swf-mojo.html#compile-swf-mojo.html-configFile
Sorry for the archeology. If you want to hide only some warnings, you can add the following in your pom:
<configuration>
<!-- Maven is more strict than Flash Builder, so I'm hiding some warnings -->
<compilerWarnings>
<warn-no-constructor>false</warn-no-constructor>
</compilerWarnings>
</configuration>
From this documentation:
compilerWarnings
A list of warnings that should be enabled/disabled
Equivalent to -compiler.show-actionscript-warnings,
-compiler.show-binding-warnings,
-compiler.show-shadowed-device-font-warnings,
-compiler.show-unused-type-selector-warnings and -compiler.warn-*
Usage:
<compilerWarnings>
<show-actionscript-warnings>true</show-actionscript-warnings>
<warn-bad-nan-comparison>false</warn-bad-nan-comparison>
</compilerWarnings>

Resources