7

I tried a lot of things but for some reason I could not get things working. I am trying to run dumpbin utility of MS VS using a Python script.

Here are what I tried (and what did not work for me)

1.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w')
command = '"C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin" /EXPORTS ' + dllFilePath
process = subprocess.Popen(command, stdout=tempFile)
process.wait()
tempFile.close()

2.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w')
command = 'C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin /EXPORTS ' + dllFilePath
process = subprocess.Popen(command, stdout=tempFile)
process.wait()
tempFile.close()

3.

tempFile = open('C:\\Windows\\temp\\tempExports.txt', 'w')
process = subprocess.Popen(['C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin', '/EXPORTS', dllFilePath], stdout = tempFile)
process.wait()
tempFile.close()

does anyone have any idea on doing what i am trying to do (dumpbin /EXPORTS C:\Windows\system32\kernel32.dll > tempfile.txt) correctly in Python?

Hayri Uğur Koltuk
  • 2,970
  • 4
  • 31
  • 60

2 Answers2

8

The argument pattern for Popen expect a list of strings for non-shell calls and a string for shell calls. This is easy to fix. Given:

>>> command = '"C:/Program Files/Microsoft Visual Studio 8/VC/bin/dumpbin" /EXPORTS ' + dllFilePath

Either call subprocess.Popen with shell=True:

>>> process = subprocess.Popen(command, stdout=tempFile, shell=True)

or use shlex.split to create an argument list:

>>> process = subprocess.Popen(shlex.split(command), stdout=tempFile)
Raymond Hettinger
  • 216,523
  • 63
  • 388
  • 485
2
with tempFile:
    subprocess.check_call([
        r'C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe',
        '/EXPORTS', 
        dllFilePath], stdout=tempFile)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • ok now we are sure that it does not work :) subprocess.CalledProcessError: Command '['C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin.exe', '/EXPORTS', 'C:\\Windows\\system32\\user32.dll']' returned non-zero exit status -1073741515 – Hayri Uğur Koltuk Oct 20 '11 at 06:58
  • when i do this: subprocess.check_call('"C:\\Program Files\\Microsoft Visual Studio 8\\VC\\bin\\dumpbin.exe" /EXPORTS ' + dllFilePath, stdout=tempFile, shell=True) the exit code is the same, but after debugging i found out that dumpbin (or cmd.exe) displays the message: 'The filename, directory name, or volume label syntax is incorrect.' and debugging showed that when shell=True the command that is executed is: C:\WINDOWS\system32\cmd.exe /c ""\"C:\Program Files\Microsoft Visual Studio 8\VC\bin\dumpbin.exe\" /EXPORTS C:\Window\system32\user32.dll"" – Hayri Uğur Koltuk Oct 20 '11 at 07:55