Programatically determine if native .exe is 32-bit or 64-bit - c++

I need to know whether a given .exe is 32-bit or a 64-bit, before I launch it. IsWow64Process is no use here, since there is no process yet. Is there some other API that will give me this information?

If you really only want to do this for EXEs and not DLLs, just use GetBinaryType.
Determines whether a file is an
executable (.exe) file, and if so,
which subsystem runs the executable
file.

This post will surely help you.
Is C# related but it will give you the idea.

This information is available in one of the headers of the PE File file format (the format used for exe's and dll's). The information in these headers can either be extracted programmatically (they are at a specified offset) or more safely queried via the Win32 API.
Alright, Liviu got the correct pointer for you.

Related

In memory load of .NET PE from native process

I did code a PE packer in C++, which does load an executable into his process space, fixes IAT and relocations, then calls the entry point.
My problem is that .NET is not supported: I did search about .NET PE format, and it is just a 32 bit executable with the entry point set to an imported function (_CorExeMain).
If I call it, windows just popups an error that tells me that I have not .NET installed.
I did also read that .NET PE executables have a special COM header in DataDirectory but I don't know how to handle it.
I hope someone could help me.
Thanks in advance
Obviously the idea you had does not work as-is for .Net.
Your "packed" executable is just a native executable whose code is an unpacker, and whose data is the compressed real executable. When running, Windows expects and gets native code.
For .Net executables, you must obviously do the same: write a .Net executable whose code is an unpacker, and whose data is the compressed .Net bytecode.

Why does QFileInfo::isExecutable() return false for ".msi" files?

I'm currently developing an application using C++ and Qt and I need to know if an input file is executable.
I'm using QFileInfo::isExecutable() which behaves correctly for most files, except for those with the .msi extension, for which it returns false.
How can I figure out for sure if a file is executable or not? I need to do this in Qt for cross-compatibility reasons.
Well, an MSI file isn't actually executable. It's a document format used by the Windows Installer executable. So QFileInfo is correct.
What is the specific problem you need to solve? Why do you think knowing whether a file is executable will help you? What is the goal you have in mind? If you give us more information, we may be able to suggest a better approach.
You should know how QFileInfo::isExecutable() determines a file as executable:
In Windows Qt just checks if the extension of file is .exe, .com and .bat without checking its content.
In Unix-like systems, it checks the file's attribute and returns true for files which have execution permission.
So a .msi file isn't a executable file in view of Qt under Windows.

DLLs for current process

how can one tell (using non-CLR C++) which DLLs is a given running process (by ID) using? With file system paths to those DLLs and EXE.
Thank you in advance.
If you are trying to do this in code, you are probably looking for the EnumProcessModules function (or K32EnumProcessModules depending on operating system. See the link for more details). There is an EnumProcessModulesEx that can give you a little more more information. Simply give it a handle to the process you want to know which modules (DLLs) are loaded. If you don't know the handle, you can find it using EnumProcesses or OpenProcess if you know the PID.

Find out the mime type and associated applications with Qt

How can I find out a mime-type or content-type of a given file?
I cannot use the suffix because the file could be renamed.
Possible additions would be categorizing them as jpg, gif, png and so on are image files and can be opened by editing applications, which has been set in the OS.
Thank you in advance.
What platform? On *nix, you should refer to how the program file does it, which is based on a few heuristics including checks of the first few bytes of a file (many file formats start with a fixed header, including many image formats).
If you're on Windows, the *nix file command is probably still instructive, even if you can't reuse its code as directly. There may also be some better solution in the Windows APIs (I'm not a Windows programmer).
This could help, it is with C# but I think you can get the idea.
http://kseesharp.blogspot.com/2008/04/c-get-mimetype-from-file-name.html
You can use some sort of class for acccesing Windows Registry from qt or using the Windows API directly from qt.
I am not a qt programmer.
You can't, not from within Qt.
However, if all you want is to show a file with the correct application, you can use QDesktopServices::openUrl(), which wraps open (Windows/OSX) and xdg-open (Unix). The URL may also be a local file (in that case, use QUrl::fromLocalFile() to construct the URL).
For categorizing image files. For Qt and just only for image files.
QByteArray imageFormat = QImageReader::imageFormat(fileName); //Where fileName - path to your file
Further mapping imageFormat to mime-type is not complicable
It doesn't look like Qt offers that capability yet. However, you may want to look into the libmagic library which does what the file and other similar commands do. Assuming the library is maintained properly it will include new MIME types as time passes. From what I can tell it is part of the file tool suite. Under a Debian system do something like this:
sudo apt-get install libmagic-dev
to get the development library and use #include to make use of it.

How to check if path leads to executable file?

I try to create some kind of file browser. I want to know if file under path is executable in a cross-platform way.
How to do such thing with boost::filesystem?
Boost doesn't have stuff about permissions, because POSIX permissions are not "crossplatform".
Use the platform-specific APIs at your disposal as required. Sorry!
You can try QT. It is cross-platform. You do not have to care about the operating system differences while dealing with files. What you mean by "executable" is somehow unclear though. If you are talking about file permissions, OT can give this kind of information (Just look at QFile class documentation). If you want to learn whether you can actually run it or not, you have to have some kind of file extension convention. For example, .exe in Windows. I do not know, may be there is a way to look at the initial bits of the file and learn whether it is a binary or not, but I think you will not be able to find a library call for that. You have to implement some platform specific routines for this. If I am not mistaken, file browsers mostly look at the extension of the file to find out the type. For example, if you change the file extension of a pdf to exe than windows explorer sees this file as an executable. Clearly after the file type assumption, it can try to learn some other things about the file, such as icon of the executable. But initially it only looks at the extension. Otherwise, it would be very slow to browse directories containing large numbers of files.
I hope, I gave some relevant information here