Create a c/c++ DLL and use it in Xojo

Hi,

I found this example https://forum.xojo.com/t/import-c-function-in-xojo/19944/4

The project can’t be downloaded anymore so I try to replicate it.
I managed to make the fastmath.dll with MinGW, but I got an error when try to call in xojo

Soft Declare Function DoubleValue Lib "fastmath.dll" ( x As Int32 ) as Int32
  
  MsgBox Str(DoubleValue(20))

Any C++ functions in the DLL would need to be declared as ‘extern “C”’ in order to be accessible via a declare. C++ functions are modified by their parameter types so that they can be overloaded.

1 Like

Seems to me it’s declared that way in the H file.

What is the C function definition. It could be down to types. Int32 is obviously a 32 bit integer, which would typically be a short in C. Also worth noting is that the function you have called “DoubleValue” is returning an Int32 also (according to your declaration).

It’s :

int32_t DoubleValue(int32_t x) {
        return 2 * x;
}

How can I declare as ‘extern “C”’?
Sorry if I’m asking lame questions, I’m amateur.

Hello @Thomas_Kis,

I have a free repository in Github that creates a simple C/C++ function that can be used in Xojo.

Here is the link: GitHub - eugenedakin/RawPlugin: Create a raw x64 bit Windows "C" with "C++" plugin that can be used in Xojo

Warm regards.

1 Like
extern "C" int32_t DoubleValue(int32_t x) {
        return 2 * x;
}

it just changes the name used for the function, so it doesn’t include the parameter types and return type in the name.

Usually you split it:

extern "C" 
{
   // all C functions
   int32_t DoubleValue(int32_t x);
   int32_t HalfValue(int32_t x);
}

int32_t DoubleValue(int32_t x) {
        return 2 * x;
}

int32_t HalfValue(int32_t x) {
        return x / 2;
}

I guess it is missing the dllexport

1 Like

Well, there are multiple ways to mark a function for export.
one of them is dll export attribute.

for MBS Plugin, I usually use an extra export file to list the functions to export to the linker.

Or if no dead stripping is done, all functions may end up in the DLL and exposrted by default.

1 Like

Thank you, I’ll look into it. I just tried this one because it’s seems simple and no Visual Studio involved.

Ouch…I just realized the dll I made is 64bit and the Xojo test project is 32bit. Ouch again…
Changed the project to 64bit, now works like a charm.