31

I'm looking for a command in windows cmd to tell me if a certain dll file is 32 bit or 64 bit

Is there something like this in windows ?

becks
  • 2,656
  • 8
  • 35
  • 64
  • How can I test a windows dll to determine if it is 32bit or 64bit? http://stackoverflow.com/questions/495244/how-can-i-test-a-windows-dll-to-determine-if-it-is-32bit-or-64bit –  Jan 28 '13 at 11:32
  • Thanks, I checked this but I needed a windows command not a script. – becks Jan 28 '13 at 11:39
  • `dumpbin` and `cygwin file.exe` are mentioned there also –  Jan 28 '13 at 12:45
  • 2
    Does this answer your question? [How to find if a native DLL file is compiled as x64 or x86?](https://stackoverflow.com/questions/480696/how-to-find-if-a-native-dll-file-is-compiled-as-x64-or-x86) – StayOnTarget May 20 '21 at 13:16

4 Answers4

35

DUMPBIN is included with Visual C++ and can provide this information with the /HEADERS switch.

Example output from a 32-bit image:

FILE HEADER VALUES
     14C machine (i386)
       6 number of sections
306F7A22 time date stamp Sun Oct 01 22:35:30 1995
       0 file pointer to symbol table
     1D1 number of symbols
      E0 size of optional header
     302 characteristics
            Executable
            32 bit word machine
            Debug information stripped
Jon
  • 428,835
  • 81
  • 738
  • 806
  • Thanks you for that, It worked for windows with Visual C++ SDK installed. but I'm targeting a machine with no SDK installed. is there another command ?! – becks Jan 28 '13 at 11:38
  • @becks: There's nothing built-in. You will have to provide an implementation, be it your own or something like `dumpbin`. – Jon Jan 28 '13 at 11:42
4

If you have installed 7zip :

"C:\Program Files\7-Zip\7z.exe" l "my-program.exe" | findstr CPU
P. Debrabant
  • 131
  • 1
  • 13
3

You can use the dbghelp library to obtain the image headers. Then you can read the information you need out of the FileHeader.

Here's some sample code. Please forgive the rather lame error handling. I just knocked it up quickly to illustrate, and I'm not even remotely fluent in C++.

#include <Windows.h>
#include <Dbghelp.h>

#include <string>
#include <iostream>

using namespace std;

bool GetImageFileHeaders(wstring fileName, IMAGE_NT_HEADERS &headers)
{
    HANDLE fileHandle = CreateFile(
        fileName.c_str(),
        GENERIC_READ,
        FILE_SHARE_READ,
        nullptr,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        0
    );
    if (fileHandle == INVALID_HANDLE_VALUE)
        return false;

    HANDLE imageHandle = CreateFileMapping(
        fileHandle,
        nullptr,
        PAGE_READONLY,
        0,
        0,
        nullptr
    );
    if (imageHandle == 0)
    {
        CloseHandle(fileHandle);
        return false;
    }

    void *imagePtr = MapViewOfFile(
        imageHandle,
        FILE_MAP_READ,
        0, 
        0,
        0
    );
    if (imagePtr == nullptr)
    {
        CloseHandle(imageHandle);
        CloseHandle(fileHandle);
        return false;
    }
    
    PIMAGE_NT_HEADERS headersPtr = ImageNtHeader(imagePtr);
    if (headersPtr == nullptr)
    {
        UnmapViewOfFile(imagePtr);
        CloseHandle(imageHandle);
        CloseHandle(fileHandle);
        return false;
    }

    headers = *headersPtr;

    UnmapViewOfFile(imagePtr);
    CloseHandle(imageHandle);
    CloseHandle(fileHandle);

    return true;
}

int main(int argc, char* argv[])
{
    IMAGE_NT_HEADERS headers;
    if (GetImageFileHeaders(L"C:\\windows\\system32\\user32.dll", headers))
    {
        if (headers.FileHeader.Machine == IMAGE_FILE_MACHINE_I386)
            cout << "x86";
        else if (headers.FileHeader.Machine == IMAGE_FILE_MACHINE_IA64)
            cout << "IA64";
        else if (headers.FileHeader.Machine == IMAGE_FILE_MACHINE_AMD64)
            cout << "x64";
        else
            cout << "Machine not recognised";
    }
    return 0;
}

To link this you need to add dbghelp.lib to the additional dependencies of your project. To learn more about the details behind this, refer to the MSDN documentation for the various API calls that are used.

phuclv
  • 37,963
  • 15
  • 156
  • 475
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
1

The capability you are looking for is available natively on UNIX systems; use the 'file' command. You can use 'file' on Windows systems if you install Cygwin or one of the other UNIX emulators.

Chris Golledge
  • 350
  • 4
  • 7