Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 295 Vote(s) - 3.65 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Wrapping a C++ object in extern "C"

#1
consider a simple example class:

class BankAccount {
public:
BankAccount() { balance =0.0; };
~BankAccount() {};
void deposit(double amount) {
balance += amount;
}
private:
double balance;
};

Now say I want to wrap this in extern "C" so that I can call it from many different programming languages such as C# and Java.
I tried the following which seemed to work:

// cbankAccount.h:
extern "C" unsigned long createBackAccount();
extern "C" void deposit(unsigned long bankAccount, double amount);
// cbankAccount.cpp
unsigned long createBackAccount() {
BankAccount *b = new BankAccount();
return (unsigned long) b;
}
void deposit(unsigned long bankAccount, double amount) {
BankAccount *b = (BankAccount*) bankAccount;
b->deposit(amount);
}
Is this portable?
Is the type unsigned "unsigned long" large enough for an object pointer?
Any other problems with this approach?

Thank in advance for any answers!






Reply

#2
This is not portable, because `unsigned long` may be not long enough for a pointer. A not so rare platform where this happens is win64.

Better to use `ptrdiff_t` or `void*`.
Reply

#3
Yeah. It's bad. Really bad. `unsigned long`- just no. Return a properly typed `BankAccount*`- the other languages will see it on the other end as a generic pointer (such as System.IntPtr) and there's no need to return an untyped pointer when the binary interface doesn't type pointers anyway.

extern "C" BankAccount* CreateBankAccount() {
return new BankAccount;
}
extern "C" void deposit(BankAccount* account, double amount) {
account->deposit(amount);
}
Reply

#4
It would probably be platform dependent whether long is large enough (probably not on x86-64) an alternative is to use something like [swig][1]


[1]:

[To see links please register here]

Reply

#5
I'd use void* instead of unsigned long.
Reply

#6
That basically looks fine with one proviso. Trying to use an integer type to hold a pointer is not a great idea—much better to use `void*` since that, by definition, is the width of a pointer.


----------
Actually, I think @DeadMG's answer is a cleaner approach than this.

Reply

#7
Type-strong is even better than void *.

typedef struct BankAccountProxy * BankAccountPtr;

BankAccountPtr createBackAccount() {
BankAccount *b = new BankAccount();
return (BankAccountPtr) b;
}

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through