Using libpurple with Visual Studio

Zoltán Sólyom z-ismeretlen at freemail.hu
Tue Jun 4 14:28:06 EDT 2013


I made libpurple work with my messenger project in C# in Visual Studio 
2012 Express weeks ago, just didn't have time to touch it till now. The 
solution I found does not require rebuilding libpurple. I never wanted 
to work on libpurple, I wanted to work on a separate project which uses 
libpurple, big difference. I think I'm being seriously misunderstood 
about this. (If not, the advices I got here about having to rebuild 
libpurple were mistaken.)

Here is a step-by-step tutorial how to make libpurple work for anyone in 
any VS project.

What you need:
Visual Studio (probably works from VS2008 and up), MinGW (there is no 
need to install cygwin etc.), the Pidgin source, DLLs from a working 
Pidgin, some of the dependencies from 
https://developer.pidgin.im/wiki/BuildingWinPidgin (If I remember right, 
the only one you need is gtk+ for some glib libraries and header files.)

1. Create libpurple.lib from libpurple.dll. (Instructions here: 
http://adrianhenke.wordpress.com/2008/12/05/create-lib-file-from-dll/ )

2. Create the VS project and add libpurple.lib, glib-2.0.lib and 
gthread-2.0.lib to Linker\Input\Additional Dependencies (I took the 
library version numbers from the gtk+ archive on the BuildingWinPidgin 
page, though they might be different in other sources.)

3. Add the pidgin source root dir to the include libraries, and also the 
following: $(YourGTKRoot)\lib\glib-2.0\include and 
$(YourGTKRoot)\include\glib-2.0
$(YourGTKRoot) is a folder like "gtk_2_0-2.14", wherever you extracted gtk+.

4. Add the folder with libpurple.lib to the "Library Directories" and 
also: $(YourGTKRoot)\lib

5. In Stdafx.h add the following:
#pragma warning(disable : 4005)
#include <glibconfig.h>
#include <glib.h>
#include <libpurple/util.h>
#include <libpurple/pounce.h>
#include <libpurple/core.h>
#include <libpurple/debug.h>
#pragma warning(default : 4005)

These include all the glib and libpurple functions and structures I used 
in my project so far. It is possible that some others might be needed 
later. (The libpurple/*** folder is in the pidgin source root. If the 
pidgin source root is added to the include libraries as I instructed 
above, VS will find these includes.)

6. This is the step which I only tested but haven't needed in my project 
yet: If you need to use functions that return stuff that was created 
with malloc, fopen etc. and it is written in the header "documentation" 
that it must be freed by the caller, it obviously cannot be done by the 
MS runtime. The simplest solution which avoids having to rebuild the 
whole glib, libpurple and additional libraries with VS, is to create a 
"proxy DLL" with MinGW. The only things that the DLL has to export are 
functions that call free, fclose etc. For example:
extern "C"
{
  __declspec(dllexport) void __cdecl exported_free(void *ptr)
   {
     free(ptr);
   }
}

The DLL created with MinGW this way works with VS and makes it possible 
to use the MS runtime for your own code and the thing in MinGW at the 
same time.

Basically this is it. It is now easy to use C# with libpurple this way 
via c++/cli.

As you can see I haven't done anything difficult. I could have got to 
this point in a day or two if I'm not told that I MUST rebuild libpurple 
and all its dependencies, which took weeks from my life before I gave 
up. I wrote this in the hope that someone can make use of it some day, 
and won't have to go through all the frustration I went through. (I'm 
currently frustrated with some stuff in libpurple itself, but that's a 
different story.)




More information about the Devel mailing list