Search code examples
c#phpwindowsdlltaglib-sharp

Build TagLib# DLL from source and make it COM Visible to PHP


Hello I want to scan audio-video files and store their metadata in a database using php. I found this Command-line wrapper that uses TagLib.dll compiled by banshee developpers to do the job. It's works fine but it limited by the functions implemented. I want to access directly to the dll methods via PHP.

In PHP we have a function (DOTNET) that allows me to instantiate a class from a .Net assembly and call its methods and access its properties like this :

/*$obj = new DOTNET("assembly", "classname");*/  

$stack = new DOTNET("mscorlib", "System.Collections.Stack");
$stack->Push(".Net");
$stack->Push("Hello ");
echo $stack->Pop() . $stack->Pop();

//Returns string(10) "Hello .Net";

Here is the Taglib# project sources in github

I saw many questions relatives to PHP-DLL-COM and there is some recommendations :

  • Make the dll comVisible;
  • Register the dll with regsvr32;
  • Use a module definition file similar to

;

;

DESCRIPTION     "Simple COM object"

EXPORTS
                DllGetClassObject       PRIVATE
                DllCanUnloadNow         PRIVATE
                DllRegisterServer       PRIVATE
                DllUnregisterServer     PRIVATE

My question is : How can I build the DLL and use its method via PHP ?

My config :

  • OS

    Windows Server 2012 R2 Standard Edition i586

  • Apache :

    Apache/2.2.21 (Win32) DAV/2 PHP/5.4.42 mod_ssl/2.2.21 OpenSSL/0.9.8r

  • PHP

    PHP Version : 5.4.42
    Arch : x86
    Compiler : MSVC9 (Visual C++ 2008)
    COM support : enabled
    DCOM support : disabled
    .Net support enabled

  • Microsoft Visual Studio 2013


Solution

  • Give the following steps a try:

    1. Download taglib source code from github
    2. Remove the ApplicationIcon tag from the .csproj file and open .sln in Visual Studio
    3. Unload the test project (you don't need to build this)
    4. Right-click on taglib-sharp project --> properties --> build --> enable 'Register for COM interop'. Also clear the conditional compilation symbols textbox so that you don't have to bother with downloading SharpZipLib for now.
    5. Observe #1: in project properties --> Application --> Assembly information --> 'Make assembly COM visible' is checked
    6. Observe #2: in project properties --> Application --> target framework is set to 3.5 (make sure you leave it as that)
    7. Build the project (F6)
    8. Read the contents of the output window to see some warnings

    Now that you have src\taglib-sharp.dll you need to register it into the global assembly cache in order for the DOTNET class to find it. See PHP DOTNET hell for details if you are not familiar with this.

    If all is good, you can get yourself a copy of SharpZipLib, and reenter the HAVE_SHARPZIPLIB conditional compilation symbol --> rebuild --> redeploy to GAC --> and be a happy man! :)