c++-cli

C# to C++ process with WM_COPYDATA passing struct with strings

ⅰ亾dé卋堺 提交于 2019-12-03 12:04:35
From a c# program I want to use WM_COPYDATA with SendMessage to communicate with a legacy c++/cli MFC application. I want to pass a managed struct containing string objects. I can find the handle to the c++ application for use with SendMessage fine. The bit I don't know about is how the struct and it's strings can be marshalled and read at the other end. Especially as it contains non-blittables. Do people think this is feasible? I'll continue to work on it, but would apprecite someone who's done this sort of thing telling me if it just isn't going to work. Here is some demo code if it was a c+

this == null // How can it be possible?

守給你的承諾、 提交于 2019-12-03 11:53:07
问题 Recently I came across some strange behaviour of my application. It has been developed mainly in C# but CLI/C++ was also used to achieve better performance. I was getting a System.NullReferenceException in a very simple method at the TimeSpan comparison: TimeSpan _timestamp; void UpdateFrame(TimeSpan timestamp) { if(TimeSpan::Equals(_timestamp, timestamp) == false) It was obvious that the only reference used in this expression was implicit this (this._timestamp). I added an assert statement

How do people handle warning C4793: 'some_function' : function compiled as native?

梦想的初衷 提交于 2019-12-03 09:58:45
I'm using the OpenCV library and one of its header files, cxoperations.hpp, generates "warning C4793: 'anonymous namespace'::CV_XADD' : function compiled as native" , if my C++ project is compiled with CLR support. I can prevent the warning by surrounding the OpenCV header include like this: #pragma managed(push,off) #include <cv.h> #pragma managed(pop) But the project that actually uses OpenCV isn't compiled with CLR support, it's a native C++ static library. The project that does have CLR support, and generates this warning without the pragma statements, simply uses this static library. So I

ref and out in C++/CLI

孤人 提交于 2019-12-03 09:06:52
问题 I know that the C++/CLI code void foo(Bar^% x); transforms into Void foo(ref Bar x); What is the C++/CLI code that becomes Void foo(out Bar x); ? 回答1: You can use the OutAttribute: using namespace System::Runtime::InteropServices; void foo([Out] Bar^% x); 回答2: There is no such specific syntax in C++/CLI. I think you can get fairly close by adding the OutAttribute to modify the parameter. But I'm not sure that achieves the exact same semantics as C# out . The concept of out is for the most

Convert a 'System::String ^' to 'const char *' in vc++

人走茶凉 提交于 2019-12-03 07:55:39
How can I convert a 'System::String ^' to 'const char *' in vc++? My code: String ^Result1= "C:/Users/Dev/Desktop/imag.jpg"; IplImage *img1 = cvLoadImage(Result1, 1); if I do like above it will generate following error. error C2664: 'cvLoadImage' : cannot convert parameter 1 from 'System::String ^' to 'const char *' Please help me. It's like this: How to convert from System::String* to Char* in Visual C++ System::String ^ str = "Hello world\n"; //method 1 pin_ptr<const wchar_t> str1 = PtrToStringChars(str); wprintf(str1); //method 2 char* str2 = (char*)Marshal::StringToHGlobalAnsi(str)

Universal Windows Platform Apps and C++/CLI (VS 2015 RC1)

孤者浪人 提交于 2019-12-03 07:45:38
I have some C++/CLI code which derives from the .NET System Namespace classes. Is there a way to reuse this code for Universal Windows Platform Apps? I can't get a reference to the System Namespace in C++, though in C# it is possible. It looks like there is only support for C++/Cx code and not for managed C++/CLI. The syntax and keywords of the C++/CX extension resembles C++/CLI a great deal. But that's where similarity ends, they have nothing whatsoever in common. C++/CX gets compiled directly to native code, just like native C++. But C++/CLI is compiled to MSIL, the intermediate language of

Copy unmanaged data into managed array

不打扰是莪最后的温柔 提交于 2019-12-03 06:59:45
问题 I need to copy native (i.e. unmanaged) data (byte*) to managed byte array with C++/CLI (array). I tried Marshal::Copy (data is pointed to by const void* data and is dataSize bytes) array<byte>^ _Data=gcnew array<byte>(dataSize); System::Runtime::InteropServices::Marshal::Copy((byte*)data, _Data, 0, dataSize); This gives error C2665: none of the 16 overloads can convert all parameters. Then I tried System::Runtime::InteropServices::Marshal::Copy(new IntPtr(data), _Data, 0, dataSize); which

Is there a C++/CLI smart pointer project (e.g. scoped_ptr)?

感情迁移 提交于 2019-12-03 05:35:30
Is there a C++/CLI RAII smart pointer class for containment of a native pointer in a managed type? Just wondering, before I go write my own clr_scoped_ptr value class template. I'm aware of the Microsoft-provided: containment of a managed handle in a native class: auto_gcroot containment of a managed handle in a managed class: auto_handle The above two are similar to auto_ptr or unique_ptr . I gave skeleton code for a counted_handle here, similar to shared_ptr But all these are for disposing managed ref class instances, not for freeing native objects. Ben Voigt This one looks fairly complete,

Understanding gcroot

*爱你&永不变心* 提交于 2019-12-03 04:31:19
I have been reading this article to understand the gcroot template. I understand the gcroot provides handles into the garbage collected heap and that the handles themselves are not garbage collected. What I don't understand is the following: When the CLR object moves with the garbage-collected heap, the handle will return the new address of the object. A variable does not have to be pinned before it is assigned to a gcroot template. Does that mean that the CLR object will be deleted by the garbage collector even if there is a gcroot handle referencing that object? What is the "new address"

When and why would you seal a class?

走远了吗. 提交于 2019-12-03 04:01:03
问题 In C# and C++/CLI the keyword sealed (or NotInheritable in VB) is used to protect a class from any inheritance chance (the class will be non-inheritable). I know that one feature of object-oriented programming is inheritance and I feel that the use of sealed goes against this feature, it stops inheritance. Is there an example that shows the benefit of sealed and when it is important to use it? 回答1: On a class that implements security features, so that the original object cannot be