---
title: "[Windows] 如何检测32位或64位的文件？"
author: "ray_linn"
date: "2010-08-16T17:29:22Z"
tags: []
categories: ["编程语言"]
slug: "windows-32-64"
draft: false
lastmod: "2010-08-16T17:29:22Z"
id: 738597
url: "https://raylinn.iteye.com/blog/738597"
---

这几天在搞64位的windows编程，有时候需要知道所使用的dll或者lib是32 bit或者 64bit的  （64 bit的程序无法调用32 bit的lib或dll)，最简单的方法是用windows下的工具dumpbin：


```

dumpbin /headers <filename>

```


开头几行就显示了这个文件的target machine，比如

```

PE signature found

File Type: DLL

FILE HEADER VALUES
             14C machine (x86)
               1 number of sections
        45499E0A time date stamp Thu Nov 02 03:28:10 2006
               0 file pointer to symbol table
               0 number of symbols
              E0 size of optional header
            2102 characteristics
                   Executable
                   32 bit word machine
                   DLL

OPTIONAL HEADER VALUES
             10B magic # (PE32)

```


或者，你也可以用这段perl代码：

```

#!/usr/bin/perl 
# 
# usage: petype <exefile> 
# 
$exe = $ARGV[0]; 
 
open(EXE, $exe) or die "can't open $exe: $!"; 
binmode(EXE); 
if (read(EXE, $doshdr, 68)) { 
 
   ($magic,$skip,$offset)=unpack('a2a58l', $doshdr); 
   die("Not an executable") if ($magic ne 'MZ'); 
 
   seek(EXE,$offset,SEEK_SET); 
   if (read(EXE, $pehdr, 6)){ 
       ($sig,$skip,$machine)=unpack('a2a2v', $pehdr); 
       die("No a PE Executable") if ($sig ne 'PE'); 
 
       if ($machine == 0x014c){ 
            print "i386\n"; 
       } 
       elsif ($machine == 0x0200){ 
            print "IA64\n"; 
       } 
       elsif ($machine == 0x8664){ 
            print "AMD64\n"; 
       } 
       else{ 
            printf("Unknown machine type 0x%lx\n", $machine); 
       } 
   } 
} 
 
close(EXE); 

```