2

Possible Duplicate:
C++/CLI Mixed Mode DLL Creation

I'm wrapping a C++ class using c++ cli. The header file looks like this:

pabcon.h

class PABCon {
private:
        unsigned int maxIndex;      
        long byteSize;  
public:
    __declspec(dllexport) inline unsigned int GetMaxIndex() { return this->maxIndex; };
    __declspec(dllexport) void invertData();
};

I'm wrapping non-inline functions this way:

pabconwrapper.h

public ref class PABConWrapper
{
private:
    PABCon *pabc;

public:
    PABConWrapper();
    ~PABConWrapper();
    void invertData();
};

pabconwrapper.cpp

PABConWrapper::PABConWrapper() : pabc(new PABCon())
{

}

void PABConWrapper::invertData()
{
    pabc->invertData();
}

PABConWrapper::~PABConWrapper()
{
    delete pabc;
}

My questions are:

1) What is the best way to wrap the inline c++ functions?

2) What should I do with private variables defined in pabcon.h? Should I wrap them too somehow?

Thanks

P.S. I've touched C++ last time 4 years ago and coded in C# since then.

Community
  • 1
  • 1
VladL
  • 12,769
  • 10
  • 63
  • 83
  • Do you mean inter-operate with them from C#? And you would make another function to access the private variables unless you planned on doing some sniffing around the bytes of the class – Jay Jan 18 '13 at 11:56
  • @Jay exactly, I need those inline functions in c# – VladL Jan 18 '13 at 11:57
  • Use PInvoke if you just need the calls. See http://stackoverflow.com/questions/10388405/how-to-make-c-p-invoke-code-called-from-c-sharp-thread-safe – Jay Jan 18 '13 at 12:03
  • @Jay I can't use PInvoke as the C++ functions are not static. – VladL Jan 18 '13 at 12:07
  • Make a function which is static then :) Also what are you trying to 'inline' as you can't inline something which is already inlined... – Jay Jan 18 '13 at 12:08
  • @Jay I would make them all static if I could :) But I can't really modify the c++ code. I have only compiled dll and corresponding header files – VladL Jan 18 '13 at 12:09
  • Yes, you can... (at run-time, dynamically) but No you don't have to... Create a new C++ code to do it and then call that function and again what are you trying to inline? the function is already inlined. – Jay Jan 18 '13 at 12:11
  • @Jay I showed here just a simple example. It's nearly impossible/to much overhead to make them static. I do not try to inline something, there are inline functions in unmanaged code (delivered header files) and I simply need to have them in managed code. – VladL Jan 18 '13 at 12:16
  • They are already in-lined from the managed code because the compiler compiled them with in-line... once compiled with inline you can't change that after the fact.. it's not like `const` your question makes no sense. – Jay Jan 18 '13 at 12:17
  • @Jay where? please show me this managed code. I don't want to change them. I want to use them from c# so I need to wrap them somehow. pabcon.h is unmanaged – VladL Jan 18 '13 at 12:20
  • See http://en.wikipedia.org/wiki/Inline_function or http://www.parashift.com/c++-faq/overview-inline-fns.html Your un-managed code should have inline defined on the functions it wants in-lined. Managed code is always in-lined when possible. See http://msdn.microsoft.com/en-us/library/ms235282.aspx – Jay Jan 18 '13 at 12:22
  • @Jay mate, we don't understand each other. There is an inline c++ function in pabcon.h? This is unmanaged code. See it? OK. Now I need to call this function from the c#. That simple. Do you know how? Thank you! – VladL Jan 18 '13 at 12:28
  • @HansPassant not even remotely the duplicate. Keyword is inline – VladL Jan 18 '13 at 14:29
  • Right, you really need to stop thinking that it is relevant at all. It is very unclear why you think it is. – Hans Passant Jan 18 '13 at 14:36
  • @HansPassant Not sure what you mean, but if you know the answer you could just post it here instead of marking my question as duplicate (which is definitely not). As I written, I didn't touch c++ for 4 years. I got header files delivered, seen some inline code and asked how to handle it. If the answer is obvious for you - please help, because not everyone here is as clever/experienced as you. – VladL Jan 18 '13 at 15:01

1 Answers1

2

inline: Tagging a method inline is an instruction to the compiler to not emit a function call, but to instead take the contents of the method, and put it directly into the calling function. I believe this is only advisory, the compiler can choose to emit a function call anyway. Regardless, you don't need to do anything fancy when calling this function from C++/CLI, so you don't need to do anything fancy when wrapping it. Just wrap it the same as any other method.

private variables: If they're private variables, then unmanaged C++ code using PABCon wouldn't have access to them. The public interface of PABCon is just the public methods, so that's all you need to worry about. (If the C++ class had any public variables, then you'd wrap them by creating a property in C++/CLI.)

~PABConWrapper: In C++/CLI, ~ is not the destructor, it's the dispose method. As implemented right now, you'll have a memory leak if you forget to dispose your new class. At a minimum, switch the ~ to !, and declare the finalizer instead. Ideally, implement both ! and ~, delete the unmanaged object in both methods (with a proper null check), and add proper null checks in the other methods. This would be a good & proper implementation of IDisposable.

David Yaw
  • 27,383
  • 4
  • 60
  • 93