PDA

View Full Version : Using C++ for plugins in Visual Studio



BloodQuest
12-10-2008, 12:23 PM
From an old plugin tutorial by Patrick Westlin:

SPECIAL NOTE FOR WORKING WITH AND COMPILING C++ .cpp FILES

Normally you would create and build LW plug-ins written in pure C code. However if you're like me you like some of the benefits of writing in C++. In order to get your plug-ins to compile properly you will need to make a couple of type-casting changes in your code and the above code. The first thing you will need to do is explicitly cast the ServerRecord as extern C, so change:

ServerRecord ServerDesc[] =
{
{ LWLAYOUTGENERIC_CLASS, "Hello World", HelloworldPanel },
{ NULL }
};

to

extern "C"
{ ServerRecord ServerDesc[] =
{
{ (const char*)LWLAYOUTGENERIC_CLASS, (const char*)"Hello World", (ActivateFunc*)HelloworldPanel },
{ NULL }
};
}

(Basically the whole thing gets wrapped inside the extern "C" { } block.)

Notice that the each of the ServDesc[] parameters were also explicitly type cast as well.

One last note is that you will need to explicitly typecast some of the LW functions to keep them properly aligned. How will you know if you need to do this? Well if you get a message like this:

[path]\hello.cpp(35) : error C2440: '=' : cannot convert from 'void *' to 'struct st_LWPanelFuncs *'
Conversion from 'void*' to pointer to non-'void' requires an explicit cast
That would be a good indication you will need to explicitly type cast the offending function call. In this case you will need to change this line of code

//Get the panel fxns
panf = global(LWPANELFUNCS_GLOBAL, GFUSE_TRANSIENT);

to

//Get the panel fxns
panf = (LWPanelFuncs*)global(LWPANELFUNCS_GLOBAL, GFUSE_TRANSIENT);

Noting that panf was originally casted as LWPanelFuncs.

I've used this for several plugins and it works fine (also don't forget to tell Visual Studio that you're now compiling C++ code, not plain C).

Simon

Roadwarrior
12-12-2008, 08:35 PM
Interesting stuff BloodQuest Thanks for posting:thumbsup: