Explicit DLL 1 function failing - c++

I am writing the following code
#include "SwiWrapper.h"
#include <windows.h>
HINSTANCE hDLL;
SwiWrapper::SwiWrapper()
{
}
SwiWrapper::~SwiWrapper()
{
}
bool SwiWrapper::Initialize()
{ // Handle to DLL
hDLL = LoadLibrary("SWI32.dll");
return true;
}
void SwiWrapper::CloseDll()
{
FreeLibrary(hDLL);
}
//WiRawImage* CALLSPEC WiCreateRawImage ARGSPEC((void));
typedef WiRawImage*(*FuncWiCreateRawImage) (void);
WiRawImage * SwiWrapper::WiCreateRawImage()
{
FuncWiCreateRawImage Exec = (FuncWiCreateRawImage)GetProcAddress(hDLL, "WiCreateRawImage");
return Exec();
}
//WiCmpImage* CALLSPEC WiCreateCmpImage ARGSPEC((void));
typedef WiCmpImage*(*FuncWiCreateCmpImage) (void);
WiCmpImage * SwiWrapper::WiCreateCmpImage()
{
FuncWiCreateCmpImage Exec = (FuncWiCreateCmpImage)GetProcAddress(hDLL, "WiCreateCmpImage");
return Exec();
}
// WiDecmpOptions* CALLSPEC WiCreateDecmpOptions ARGSPEC((void));
typedef WiDecmpOptions*(*FuncWiCreateDecmpOptions) (void);
WiDecmpOptions * SwiWrapper::WiCreateDecmpOptions()
{
FuncWiCreateDecmpOptions Exec = (FuncWiCreateDecmpOptions)GetProcAddress(hDLL, "WiCreateDecmpOptions");
return Exec();
}
//int CALLSPEC WiDecompress ARGSPEC(( WiDecmpOptions *DecmpOptions, WiRawImage *RawImage, WiCmpImage *CmpImage ));
typedef int(*WiDecompressFunc) (WiDecmpOptions*, WiRawImage*, WiCmpImage*);
int SwiWrapper::WiDecompress(WiDecmpOptions * DecmpOptions, WiRawImage * RawImage, WiCmpImage * CmpImage)
{
WiDecompressFunc Exec = (WiDecompressFunc)GetProcAddress(hDLL, "WiDecompress");
int result = Exec(DecmpOptions, RawImage, CmpImage);
return 0;
}
And I am using it like this
SwiWrapper *wrapper = new SwiWrapper();
if (initialized)
{
image = wrapper->WiCreateRawImage();
cmpImage = wrapper->WiCreateCmpImage();
decmpOpts = wrapper->WiCreateDecmpOptions();
GetCmpImage(cmpImage, "path\\data.bin");
SetDecompressionOptions(decmpOpts);
wrapper->WiDecompress(decmpOpts, image, cmpImage); //This line is failing
FileImage("path\\data.jpg", image);
wrapper->CloseDll();
}
However when I reach the following line wrapper->WiDecompress(decmpOpts, image, cmpImage); it fails and gives the following error
Run-Time Check Failure #0 - The value of ESP was not properly saved
across a function call. This is usually a result of calling a
function declared with one calling convention with a function pointer
declared with a different calling convention.
I think that I am messing up with the parameters but I am not sure what I am doing wrong. I have the header file to know the input and outputs.
I am not that experienced with C++, well not lately, so my searching didn't get me to an answer that worked although I found a few Stackoverflow solutions on the error but I cannot figure out what I need to change
Everything that I have on the swi32.dll is in this rar file in this dropbox link
https://www.dropbox.com/s/2bfhylzb2evrggp/Lib.rar?dl=0
My full source code is in the following link
https://www.dropbox.com/s/jkxfyt6xjeanvng/ConsoleApplication1.rar?dl=0

Because all other function don't take an argument, I suppose that the CALLSPEC that is mentioned in the comment for the Decompress function is different to the calling convention you use.
Recheck the calling conventions and the definition of your typedef for the Decompress function.

I have managed to get it working by changing
this line
typedef int(*WiDecompressFunc) (WiDecmpOptions*, WiRawImage*, WiCmpImage*);
to this line
typedef int(__stdcall *WiDecompressFunc)(WiDecmpOptions * DecmpOptions, WiRawImage * RawImage, WiCmpImage * CmpImage);
Credits to:
#Mgetz, #HansPassant who pointed out to use __stdcall and also the link how to use it

Related

How to pass string argument to macro

I am trying to print IT SUCCESS\nET SUCCESS\n using following code but it's failing in compilation with error error: ‘printds’ was not declared in this scope which I know is because it's taking macro input as ds literal. Does anyone know how to do this? The use case is that there are several printXX() functions which should be called based on value passed in macro.
#include <stdio.h>
#define FOO(val) { \
print ## val(); \
}
void printIT() { printf("IT SUCCESS\n"); }
void printET() { printf("ET SUCCESS\n"); }
int main() {
const char* ds = "IT", es = "ET";
FOO(ds); FOO(es);
return 0;
}
You can change
FOO(ds); FOO(es);
to
FOO(IT); FOO(ET);
Because macro substitutions happen before your code is compiled.
But you can define a function called FOO like
#include <stdio.h>
#include <iostream>
using namespace std;
void printIT() { printf("IT SUCCESS\n"); }
void printET() { printf("ET SUCCESS\n"); }
void FOO(const string str)
{
if(str=="IT")
printIT();
else
printET();
}
int main()
{
const char* ds = "IT",*es="ET";
FOO(ds);FOO(es);
return 0;
}
it's taking macro input as ds literal.
Yes, that's to be expected. Preprocessor macros are expanded at compile time. The arguments to function-like macros are the literal source-code tokens that appear between the parentheses in the macro invocation. These have no additional meaning to the preprocessor.
Does anyone know how to do this? The use case is that there are several printXX() functions which should be called based on value passed in macro.
Again, macros are expanded, to source code (approximately), at compile time. The process does not and cannot take into account C++ runtime semantics such as converting variables' identifiers into corresponding values.
If runtime dynamic function dispatch based on variables' values is what you're after then you need an altogether different mechanism. You could use ordinary conditional statements to select between different function calls, for example. If you wanted to be even more dynamic then you could consider preparing a lookup table of function pointers, and using that to select and call appropriate functions.
In comments, you added
I've several methods queryServers, queryNodes, queryTargets which I want to call using above trick.
You may be able to accomplish something similar to what you ask via templates or overloaded functions. These mechanisms, too, operate at compile time, so they have no access to runtime information such as variables' values, but they do know about and rely upon C++ data types.
Alternatively, perhaps you're looking for the Strategy pattern.
The first thing you need to know is that the Macros are preprocessor directives which are a fragment of code with a given name. if you use macro name in your program it will replace that code fragment into that place you use the macro name at compile time first stage called Pre-processing stage.
#include <stdio.h>
#define FOO(val) { \
print ## val(); \
}
void printIT() { printf("IT SUCCESS\n"); }
void printET() { printf("ET SUCCESS\n"); }
int main() {
const char* ds = "IT", es = "ET";
FOO(ds); FOO(es);
return 0;
}
In your code, you try to input ds and es variables into FOO function like macro. But ds and es variables declared in program stack only when you run the program. At the compile-time, it just treats them as only texts. Therefore macro function input it takes as text ds and es and replaced with val. That's why you got the compile time error. The following code fragment I have changed is working as you expected.
#include <stdio.h>
#define FOO(val) { \
print ## val(); \
}
void printIT() { printf("IT SUCCESS\n"); }
void printET() { printf("ET SUCCESS\n"); }
int main() {
const char* ds = "IT", *es = "ET";
FOO(IT); FOO(ET);
return 0;
}
If you are interested you can find more about Macros in the following resources.
GCC online documentation and Article about Macros . Also, you can view the preprocessed code using g++ -E (your cpp file name). Thanks.
Possible solution without MACRO:
void printIT() { printf("IT SUCCESS\n"); }
void printET() { printf("ET SUCCESS\n"); }
void foo(std::string_view s)
{
static const std::map<std::string_view, void(*)()> funcs{
{"IT", &printIT},
{"ET", &printET}
};
auto it = funcs.find(s);
if (it != funcs.end()) {
(*it->second)();
}
}
int main() {
const char* ds = "IT";
const char* es = "ET";
foo(ds); foo(es);
}
Demo

Dynamically loaded library and global variables in c++

I'm trying to build a system, that uses dynamic loaded library files (.so files) as plugins in C++14. I build the project using gcc in combination with qt5.5.1 within qtcreator.
The problem I'm having is, that I don't fully understand, what dlopen() (and dlsym()) actually does and get strange behavior because of it. Here is a simplified (not executable) Version:
/*Kernel.hpp*/
class Kernel{
int loadPlugins();
}
void* sharedPointer; //The Object location is stored in here
/*Kernel.cpp*/
Kernel::loadPlugins(){
handle1 = dlopen(<file1>, RTLD_LAZY);
init_t init = (init_t) dlsym(handle1, "init"); //init_t is just a fitting function pointer
execute_t exec = (execute_t) dlsym(handle1, "execute"); //same goes for "execute_t"
handle2 = dlopen(<file2>, RTLD_LAZY);
init_t init = (init_t) dlsym(handle2, "init");
execute_t exec = (execute_t) dlsym(handle2, "execute");
}
/*<file1.h>*/
Class Test{
int func();
int field = 0;
}
/*<file1.cpp>*/
int Test::func(){/*do stuff*/}
Test* test = NULL;
extern void* sharedPtr; //use the ptr from kernel
extern "C" init(){
test = new Test();
sharedPtr = (void*)test; //store address of newly created Test-Object
}
extern "C" execute(){
/* Do Stuff */
}
/*<file2.h>*/
/*<file2.cpp>*/
Test* test = NULL;
extern void* sharedPtr; //use the ptr from kernel
extern "C" init(){
test = (Test*)sharedPtr; //get address of Testobject
}
extern "C" execute(){
std::cout << test->field << std::endl; //Working perfectly
std::cout << test->func() << std::endl //Segmentaion fault
}
The precise Error is a symbol lookup error of a member function with some mangled name (unmangled name is Kernel::test()).
What I think, that should happen:
When Kernel.loadPlugins() is called, the first library creates an object, saves its address in the main program. Library reads that address and can use it, as if it had created that object. So the field can be read and written to and the member function can be called.
What actually happens:
When Kernel.loadPlugins() is called, the first library creates said object, can use it as expected, saves its address in the main program. Library receives said address as expected, can use the field of said object as expected (it does not matter what type that field has, even other objects, like strings, worked, valgrind does not show any leaks as well), but when it tries to call func() it produces a segmentation fault.
I have two primary doubts -
First, I would like to know why that happens?
Second, I would like to know if there is a nice way to fix it?

Accessing members in a typedef map struct?

My question should be simple and I've spent that last few hours trying to get an answer to it by figuring it out on my own (and google-ing) answers and getting nowhere.
I've got the following in my header file:
Window.hpp
typedef struct {
SDL_Window* window = 0;
const char* title = 0;
int width;
int height;
int flags;
} m_windowStruct;
typedef std::map<std::string, m_windowStruct> m_windowMap;
m_windowMap windowMap; /* <-- Made this accessible */
and in my source file I've the following code:
Window.cpp
bool Window::createWindow(std::string id, const char* title, int width, int height, int flags) {
m_windowMap* windowMap;
m_windowStruct windowData;
windowData.window = SDL_CreateWindow("insert window creation info here");
if (!windowData.window) {
windowData.title = title;
windowData.width = width;
windowData.height = height;
windowData.flags = flags;
windowMap->insert(m_windowMap::value_type(id, &windowData));
SDL_LogInfo(INFO, "Window creation successful");
return true;
}
else {
return false;
}
};
Which works all fine and dandy... The problem I'm having is accessing the mapped struct members after.
For example if I want the instance of the primary window I'd want the following function:
/* Returns the window instance from the mapped struct */
SDL_Window* Window::getWindow(std::string id) {
m_windowMap::const_iterator keyValuePair = windowMap.find(id); /* <-- New error: Error: expression must have a class type */
if (keyValuePair == windowMap.end()) { /* <-- same error as just above */
return 0;
}
return keyValuePair->second.window; /* <-- Same error as just above */
};
I cannot for the life of me figure out how to access the second window member so that I can then give it to my renderer!
Any ideas?
I guess you want the windowMap being a global variable which is accessible across multiple source files (i.e., .cpp). Here are the changes you need to make,
In header file, replace the last line with extern m_windowMap* windowMap;. This makes variable accessible to all source files that include the header.
In your main function (or anywhere appropriate), add this line to initialize the map windowMap = new m_windowMap();
Remove m_windowMap* windowMap; in the Window::createWindow function, otherwise the re-declaration creates a new local m_windowMap and the rest of the function makes no effect to the global windowMap.
Lastly, since the global windowMap is a pointer, change .find() and .end() to ->find() and ->end() in the getWindow function.
If the windowMap is indeed a class member of Window, it is actually easier and cleaner.
From the code you originally posted, remove m_windowMap* windowMap; in the Window::createWindow function. Otherwise the insert goes to this newly created local map. Also, change ->insert to .insert as you did not declare windowMap as an pointer in the header.

How to test multi-threaded in google test?

The following testcode creates a server- and a clientsocket. Then client sends a message to server and server replies. Thats all. But I can't compile. All ASSERT_EQ in the threadfunctions raise the error "error: void value not ignored as it ought to be". I have no clue what this should tell me. What is the problem in here? Type is irrelevant as ASSERT_EQ(1, 1); raises the errors too.
EDIT Found this in FAQ from google:
Q:My compiler complains "void value not ignored as it ought to be." What does this mean?
A: You're probably using an ASSERT_XY() in a function that doesn't return void. ASSERT_XY() can only be used in void functions.
How shall I understand this?
void * serverfunc(void * ptr);
void * clientfunc(void * ptr);
TEST(netTest, insert)
{
pthread_t mThreadID1, mThreadID2;
::pthread_create(&mThreadID1, nullptr, serverfunc, nullptr);
::sleep(1);
::pthread_create(&mThreadID1, nullptr, clientfunc, nullptr);
::pthread_join(mThreadID1, nullptr);
::pthread_join(mThreadID2, nullptr);
}
void * serverfunc(void * ptr)
{
net::ServerSocket serv(IPV4, TCP, 55555,5);
net::ServerSocket * conn = serv.accept();
net::Message msg;
conn->recvmsg(&msg);
ASSERT_EQ(msg.size(),5);
ASSERT_EQ(msg[0],1);
ASSERT_EQ(msg[1],2);
ASSERT_EQ(msg[2],3);
ASSERT_EQ(msg[3],4);
ASSERT_EQ(msg[4],5);
msg = {9,8,6};
ASSERT_EQ(msg.size(),3);
ASSERT_EQ(msg[0],9);
ASSERT_EQ(msg[1],8);
ASSERT_EQ(msg[2],6);
conn->sendmsg(msg);
::sleep(1);
delete conn;
return 0;
}
void * clientfunc(void * ptr)
{
net::ClientSocket clie(IPV4, TCP, "localhost",55555);
net::Message msg;
msg = {1,2,3,4,5};
ASSERT_EQ(msg.size(),5);
ASSERT_EQ(msg[0],1);
ASSERT_EQ(msg[1],2);
ASSERT_EQ(msg[2],3);
ASSERT_EQ(msg[3],4);
ASSERT_EQ(msg[4],5);
clie.sendmsg(msg);
clie.recvmsg(&msg);
ASSERT_EQ(msg.size(),3);
ASSERT_EQ(msg[0],9);
ASSERT_EQ(msg[1],8);
ASSERT_EQ(msg[2],6);
return 0;
}
Q:My compiler complains "void value not ignored as it ought to be."
What does this mean?
A: You're probably using an ASSERT_XY() in a function that doesn't
return void. ASSERT_XY() can only be used in void functions.
How shall I understand this?
Your functions don't return void, they return void* - i.e. they return something (void* is a pointer-to-anything) while they should return nothing. The FAQ says it is required for the functions which use ASSERT_EQ() to have the void return type.
I have the same problem, too, and I found an "ugly" way to solve it:
void* your_func(void* ptr)
{
_your_func(ptr);
reutrn NULL;
}
void _your_func(void* ptr)
{
...
ASSERT_EQ(1, 1);
...
}
It looks like the ASSERT_EQ can only be called in a function with the right return type (which appears to be void while yours return void *)
Personally I dislike macro over-use but that's the way it is. The issue with the macro is it obfuscates the code, so you can't see what it's doing wrong.
So just write such a function and get serverfunc and clientfunc to call it.
As proposed, you should replace void* type for your method with void, and return 0 with return.

Calling a C++ Shared Lib within a C program...how to manage?

i'd like to write a wrapper for a C++ framework. this framework is kinda buggy and not really nice and in C++. so i'd like to be able to call their methods from outside (via good old C file) of their framework by using just one shared lib. this sounds like the need for a wrapper that encapsulates the wanted framework methods for usage with C instead of C++.
So far so good.... here is what i already did:
interface aldebaran.h
(this is in my include folder, the ultrasound methods should be called from outside of the framework):
#ifndef _ALDEBARAN_H
#define _ALDEBARAN_H
#ifdef __cplusplus
extern "C" {
#endif
void subscribe_ultrasound();
void unsubscribe_ultrasound();
float read_ultrasound();
#ifdef __cplusplus
}
#endif
#endif
now the wrapper:
cpp file aldebaran.cpp:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "aldebaran.h"
#include "alproxy.h"
#include "../../include/aldebaran.h"
/*
* Ultrasound defines
*/
#define ULTRASOUND_RESERVATION_MAGIC "magic_foobar"
#define ULTRASOUND_POLL_TIME 250
#define ULTRASOUND_READ_ATTEMPTS 50
#define ULTRASOUND_SLEEP_TIME 20
using namespace std;
using namespace AL;
/*
* Framework proxies
*/
ALPtr<ALProxy> al_tts;
ALPtr<ALProxy> al_led;
ALPtr<ALProxy> al_motion;
ALPtr<ALProxy> al_mem;
ALPtr<ALProxy> al_us;
ALPtr<ALProxy> al_cam;
ALPtr<ALProxy> al_dcm;
/*
* Constructor
*/
Aldebaran::Aldebaran(ALPtr<ALBroker> pBroker, std::string pName): ALModule(pBroker, pName)
{
try {
al_tts = this->getParentBroker()->getProxy("ALTextToSpeech");
al_led = this->getParentBroker()->getProxy("ALLeds");
al_motion = this->getParentBroker()->getProxy("ALMotion");
al_mem = this->getParentBroker()->getProxy("ALMemory");
al_us = this->getParentBroker()->getProxy("ALUltraSound");
al_cam = this->getParentBroker()->getProxy("NaoCam");
al_dcm = this->getParentBroker()->getProxy("DCM");
}catch(ALError& err){
std::cout << "XXX: ERROR: " << err.toString() << std::endl;
return 1;
}
printf("XXX: module aldebaran initiated\n");
fflush(0);
}
/*
* Destructor
*/
Aldebaran::~Aldebaran()
{
printf("XXX: module aldebaran destructed\n");
fflush(0);
}
/*
* Subscribe to ultrasound module
*/
void subscribe_ultrasound()
{
ALValue param;
param.arrayPush(ULTRASOUND_POLL_TIME);
al_us->callVoid("subscribe", string(ULTRASOUND_RESERVATION_MAGIC), param);
printf("XXX: ultrasound subscribed: %s\n", ULTRASOUND_RESERVATION_MAGIC);
fflush(0);
}
/*
* Unsubscribe to ultrasound module
*/
void unsubscribe_ultrasound()
{
al_us->callVoid("unsubscribe", string(ULTRASOUND_RESERVATION_MAGIC));
printf("XXX: ultrasound unsubscribed: %s\n", ULTRASOUND_RESERVATION_MAGIC);
fflush(0);
}
/*
* Read from ultrasound module
*/
float read_ultrasound()
{
int i;
float val1, val2;
float val_sum;
ALValue distance;
val_sum = .0f;
for(i = 0; i < ULTRASOUND_READ_ATTEMPTS; ++i){
SleepMs(ULTRASOUND_SLEEP_TIME);
distance = al_mem->call<ALValue>("getData", string("extractors/alultrasound/distances"));
sscanf(distance.toString(AL::VerbosityMini).c_str(),"[%f, %f, \"object\"]", &val1, &val2);
val_sum += val1;
}
return val_sum / (1.f * ULTRASOUND_READ_ATTEMPTS);
}
definition file for aldebaran.cpp:
#ifndef ALDEBARAN_API_H
#define ALDEBARAN_API_H
#include <string>
#include "al_starter.h"
#include "alptr.h"
using namespace AL;
class Aldebaran : public AL::ALModule
{
public:
Aldebaran(ALPtr<ALBroker> pBroker, std::string pName);
virtual ~Aldebaran();
std::string version(){ return ALTOOLS_VERSION( ALDEBARAN ); };
bool innerTest(){ return true; };
};
#endif
So this should be a simple example for my wrapper and it compiles fine to libaldebaran.so.
now my test program in C:
... now i'd like to call the interface aldebaran.h methods from a simple c file like this:
#include <stdio.h>
/*
* Begin your includes here...
*/
#include "../include/aldebaran.h"
/*
* End your includes here...
*/
#define TEST_OKAY 1
#define TEST_FAILED 0
#define TEST_NAME "test_libaldebaran"
unsigned int count_all = 0;
unsigned int count_ok = 0;
const char *__test_print(int x)
{
count_all++;
if(x == 1){
count_ok++;
return "ok";
}
return "failed";
}
/*
* Begin tests here...
*/
int test_subscribe_ultrasound()
{
subscribe_ultrasound();
return TEST_OKAY;
}
int test_unsubscribe_ultrasound()
{
unsubscribe_ultrasound();
return TEST_OKAY;
}
int test_read_ultrasound()
{
float i;
i = read_ultrasound();
return (i > .0f ? TEST_OKAY : TEST_FAILED);
}
/*
* Execute tests here...
*/
int main(int argc, char **argv)
{
printf("running test: %s\n\n", TEST_NAME);
printf("test_subscribe_ultrasound: \t %s\n", __test_print(test_subscribe_ultrasound()));
printf("test_read_ultrasound: \t %s\n", __test_print(test_read_ultrasound()));
printf("test_unsubscribe_ultrasound: \t %s\n", __test_print(test_unsubscribe_ultrasound()));
printf("test finished: %s has %u / %u tests passed\n\n", TEST_NAME, count_ok, count_all);
return (count_all - count_ok);
}
how can i manage to call these methods? i mean within my C file i have no possibility to create such an object-instance (that generated all the needed ALProxies), have i?
help would be really appreciated... thx
thank you very much so far!!
as xtofl said.. i'd like to keep my interface as simple as possible (without another c++ object preferably):
#ifndef _ALDEBARAN_H
#define _ALDEBARAN_H
#ifdef __cplusplus
extern "C" {
#endif
void subscribe_ultrasound();
void unsubscribe_ultrasound();
float read_ultrasound();
#ifdef __cplusplus
}
#endif
#endif
the problem hereby is that functions like subscribe_ultrasound() cannot be called without the instanciation of all the proxies... this is our precondition:
...
al_tts = this->getParentBroker()->getProxy("ALTextToSpeech");
al_led = this->getParentBroker()->getProxy("ALLeds");
al_motion = this->getParentBroker()->getProxy("ALMotion");
al_mem = this->getParentBroker()->getProxy("ALMemory");
al_us = this->getParentBroker()->getProxy("ALUltraSound");
al_cam = this->getParentBroker()->getProxy("NaoCam");
al_dcm = this->getParentBroker()->getProxy("DCM");
...
if i don't have the code above called, all other will fail.
within their framework it is possible to "autoload" my libaldebaran.so via a python script like this call:
myModule = ALProxy("Aldebaran", global_params.strRemoteIP, global_params.nRemotePort );
The framework log then says:
May 10 15:02:44 Hunter user.notice root: XXX: module aldebaran initiated
May 10 15:02:46 Hunter user.notice root: INFO: Registering module : 'Aldebaran'
May 10 15:02:46 Hunter user.notice root: ______ End of loading libraries ______
which is totally okay... it called the constructor of my module (so all other needed proxies got instanciated too).
but of course this instance does not belong to my C program...
maybe there is a possibility to share this to all other processes?
You might want to take a slightly different approach. Consider something like this for your C interface:
#ifdef __cplusplus
extern "C" {
#endif
struct UltrasoundHandle;
UltrasoundHandle* ultrasound_Create();
void ultrasound_Destroy(UltrasoundHandle *self):
void ultrasound_Subscribe(UltrasoundHandle *self);
void ultrasound_Unsubscribe(UltrasoundHandle *self);
float ultrasound_Read(UltrasoundHandle *self);
#ifdef __cplusplus
}
#endif
The UltrasoundHandle structure is purposefully opaque so that you can define it in the implementation to be whatever you want it to be. The other modification that I made was to add explicit creation and destruction methods akin to the constructor and destructor. The implementation would look something like:
extern "C" {
struct UltrasoundHandle {
UltrasoundHandle() {
// do per instance initializations here
}
~UltrasoundHandle() {
// do per instance cleanup here
}
void subscribe() {
}
void unsubscribe() {
}
float read() {
}
};
static int HandleCounter = 0;
UltrasoundHandle* ultrasound_Create() {
try {
if (HandleCounter++ == 1) {
// perform global initializations here
}
return new UltrasoundHandle;
} catch (...) {
// log error
}
return NULL;
}
void ultrasound_Destroy(UltrasoundHandle *self) {
try {
delete self;
if (--HandleCounter == 0) {
// perform global teardown here
}
} catch (...) {
// log error
}
}
The key is to wrapping C++ interfaces for C is to expose the OO concepts through free functions where the caller explicitly passes the object pointer (this) to the function and to explicitly expose the constructor and destructor in the same manner. The wrapper code can be almost mechanically generated from there. The other key points are that you never let exceptions propagate outward and steer clear of global object instances. I'm not sure if the latter will cause you grief, but I would be concerned about construction/destruction ordering problems.
You said yourself to create a C wrapper API around an OO framework.
This means you don't need any objects passing the wrapper API (as it appears from the decribed header). It seems all objects needed are created/destructed behind the wrapper API, out of view of your test program.
The first seems the case. You don't need objects to test your wrapper API. In the end, all objects are bytes (in memory) that are accessed through a fixed set of functions. It doesn't matter much whether the functions are written as member-functions (C++) or as plain C functions, as long as they obey the intended semantics of your objects.
I'm not clear whether you're aware of this, but if you have C++ code to dynamically load into your program, then you should link your program with the C++ compiler and make your main function a C++ function too - even if it is as trivial as:
int main(int argc, char **argv)
{
return(real_main_in_c(argc, argv));
}
The real_main_in_c() function is what you previously called main(); it has simply been renamed. This ensures that the C++ mechanisms for handling initialization of global and static variables, etc, are loaded and operational. The C++ startup code does more work than the C startup code. Dynamically loading C
This is only one (small) facet of the answer - but it is an important practical one.
Keep things as simple as possible - but not simpler. I think you're trying to make it too simple here, wrap it all up but use the c++ compiler.
so, if you create your wrapper using the c++ compiler, you can instantiate the objects inside your subscribe function, release them all in the unsubscribe, all using static (or global) objects. The 3 functions you want to expose simple get wrapped with "extern C" and you have a C-style interface exposed to any caller, whilst still encapsulating C++ objects.
If you need another function to instantiate all the proxies, add one; alternatively if they don't already exist, create them so they'll always be created in the first call to subscribe.
Now, if you need the proxy objects on a per-instance basis (ie you have 2 callers who both want to subscribe, and need a unique proxy per caller), then you'll have to store the objects in a collection (I suggest a map), every call you make must then pass in a 'handle' or 'sessionid' that you use to extract the per-call objects from the map.