Skip to content

Instantly share code, notes, and snippets.

@matcool
Last active May 30, 2024 20:17
Show Gist options
  • Save matcool/05f8d1dbfa594ef81550dca49c76f01f to your computer and use it in GitHub Desktop.
Save matcool/05f8d1dbfa594ef81550dca49c76f01f to your computer and use it in GitHub Desktop.
dll2lib - generates a .lib file from a dll
@echo off
REM Usage: dll2lib [32|64] some-file.dll
REM
REM Generates some-file.lib from some-file.dll, making an intermediate
REM some-file.def from the results of dumpbin /exports some-file.dll.
REM
REM Requires 'dumpbin' and 'lib' in PATH - run from VS developer prompt.
REM
REM Script inspired by http://stackoverflow.com/questions/9946322/how-to-generate-an-import-library-lib-file-from-a-dll
REM mat: based off https://gist.github.com/Trass3r/8d0232a66b098530d07b0e48df6ad5ef/, made *much* faster by using python
if "%~2"=="" (
echo "Usage: dll2lib [32|64] some-file.dll"
goto :eof
)
SETLOCAL
if "%1"=="32" (set machine=x86) else (set machine=x64)
set dll_file=%2
set dll_file_no_ext=%~n2
set exports_file=%dll_file_no_ext%-exports.txt
set def_file=%dll_file_no_ext%.def
set lib_file=%dll_file_no_ext%.lib
set lib_name=%dll_file_no_ext%
dumpbin /exports %dll_file% > %exports_file%
echo LIBRARY %lib_name% > %def_file%
echo EXPORTS >> %def_file%
REM for /f "skip=19 tokens=1,4" %%A in (%exports_file%) do if NOT "%%B" == "" (echo %%B @%%A >> %def_file%)
REM much faster.. because batch sucks for some reason..
python -c "import sys; open(sys.argv[2], 'a').write('\n'.join([l.split()[3] for l in open(sys.argv[1],'r').read().splitlines()[19:] if len(l.split()) > 3]))" %exports_file% %def_file%
lib /def:%def_file% /out:%lib_file% /machine:%machine%
REM Clean up temporary intermediate files
del %exports_file% %def_file% %dll_file_no_ext%.exp
:eof
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment