Hosting VLC Player in .NET Winforms – Part 2

The Building Blocks

Previously, in Part 1, we covered the basic mechanics of accessing the VLC DLLs. Next, we just need to step back and get an overview of what we are going to do with the DLL. Note however that this is my interpretation of what is going on in the absence of real documentation.

The first step is to create a “session” with which we will be performing all of the functionality we intend to implement. For this we need to call the “libvlc_new” function of the DLL and we do this using the same basic mechanism as with GetVersion() we saw in the Part 1 section.



So, what is the passed strings ? Well, from some example code they appear to be the equivalent of command line options (there is a list of command line options on the VLC wiki) , which is why I referred to this process as creating a session, though simply passing null does seem acceptable. Boy are there a shed load of options, but looking at them from the point of view of using the DLL in our own applications I reckon the following are of relevance.

Option Description
–osd or –no-osd VLC can display messages on the video – an example is displaying a video’s file name when first starting to play it. Enabled by default but I guess for your own app you’d prefer to set the –no-osd option.
–ignore-config The VLC wiki states “no configuration option will be loaded nor saved to config file (default enabled)”. I suspect that this might mean if we did specify certain options (not sure which) then they might alter any user settings kept by the real macoy VLC player. Personally, I think our own application should not modify another application’s settings, always specify this setting.
–plugin-path=<string path> Additional path(s) that should be searched for additional plugins. If you are directly using VLC’s DLLs – that is, including them all in your project and copying said DLLs to your project’s output directory then I suspect you may need to use this option and specify e.g. “./plugins” (assuming you put the zillion plugin DLLs in subdirectory “Plugins” on your project’s output directory.

There are additional options that allow for logging to files which can also come in pretty handy:

Option Description
–logfile=<string filename> Specify the log filename.
–logmode={text,html} Specify the log format. Available choices are “text” (default) and
–log-verbose=<integer> Select the verbosity to use for log or -1 to use the same verbosity
given by –verbose.

So, back to the New() function. You’ll probably call it like this, saving the returned IntPtr (a session handle, _hDllSession) for future use :



With this IntPtr “_hDllSession” we will be able to create a media object – a file we want to play – as well as a player object with which to play the media item. But before we get carried away remember the returned IntPtr is basically a handle used by a Win32 DLL i.e. is an unmanaged resource, so when you finish whatever you are doing you will need to release the handle. For example, lets say you called the “New()” function on a form’s OnLoad handle. Well, you simply need to hook the form’s FormClosing() event and dispose of the handle using the libvlc_release function:


And whilst we are at it, we will be dealing with a number of different IntPtr handles, we really oughta take a look at using the SafeHandle class – I don’t use it here for clarity but you should think again in production code.

Anyhow, down to business. We will be creating a media object to represent a media file and also a player object to play the media. Both the media object and the player object have their own “new” and release functions; from this point on, since the process of hooking the relevant DLL function follows the vlc_get_version, libvlc_new and libvlc_release functions already presented, I will simplify things and just give the appropriate delegate functions. You can create the remaining code using the following logic, assuming you want to call a libvlc function “libvlc_a_b” :-

  • Create a delegate called “libvlc_a_b”
  • Create a variable “libvlc_a_b_c   _abc”
  • Create a function “abc” that initialises the variable _abc (if required) and calls it.

So, first are the “new” and “release” functions for creating a media object:


Where “mrl” can be the path and file name of the media file and “libvlc_media_inst” is the IntPtr handle originally returned by a call to libvlc_media_new_path.

Next are the functions for creating and releasing a player object.


We can now load the DLL and create player and media objects. Now we need to build our very own player which we shall cover in the next part.


Leave a Reply