Repository: pyinstaller/pyinstaller

Issue:
Fixing 'Cannot Get Architecture from File' Error in 32-bit PyInstaller Compilation on Windows
When I compile the program using pyinstaller on a 32-bit Python, 
it gives the error message "Cannot get architecture from file." After my investigation, 

I found that the `matchDLLArch` function in `bindepend.py` is causing the error. 

When packaging a Win X32 executable on a Win X64 environment, 

the following code needs to be added at the problematic location in `bindepend.py`:

```python
try:
    print("modify-->" + filename)
    filename = filename.replace(r"C:\Windows\system32", r"C:\Windows\System32\downlevel")
    pe = pefile.PE(filename, fast_load=True)
    match_arch = pe.FILE_HEADER.Machine == _exe_machine_type
    pe.close()
except Exception as e:
    print(e)
    raise SystemExit('Cannot get architecture from file: %s\n  Reason: %s' % (pefilename, exc))
```

After adding this code, 

you can successfully compile the program on a 32-bit application and it won't affect the results of 64-bit compilation.


Discussion:
The expression is somewhat ambiguous. 
Within the modifications mentioned above, they are only effective for 32-bit systems. 
If you want them to work for 64-bit systems, you would need to replace them with the original PyInstaller. 
It's possible to consider implementing this modification by utilizing an operating system-based approach to determine the system's architecture. 
However, I didn't incorporate such a method within the provided code. 
My intention was simply to offer a solution for resolving compilation errors in 32-bit programs.
Yes, we need to detect 32-bit python on 64-bit Windows, and adjust the DLL search paths accordingly. 
For the reference, what was the full error message? 

Because even if 32-bit PyInstaller is searching 64-bit system32 directory, it should not raise a `Cannot get architecture from file` error - it should just not match the DLL architecture and thus ignore the DLLs in there. That's why I would like to know which DLL it failed on, and what exception was printed before final error (if any).
I will provide the compiled information after the modifications. This information will provide a more comprehensive view of which specific DLLs are not matching. Here is the information:

```bash
(venv_32) D:\git\public_tool\batch_operate>pyinstaller config_optimize_ui_main.py
3141 INFO: PyInstaller: 5.13.0
3141 INFO: Python: 3.8.10
3152 INFO: Platform: Windows-10-10.0.19045-SP0
3153 INFO: wrote D:\git\public_tool\batch_operate\config_optimize_ui_main.spec
3157 INFO: Extending PYTHONPATH with paths
['D:\\git\\public_tool']
5866 INFO: checking Analysis
5866 INFO: Building Analysis because Analysis-00.toc is non existent
5866 INFO: Initializing module dependency graph...
5871 INFO: Caching module graph hooks...
5911 INFO: Analyzing base_library.zip ...
7678 INFO: Loading module hook 'hook-heapq.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
7840 INFO: Loading module hook 'hook-encodings.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
9658 INFO: Loading module hook 'hook-pickle.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
11842 INFO: Caching module dependency graph...
12059 INFO: running Analysis Analysis-00.toc
12078 INFO: Adding Microsoft.Windows.Common-Controls to dependent assemblies of final executable
  required by D:\Python38-32\python.exe
modify-->C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
12248 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
12248 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
12248 WARNING:   Reason: 'DOS Header magic not found.'
12249 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
12249 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
12249 WARNING:   Reason: 'DOS Header magic not found.'
12250 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
12250 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
12250 WARNING:   Reason: 'DOS Header magic not found.'
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
12263 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
12264 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
12264 WARNING:   Reason: 'DOS Header magic not found.'
12264 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
12264 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
12265 WARNING:   Reason: 'DOS Header magic not found.'
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
12359 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
12360 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
12360 WARNING:   Reason: 'DOS Header magic not found.'
12360 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
12360 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
12361 WARNING:   Reason: 'DOS Header magic not found.'
12361 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
12361 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
12361 WARNING:   Reason: 'DOS Header magic not found.'
12362 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
12363 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
12363 WARNING:   Reason: 'DOS Header magic not found.'
12363 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
12363 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
12364 WARNING:   Reason: 'DOS Header magic not found.'
12366 INFO: Analyzing D:\git\public_tool\batch_operate\config_optimize_ui_main.py
12504 INFO: Loading module hook 'hook-pynput.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
13185 INFO: Processing pre-safe import module hook six.moves from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks\\pre_safe_import_module\\hook-six.moves.py'.
13480 INFO: Loading module hook 'hook-PySide2.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
15861 INFO: Loading module hook 'hook-numpy.py' from 'D:\\git\\public_tool\\venv_32\\Lib\\site-packages\\numpy\\_pyinstaller'...
17905 INFO: Loading module hook 'hook-multiprocessing.util.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
18324 INFO: Loading module hook 'hook-xml.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
19247 INFO: Loading module hook 'hook-difflib.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
19447 INFO: Loading module hook 'hook-platform.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
19820 INFO: Loading module hook 'hook-sysconfig.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
20688 INFO: Loading module hook 'hook-xml.etree.cElementTree.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
23784 INFO: Loading module hook 'hook-charset_normalizer.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
24100 INFO: Loading module hook 'hook-certifi.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
24724 INFO: Loading module hook 'hook-Cryptodome.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\_pyinstaller_hooks_contrib\\hooks\\stdhooks'...
25848 INFO: Processing module hooks...
26109 INFO: Loading module hook 'hook-PySide2.QtCore.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
modify-->C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
26602 INFO: Loading module hook 'hook-PySide2.QtGui.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
modify-->C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
27348 INFO: Loading module hook 'hook-PySide2.QtNetwork.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
28006 INFO: Loading module hook 'hook-PySide2.QtWidgets.py' from 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks'...
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
28933 INFO: Looking for ctypes DLLs
28969 WARNING: Library UIAutomationClient_VC140_X64.dll required via ctypes not found
28971 WARNING: Library UIAutomationClient_VC140_X86.dll required via ctypes not found
29050 INFO: Analyzing run-time hooks ...
29055 INFO: Including run-time hook 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_inspect.py'
29060 INFO: Including run-time hook 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pyside2.py'
29077 INFO: Including run-time hook 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_pkgutil.py'
29093 INFO: Including run-time hook 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\hooks\\rthooks\\pyi_rth_multiprocessing.py'
29121 INFO: Looking for dynamic libraries
1598 INFO: Could not import comtypes.gen, trying to create it.
1599 INFO: Created comtypes.gen directory: 'd:\git\public_tool\venv_32\lib\site-packages\comtypes\gen'
1600 INFO: Writing __init__.py file: 'd:\git\public_tool\venv_32\lib\site-packages\comtypes\gen\__init__.py'
1602 INFO: Using writeable comtypes cache directory: 'd:\git\public_tool\venv_32\lib\site-packages\comtypes\gen'
D:\git\public_tool\venv_32\Lib\site-packages\shiboken2\files.dir\shibokensupport\__feature__.py:142: UserWarning: The numpy.array_api submodule is still experimental. See NEP 47.
  return original_import(name, *args, **kwargs)
2360 INFO: Extra DLL search directories (AddDllDirectory): ['d:\\git\\public_tool\\venv_32\\lib\\site-packages\\shiboken2', 'd:\\git\\public_tool\\venv_32\\lib\\site-packages\\numpy\\.libs']
2360 INFO: Extra DLL search directories (PATH): ['d:\\git\\public_tool\\venv_32\\lib\\site-packages\\PySide2', 'D:\\git\\public_tool\\venv_32\\Scripts', 'C:\\Program Files (x86)\\Common Files\\Oracle\\Java\\javapath', 'C:\\Windows\\system32', 'C:\\Windows', 'C:\\Windows\\System32\\Wbem', 'C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\', 'C:\\Windows\\System32\\OpenSSH\\', 'C:\\Windows\\RCC-Interface', 'D:\\Python38', 'D:\\Python38\\Scripts', 'C:\\Program Files\\Git\\cmd', 'D:\\ffmpeg\\bin', 'D:\\Program Files\\Microsoft VS Code\\bin', 'C:\\Program Files\\TortoiseSVN\\bin', 'D:\\mingw64\\bin', 'C:\\Users\\zhouxihong\\AppData\\Local\\Microsoft\\WindowsApps', 'C:\\Program Files (x86)\\OpenCppCoverage']
2364 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
2365 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-stdio-l1-1-0.dll
2366 WARNING:   Reason: 'DOS Header magic not found.'
2366 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
2366 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-locale-l1-1-0.dll
2367 WARNING:   Reason: 'DOS Header magic not found.'
2367 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
2367 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-math-l1-1-0.dll
2368 WARNING:   Reason: 'DOS Header magic not found.'
modify-->C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
2381 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
2381 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-heap-l1-1-0.dll
2382 WARNING:   Reason: 'DOS Header magic not found.'
2382 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
2383 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-runtime-l1-1-0.dll
2383 WARNING:   Reason: 'DOS Header magic not found.'
modify-->C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
2422 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
2423 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-convert-l1-1-0.dll
2423 WARNING:   Reason: 'DOS Header magic not found.'
2424 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
2424 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-string-l1-1-0.dll
2424 WARNING:   Reason: 'DOS Header magic not found.'
2425 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
2426 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-environment-l1-1-0.dll
2426 WARNING:   Reason: 'DOS Header magic not found.'
2427 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
2427 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-filesystem-l1-1-0.dll
2428 WARNING:   Reason: 'DOS Header magic not found.'
2428 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
2429 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-time-l1-1-0.dll
2429 WARNING:   Reason: 'DOS Header magic not found.'
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
modify-->C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
5457 INFO: Cannot get manifest resource from non-PE file C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
5458 WARNING: Cannot get binary dependencies for file: C:\Windows\system32\api-ms-win-crt-utility-l1-1-0.dll
5458 WARNING:   Reason: 'DOS Header magic not found.'
35816 INFO: Looking for eggs
35821 INFO: Using Python library D:\Python38-32\python38.dll
35821 INFO: Found binding redirects:
[]
35831 INFO: Warnings written to D:\git\public_tool\batch_operate\build\config_optimize_ui_main\warn-config_optimize_ui_main.txt
35954 INFO: Graph cross-reference written to D:\git\public_tool\batch_operate\build\config_optimize_ui_main\xref-config_optimize_ui_main.html
36015 INFO: checking PYZ
36015 INFO: Building PYZ because PYZ-00.toc is non existent
36015 INFO: Building PYZ (ZlibArchive) D:\git\public_tool\batch_operate\build\config_optimize_ui_main\PYZ-00.pyz
37296 INFO: Building PYZ (ZlibArchive) D:\git\public_tool\batch_operate\build\config_optimize_ui_main\PYZ-00.pyz completed successfully.
37317 INFO: checking PKG
37318 INFO: Building PKG because PKG-00.toc is non existent
37318 INFO: Building PKG (CArchive) config_optimize_ui_main.pkg
37340 INFO: Building PKG (CArchive) config_optimize_ui_main.pkg completed successfully.
37341 INFO: Bootloader d:\git\public_tool\venv_32\lib\site-packages\PyInstaller\bootloader\Windows-32bit-intel\run.exe
37341 INFO: checking EXE
37342 INFO: Building EXE because EXE-00.toc is non existent
37342 INFO: Building EXE from EXE-00.toc
37342 INFO: Copying bootloader EXE to D:\git\public_tool\batch_operate\build\config_optimize_ui_main\config_optimize_ui_main.exe.notanexecutable
37399 INFO: Copying icon to EXE
37425 INFO: Copying icons from ['d:\\git\\public_tool\\venv_32\\lib\\site-packages\\PyInstaller\\bootloader\\images\\icon-console.ico']
37427 INFO: Writing RT_GROUP_ICON 0 resource with 104 bytes
37427 INFO: Writing RT_ICON 1 resource with 3752 bytes
37427 INFO: Writing RT_ICON 2 resource with 2216 bytes
37428 INFO: Writing RT_ICON 3 resource with 1384 bytes
37428 INFO: Writing RT_ICON 4 resource with 37019 bytes
37428 INFO: Writing RT_ICON 5 resource with 9640 bytes
37428 INFO: Writing RT_ICON 6 resource with 4264 bytes
37428 INFO: Writing RT_ICON 7 resource with 1128 bytes
37430 INFO: Copying 0 resources to EXE
37431 INFO: Embedding manifest in EXE
37432 INFO: Updating manifest in D:\git\public_tool\batch_operate\build\config_optimize_ui_main\config_optimize_ui_main.exe.notanexecutable
37432 INFO: Updating resource type 24 name 1 language 0
37435 INFO: Appending PKG archive to EXE
37439 INFO: Fixing EXE headers
37506 INFO: Building EXE from EXE-00.toc completed successfully.
37512 INFO: checking COLLECT
37513 INFO: Building COLLECT because COLLECT-00.toc is non existent
37514 INFO: Building COLLECT COLLECT-00.toc
40198 INFO: Building COLLECT COLLECT-00.toc completed successfully.

(venv_32) D:\git\public_tool\batch_operate>
```
Hmm, so it is `api-ms-*` that are problematic - they are probably just stubs, and not valid DLL files. I can see the binary dependency analysis also failing on them.

However, since you only fixed the location for comparison in `matchDLLArch`, this probably means that your application ended up with bunch of invalid `api-ms-*` DLLs collected from their original location (`C:\Windows\system32`).

I think the fix here is to make errors in architecture check non-fatal (in case there are invalid DLL files in the search path). And also check if we need to adjust the search location for 32-bit DLLs on 64-bit Windows (`C:\Windows\system32` might be scanned due to being in PATH, but that should not be a problem as long as architecture check failures are not fatal, and as long as we also search ).
The method you mentioned is also a way to solve the problem. 
The current termination of the PyInstaller program is indeed caused by the program actively throwing an exception. 
In theory, 
it is only necessary to ensure that the PyInstaller program compiled with 32-bit Python on a 64-bit system does not terminate. 
The manifestation of the issue is as described, but there can be multiple solutions to resolve it.
I don't think this actually has anything to do with 32-bit python on 64-bit system. It is just that the `api-ms-*` in `C:\Windows\system32` (actually, `C:\Windows\syswow64`, which `system32` transparently maps to in 32-bit processes) for some reason contains stubs that we cannot analyze. I can reproduce the error by placing an invalid DLL in custom location that is in PATH - such cases should be simply skipped over.

A separate question is whether we should add `C:\Windows\system32\downlevel` to the base search directories or not. On my test Win 10 system, there are no `api-ms-*` DLLs in either `c:\windows\system32` or `c:\windows\syswow64` directories, but they are in `downlevel` subdirectory (in both those directories). So I suppose adding that directory would ensure that they are collected, but on the other hand, I don't see much point in redistributing `ucrtbase.dll` and `api-ms-*` in the first place...
Yes, the fundamental reason for this issue is just as you described. 
It just so happens that the scenario is triggered during the compilation with 32-bit Python. 
There are several ways to solve this problem, 
and we can opt for a solution that has minimal impact and is more universally applicable to perform the fix.

Resolving patch:
diff --git a/PyInstaller/depend/bindepend.py b/PyInstaller/depend/bindepend.py
--- a/PyInstaller/depend/bindepend.py
+++ b/PyInstaller/depend/bindepend.py
@@ -590,7 +590,12 @@ def _resolve_library_path_in_search_paths(name, search_paths=None):
 
         # On Windows, ensure that architecture matches that of running python interpreter.
         if compat.is_win:
-            if winutils.get_pe_file_machine_type(fullpath) != _exe_machine_type:
+            try:
+                dll_machine_type = winutils.get_pe_file_machine_type(fullpath)
+            except Exception:
+                # A search path might contain a DLL that we cannot analyze; for example, a stub file. Skip over.
+                continue
+            if dll_machine_type != _exe_machine_type:
                 continue
 
         return os.path.normpath(fullpath)


Test patch: