Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In your real use-case can you simply make FunctionToBePointed a <strong>static method</strong>?</p> <pre><code>static std::string Class1::FunctionToBePointed(std::string msg) { return msg; } </code></pre> <p>If yes your code should work.</p> <p>The reason is that <strong>instance methods</strong> are implicitly called with an <strong>hidden <em>this</em> pointer</strong>, this is the <strong><em>thiscall</em> calling convention</strong>, whereas <strong>static methods</strong> simply use the <strong><em>cdecl</em> convention</strong> because they don't work on any instance.</p> <p><strong>EDIT:</strong></p> <p>A sample with <strong>Boost::bind</strong>:</p> <p>The MyClass C# class:</p> <pre><code>using System; using System.ComponentModel; public class MyClass : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged = delegate{}; private string name; public string Name { get { return name; } set { if (name != value) { name = value; PropertyChanged(this, new PropertyChangedEventArgs("Name")); } } } } </code></pre> <p>The C++/CLI wrapper:</p> <p>Wrapper.h:</p> <pre><code>class WrapperPrivateStuff; class __declspec(dllexport) UnmanagedWrapperClass { private: WrapperPrivateStuff* _private; public: void changeIt(std::string newName); public: void WrapperMethod(boost::function&lt;std::string(std::string)&gt; GetCallBack); public: UnmanagedWrapperClass(); }; </code></pre> <p>Wrapper.cpp:</p> <pre><code>#using "MyClass.dll" #include &lt;boost/signals2.hpp&gt; #include &lt;boost/bind.hpp&gt; #include "Wrapper.h" #include &lt;msclr\auto_gcroot.h&gt; #include &lt;msclr\marshal_cppstd.h&gt; #include &lt;msclr\event.h&gt; class WrapperPrivateStuff { public: boost::signals2::signal&lt;void(std::string)&gt;signalEvent; public: msclr::auto_gcroot&lt;MyClass^&gt; a; public: void EventHandlerWrapper(System::Object^, System::ComponentModel::PropertyChangedEventArgs^ args) { this-&gt;signalEvent(msclr::interop::marshal_as&lt;std::string&gt;(a-&gt;Name)); } public: WrapperPrivateStuff() { a = gcnew MyClass(); a-&gt;PropertyChanged += MAKE_DELEGATE(System::ComponentModel::PropertyChangedEventHandler, EventHandlerWrapper); } BEGIN_DELEGATE_MAP(WrapperPrivateStuff) EVENT_DELEGATE_ENTRY(EventHandlerWrapper, System::Object^, System::ComponentModel::PropertyChangedEventArgs^) END_DELEGATE_MAP() }; void UnmanagedWrapperClass::changeIt(std::string newName) { this-&gt;_private-&gt;a-&gt;Name = msclr::interop::marshal_as&lt;System::String^&gt;(newName); } UnmanagedWrapperClass::UnmanagedWrapperClass() { this-&gt;_private = new WrapperPrivateStuff(); } void UnmanagedWrapperClass::WrapperMethod(boost::function&lt;std::string(std::string)&gt; GetCallBack) { _private-&gt;signalEvent.connect(GetCallBack); } </code></pre> <p>And the native application, test.cpp:</p> <pre><code>#include &lt;iostream&gt; #include &lt;boost/bind.hpp&gt; #include &lt;boost/function.hpp&gt; #include "Wrapper.h" class Class1 { private: std::string name; public: Class1(std::string name) : name(name) { } public: std::string FunctionToBePointed(std::string msg) { std::cout &lt;&lt; "Hey it's " &lt;&lt; name &lt;&lt; "! Got: " &lt;&lt; msg &lt;&lt; std::endl; return msg; } }; int main(void) { UnmanagedWrapperClass wrapper; Class1 class1("Ed"); wrapper.WrapperMethod(boost::bind(&amp;Class1::FunctionToBePointed, &amp;class1, _1)); wrapper.changeIt("azerty"); return 0; } </code></pre> <p>Result:</p> <pre><code>&gt;test.exe Hey it's Ed! Got: azerty </code></pre> <p>I have a more generic solution but it is really ugly. :(</p> <p>Let me know if this fix your issue...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload