DavidHarrison

Is it possible to convert a third party static library (libusb.lib) to a dll easily, without the source code

I want to make calls from C# to C functions contained in a static library. One way is to write a C++ .NET wrapper around the C functions, link the static library at compile time and then call the C++ class methods from C#. Alternatively, if the C functions were in a DLL, I could use the [DLLImport] atribute and Interop services to call them directly from C#.

Is it possible to easily convert the static lib to a DLL, if so how I do not have the source code for the library.

Thanks,

David.



Re: Visual C++ Language Static lib to DLL?

Ovidiu Cucu

AFAIK there is no (easy) way to convert a static library to a DLL. So, go ahead, sweat a little, and write the wrapper class(es). ;-)

Just aside note: that's one of problems making me to like more C++ than C#.






Re: Visual C++ Language Static lib to DLL?

DavidHarrison

Thanks, I suspected as much. Actually, I have much more experience in C++ than C#, and I have already created the C++ wrapper. I was just hoping there was a cleaner, simpler way.


David.





Re: Visual C++ Language Static lib to DLL?

Rajiv Bose

David,

Could you email me a copy of your C++ wrapper I too am trying to convert a third party static library to a dll to use in C#. However, I am not an experienced programmer.

Any help you can provide would be greatly appreciated.

Kindest regards,

Raj

rajiv_bose@hotmail.co.uk






Re: Visual C++ Language Static lib to DLL?

KienLip

Hello,

I could use a copy of your wrapper class too. These are the less documented areas and gives me the headache on how to do it.

I am trying to build a wrapper class myself and am getting errors. The current error is a linker error complaining that it cannot find MFC42d.lib when compiling using .Net 2003.

Your help will be greatly appreciated. Please email to kienliplee@hotmail.com.

Thank you

Kien Lip





Re: Visual C++ Language Static lib to DLL?

g_stefan

Here is a command line script that will convert a static library in one dynamic library!

Code Snippet

@echo off
rem lib_to_dll
rem Copyright (c) Grigore Stefan. All rights reserved.
rem
rem Example: lib_to_dll.cmd foo "import1.lib import2.lib"
rem where foo is yours static library name
rem
rem THIS SOFTWARE IS DISTRIBUTED "AS IS". NO WARRANTY OF ANY
rem KIND IS EXPRESSED OR IMPLIED. YOU USE AT YOUR OWN RISK.
rem NEITHER THE AUTHOR NOR THE AGENTS OF THE AUTHOR WILL BE LIABLE
rem FOR DATA LOSS, DAMAGES, LOSS OF PROFITS OR ANY OTHER KIND OF LOSS
rem WHILE USING OR MISUSING THIS SOFTWARE.
rem
setlocal
pushd "C:\Program Files\Microsoft Visual Studio 8\VC"
call vcvarsall.bat x86 1>NUL 2>NUL
popd
pushd "C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2"
call SetEnv.Cmd /XP32 /RETAIL 1>NUL 2>NUL
popd
if "%1"=="" goto usage
if "%1"=="--add-object-file" goto add_object_file
if "%1"=="--add-member" goto add_member
move %1.lib %1-static.lib
set lib_to_dll_link=%1-static.link
set lib_to_dll_members=%1-static.members
set lib_to_dll_def=%1-static.def
set lib_to_dll_list=%1-static.list
set lib_to_dll_lib=%1-static.lib
set lib_to_dll_c=dll_main__%1.c
set lib_to_dll_c2=dll_main__%1.obj
set lib_to_dll_obj_cleanup=%1-obj.cmd
lib /nologo /list %lib_to_dll_lib% >%lib_to_dll_list%
for /F "tokens=*" %%i in ("#include <windows.h>") do echo %%i >%lib_to_dll_c%
echo. >>%lib_to_dll_c%
echo BOOLEAN WINAPI DllMain( >>%lib_to_dll_c%
echo HINSTANCE hDllHandle, >>%lib_to_dll_c%
echo DWORD nReason, >>%lib_to_dll_c%
echo LPVOID Reserved){ >>%lib_to_dll_c%
echo. >>%lib_to_dll_c%
echo BOOLEAN bSuccess = TRUE;>>%lib_to_dll_c%
echo. >>%lib_to_dll_c%
echo switch (nReason){ >>%lib_to_dll_c%
echo case DLL_PROCESS_ATTACH: >>%lib_to_dll_c%
echo DisableThreadLibraryCalls( hDllHandle ); >>%lib_to_dll_c%
echo break; >>%lib_to_dll_c%
echo case DLL_PROCESS_DETACH: >>%lib_to_dll_c%
echo break; >>%lib_to_dll_c%
echo. >>%lib_to_dll_c%
echo }; >>%lib_to_dll_c%
echo. >>%lib_to_dll_c%
echo return bSuccess; >>%lib_to_dll_c%
echo }; >>%lib_to_dll_c%
echo.

echo /nologo /LD /Fo%lib_to_dll_c2% /Fe%1.dll /Zp8 /WX /MT /Od /GF /Gy /Gm- /GS- /GR- /GL /GA /GT /Gd /EHsc /D_WIN32_WINNT=0x0500 /DWINVER=0x0500 /D_WIN32_IE=0x0501 /DWIN32_LEAN_AND_MEAN=1 /DSECURITY_WIN32=1 /D_CRT_SECURE_NO_DEPRECATE=1 /D_CRT_NONSTDC_NO_DEPRECATE=1 > %lib_to_dll_link%
echo %lib_to_dll_c% >>%lib_to_dll_link%
for /F "tokens=*" %%i in (%lib_to_dll_list%) do call %~dp0lib_to_dll.cmd --add-object-file %1 %%i %%~nxi
echo ws2_32.lib user32.lib wininet.lib advapi32.lib ole32.lib oleaut32.lib shell32.lib iphlpapi.lib odbc32.lib winmm.lib vfw32.lib gdi32.lib activeds.lib adsiid.lib secur32.lib >>%lib_to_dll_link%
if not "%~2"=="" echo %~2 %~3 %~4 %~5 %~6 %~7 %~8 %~9 >>%lib_to_dll_link%
echo %lib_to_dll_def% >>%lib_to_dll_link%
dumpbin /LINKERMEMBER:1 %lib_to_dll_lib% >%lib_to_dll_members%
set lib_to_dll_member=0
echo EXPORTS >%lib_to_dll_def%
del /F /Q %1-static.flag 2>NUL
for /F "tokens=*" %%i in (%lib_to_dll_members%) do call %~dp0lib_to_dll.cmd --add-member %1 %%i %%~nxi
echo /link /LIBPATH:.\ >>%lib_to_dll_link%
cl.exe @%lib_to_dll_link%
del /Q /F %1-static.flag
del /Q /F %lib_to_dll_link%
del /Q /F %lib_to_dll_members%
del /Q /F %lib_to_dll_list%
del /Q /F %lib_to_dll_c%
del /Q /F %lib_to_dll_c2%
call %lib_to_dll_obj_cleanup%
del /Q /F %lib_to_dll_obj_cleanup%
goto done
:add_object_file
lib /nologo /extract:%3 /out:%4 %2-static.lib
echo %4 >>%2-static.link
echo del /Q /F %4 >>%2-obj.cmd
goto done
:add_member
if not exist %2-static.flag goto add_member_2
if "%4"=="member" (
del /F /Q %2-static.flag
goto done
)
set lib_to_dll_member2=%4
set lib_to_dll_member3=%lib_to_dll_member2:~1%
set lib_to_dll_member4=%lib_to_dll_member2:~0,5%
set lib_to_dll_member6=%lib_to_dll_member2:~0,1%
set lib_to_dll_member7=%lib_to_dll_member2:~0,4%
set lib_to_dll_member8=%lib_to_dll_member2:~0,3%
for /F "tokens=1 delims=@" %%i in ("%lib_to_dll_member3%") do set lib_to_dll_member5=%%i
if "%lib_to_dll_member4%"==" _C@" goto done
if "%lib_to_dll_member6%"==" " goto c_plus_plus
echo %lib_to_dll_member5% >>%2-static.def
goto done
:c_plus_plus
rem destructor
if "%lib_to_dll_member4%"==" 1 $" goto c_plus_plus2
rem virtual destructor - do not export
rem if "%lib_to_dll_member8%"==" 1" goto done
if "%lib_to_dll_member7%"==" _G" goto done
if "%lib_to_dll_member7%"==" _E" goto done
echo %lib_to_dll_member2% >>%2-static.def
goto done
:c_plus_plus2
echo %lib_to_dll_member2% >>%2-static.def
:add_member_2
if "%4 %5"=="public symbols" echo X >%2-static.flag
goto done
:usage
echo usage:
echo lib_to_dll library_name [libs]
:done
endlocal

You can download this directly from http://www.xyo.ro/lib_to_dll.cmd.txt