c++-cli

Wrapping an Unmanaged C++ Class Library with C++/CLI - Question 2 - Collections

主宰稳场 提交于 2019-12-06 00:40:46
问题 Note: This post represents Question #2 of my inquiry. The introduction block (all text until the numbers are reached) is repeated in both questions as it is background information that may be needed to answer the question. Introduction to Question I have an unmanaged C++ library that contains classes and functions that are common to and shared among several "higher level" libraries. I now have need to provide access to the common library to C#/.Net applications. To do this, I will have to

Adding Existing Form to C++/CLI WinForms Project

我的未来我决定 提交于 2019-12-05 23:31:13
问题 I have two C++/CLI projects A and B in separate solutions. I use A for experiments/testing and move the tested code to B once I am finished testing. However, I find that on adding a windows forms class (header, cpp and resx) to project B, I am no longer able to use the visual forms designer of the IDE. How do I enable that? 回答1: Got it! Open the Solution Explorer pane and the Properties pane side-by-side. Select the header file for the form class in the Solution Explorer pane. In the

how to turn C++/CLI .Net socket into boost::asio socket?

心不动则不痛 提交于 2019-12-05 23:08:38
What I want is simple - code sample of creating new boost asio socket from C++/CLI .Net socket. How to create such thing? Here is pseudo code of what I would like to do: .net::socket a; boost::asio::socket b; b.assign(a.nativeWin32Socket()); Have you tried? b.assign(a.Handle.ToInt32()); Also note they you will need to use WSADuplicateSocket as you may get that both b and a will close the socket and point you don't expect. So you need something like: SOCKET native = WSADuplicateSocket(a.Handle.ToInt32(),...); b.assign(native); Full Answer (Tested) SOCKET native = a->Handle.ToInt32(); // Now

C++ CLI Collection initializer syntax

牧云@^-^@ 提交于 2019-12-05 22:50:38
Is this supported in C++ CLI? I want to do something like the following C# example in C++ CLI var dictionary = new Dictionary<string, string> { { "foo", "bar" } }; Thanks The best I came up with was creating an array initialized inline, then initializing the dictionary with the contents of the array in a static constructor. Something like static initonly System::Collections::Generic::Dictionary<System::String^, System::String^>^ dictionary; static initonly array<System::String^> arrayToPopulateDictionary = gcnew array<System::String^> { "foo", "bar" }; static Foo() { for (int i = 0; i <

How to pass a C++ short* to managed C# assembly in C++/CLI

≡放荡痞女 提交于 2019-12-05 22:42:39
I'm having trouble passing an argument from C++/CLI code into a .NET C# function. In C++ I have something resembling the following: void SomeFunction(short *id) { CSharpClass::StaticClassInstance->SetValue(id); } On the C# side, the function is declared with a ref argument as: public void SetValue(ref short id) { id = this.internalIdField; } The compiler error I'm getting when calling SetValue(id) is "cannot convert parameter 1 from 'short *' to 'short %'". I found out that a tracking reference (%) is equivalent to C# ref but I don't know how to use it with the short* parameter I'm trying to

Using .NET class from native C++ using C++/CLI as a 'middleware'

亡梦爱人 提交于 2019-12-05 22:29:14
I have to use a class/assembly made in C# .NET from native C++ application. I suppose I need to make a wrapper class in C++/CLI, that would expose native methods in header files, but use .NET class as needed. Thing that's unclear to me is how to convert data types from .NET to standard C++ types. Does anybody have some sample code to learn from? Only text I have found on this is: http://msdn.microsoft.com/en-us/magazine/cc300632.aspx But text is very old (using Managed C++, not C++/CLI), and lot of things remains unclear Yes you need to build a wrapper with C++/CLI around your managed assembly

c++/cli static constructor of derived class is not called

限于喜欢 提交于 2019-12-05 22:24:22
As described in another SO post of me I saw a strange behaviour of my application after moving from VS 2008 (.net 3.5) to VS 2013 (and using .net 4.0, not 4.5). I found that the static constructor (cctor) of a class was not called any more. Therefore I broke the application down into a small test program: DLLs testAssembly_2-0 and testAssembly_4-0 (similar content; testAssembly_4-0 has names with 40 instead of 20 ) namespace testAssembly_20 { public ref class Class20 { public: Class20 () { Console::WriteLine (__FUNCTION__"()"); } static Class20 () { Console::WriteLine (__FUNCTION__"()" + " ms

Can I implement .ToString() on C++ structs for debugging purposes?

你。 提交于 2019-12-05 22:18:56
问题 In C#, if I define a struct, I can also override ToString(). Then when I'm debugging and I add a watch or hover my mouse over an instance of the struct, the tooltip will be the computed ToString() rather than the type name of the struct. Can I do that in C++ and/or C++/CLI somehow? That is, can I define a method as part of the struct (or do anything else) that will cause the watch-value/tooltip to display a string of my choosing? The default string rendering in Visual Studio for C/C++ is a

Memory leak in MATLAB > MEX file > managed DLL

眉间皱痕 提交于 2019-12-05 22:04:01
My MEX file is written in C++/CLI and calls a DLL written in C#. When gcnew'ing an object, shouldn't it be garbage collected when the mexFunction returns? Its references should be lost but nothing seems to be garbage collected... each call to the mex function increases MATLAB 's memory allocation (and no, the memory is not used for MATLAB variables). I've experimented with creating a large dummy value with narrow scope and when stepping through the MEX file I can see the memory allocated and released. But not so with the main object created in the mexFunction =( I've tried to delete it in the

Visual C++: Unable to invoke method from another class

放肆的年华 提交于 2019-12-05 21:07:56
Please have a look at the following code Form1.h #pragma once #include "Clicker.h" namespace TestWindowProject { using namespace System; using namespace System::ComponentModel; using namespace System::Collections; using namespace System::Windows::Forms; using namespace System::Data; using namespace System::Drawing; /// <summary> /// Summary for Form1 /// </summary> public ref class Form1 : public System::Windows::Forms::Form { public: Form1(void) { InitializeComponent(); // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary>