Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes. You can. Actually, not just <code>std::string</code>, <code>std::wstring</code>, any standard C++ class or your own classes can be marshaled or instantiated and called from C#/.NET.</p> <p>The basic idea of instantiating a C++ object from .NET world is to allocate exact size of the C++ object from .NET, then call the constructor which is exported from the C++ DLL to initialize the object, then you will be able to call any of the functions to access that C++ object, if any of the method involves other C++ classes, you will need to wrap them in a C# class as well, for methods with primitive types, you can simply P/Invoke them. If you have only a few methods to call, it would be simple, manual coding won't take long. When you are done with the C++ object, you call the destructor method of the C++ object, which is a export function as well. if it does not have one, then you just need to free your memory from .NET.</p> <p>Here is an example.</p> <pre><code>public class SampleClass : IDisposable { [DllImport("YourDll.dll", EntryPoint="ConstructorOfYourClass", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)] public extern static void SampleClassConstructor(IntPtr thisObject); [DllImport("YourDll.dll", EntryPoint="DoSomething", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)] public extern static void DoSomething(IntPtr thisObject); [DllImport("YourDll.dll", EntryPoint="DoSomethingElse", CharSet=CharSet.Ansi, CallingConvention=CallingConvention.ThisCall)] public extern static void DoSomething(IntPtr thisObject, int x); IntPtr ptr; public SampleClass(int sizeOfYourCppClass) { this.ptr = Marshal.AllocHGlobal(sizeOfYourCppClass); SampleClassConstructor(this.ptr); } public void DoSomething() { DoSomething(this.ptr); } public void DoSomethingElse(int x) { DoSomethingElse(this.ptr, x); } public void Dispose() { Marshal.FreeHGlobal(this.ptr); } } </code></pre> <p>For the detail, please see the below link,</p> <p><a href="http://www.xinterop.com/index.php/2013/04/13/introduction-to-c-pinvoke-interop-sdk/" rel="nofollow">C#/.NET PInvoke Interop SDK</a></p> <p>(I am the author of the SDK tool)</p> <p>Once you have the C# wrapper class for your C++ class ready, it is easy to implement <code>ICustomMarshaler</code> so that you can marshal the C++ object from .NET.</p> <p><a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustommarshaler.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.icustommarshaler.aspx</a></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. This table or related slice is empty.
    1. This table or related slice is empty.
    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