Assuming I want to export a set of functions that operate on a C++ object, one option is described in an answer here by tenfour. See below for the C style interface around the Employee object.
Can I do this without passing Employee* pointers to every function if I instead operate only on a global Employee object created on the dll side? How would the below change?
I'm operating under the assumption the client has no headers and is explicitly linking to the DLL at run-time.
extern "C"
{
__declspec(dllexport) Employee* employee_Construct()
{
return new Employee();
}
__declspec(dllexport) void employee_Free(Employee* e)
{
delete e;
}
__declspec(dllexport) void employee_SetFirstName(Employee* e, char* s)
{
e->SetFirstName(std::string(s));
}
__declspec(dllexport) void employee_SetLastName(Employee* e, char* s)
{
e->SetLastName(std::string(s));
}
__declspec(dllexport) int employee_GetFullName(Employee* e, char* buffer, int bufferLen)
{
std::string fullName = e->GetFullName();
if(buffer != 0)
strncpy(buffer, fullName.c_str(), bufferLen);
return fullName.length();
}
}