Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I found something that at least begins to answer my own question. The following two links have wmv files from Microsoft that demonstrate using a C# class in unmanaged C++.</p> <p>This first one uses a COM object and regasm: <a href="http://msdn.microsoft.com/en-us/vstudio/bb892741" rel="nofollow">http://msdn.microsoft.com/en-us/vstudio/bb892741</a>.</p> <p>This second one uses the features of C++/CLI to wrap the C# class: <a href="http://msdn.microsoft.com/en-us/vstudio/bb892742" rel="nofollow">http://msdn.microsoft.com/en-us/vstudio/bb892742</a>. I have been able to instantiate a c# class from managed code and retrieve a string as in the video. It has been very helpful but it only answers 2/3rds of my question as I want to instantiate a class with a string perimeter into a c# class. As a proof of concept I altered the code presented in the example for the following method, and achieved this goal. Of course I also added a altered the {public string PickDate(string Name)} method to do something with the name string to prove to myself that it worked.</p> <pre><code>wchar_t * DatePickerClient::pick(std::wstring nme) { IntPtr temp(ref);// system int pointer from a native int String ^date;// tracking handle to a string (managed) String ^name;// tracking handle to a string (managed) name = gcnew String(nme.c_str()); wchar_t *ret;// pointer to a c++ string GCHandle gch;// garbage collector handle DatePicker::DatePicker ^obj;// reference the c# object with tracking handle(^) gch = static_cast&lt;GCHandle&gt;(temp);// converted from the int pointer obj = static_cast&lt;DatePicker::DatePicker ^&gt;(gch.Target); date = obj-&gt;PickDate(name); ret = new wchar_t[date-&gt;Length +1]; interior_ptr&lt;const wchar_t&gt; p1 = PtrToStringChars(date);// clr pointer that acts like pointer pin_ptr&lt;const wchar_t&gt; p2 = p1;// pin the pointer to a location as clr pointers move around in memory but c++ does not know about that. wcscpy_s(ret, date-&gt;Length +1, p2); return ret; } </code></pre> <p>Part of my question was: What is better? From what I have read in many many efforts to research the answer is that COM objects are considered easier to use, and using a wrapper instead allows for greater control. In some cases using a wrapper can (but not always) reduce the size of the thunk, as COM objects automatically have a standard size footprint and wrappers are only as big as they need to be. </p> <p>The thunk (as I have used above) refers to the space time and resources used in between C# and C++ in the case of the COM object, and in between C++/CLI and native C++ in the case of coding-using a C++/CLI Wrapper. So another part of my answer should include a warning that crossing the thunk boundary more than absolutely necessary is bad practice, accessing the thunk boundary inside a loop is not recommended, and that it is possible to set up a wrapper incorrectly so that it double thunks (crosses the boundary twice where only one thunk is called for) without the code seeming to be incorrect to a novice like me.</p> <p>Two notes about the wmv's. First: some footage is reused in both, don't be fooled. At first they seem the same but they do cover different topics. Second, there are some bonus features such as marshalling that are now a part of the CLI that are not covered in the wmv's. </p> <p>Edit:</p> <p>Note there is a consequence for your installs, your c++ wrapper will not be found by the CLR. You will have to either confirm that the c++ application installs in any/every directory that uses it, or add the library (which will then need to be strongly named) to the GAC at install time. This also means that with either case in development environments you will likely have to copy the library to each directory where applications call it.</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.
    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