c++-cli

convert struct handle from managed into unmanaged C++/CLI

邮差的信 提交于 2019-12-03 03:47:09
In C#, I defined a struct: [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] public struct MyObject { [MarshalAs(UnmanagedType.LPWStr)] public string var1; [MarshalAs(UnmanagedType.LPWStr)] public string var2; }; I have this struct in C++: public value struct MyObject { LPWSTR var1; LPWSTR var2; }; And in the method of C++ which is a public class to be called from C#: TestingObject(MyObject^ configObject) { // convert configObject from managed to unmanaged. } The object is debugged correctly that I can see two strings var1 and var2. However, the problem now is that I need to

Any CPU dependent on C++/CLI dependent on native C dll (any cpu for c++/cli)

情到浓时终转凉″ 提交于 2019-12-03 01:23:55
问题 Here's my problem. I am wrapping a C dll in C#. To do this, I am first writing a C++/CLI wrapper. The native C library is linked to the C++/CLI wrapper. (Linker properties in C++/cli project). Here's how it is all organised now: - The native C .lib: both x86 and 64bit. 1 solution containing 2 projects: C++/CLI wrapper project to which is linked native C .lib C# project referencing C++/CLI project My problem comes from the fact that I need C# to target "Any CPU". But this option is not

Destructors not called when native (C++) exception propagates to CLR component

风流意气都作罢 提交于 2019-12-03 01:13:33
We have a large body of native C++ code, compliled into DLLs. Then we have a couple of dlls containing C++/CLI proxy code to wrap the C++ interfaces. On top of that we have C# code calling into the C++/CLI wrappers. Standard stuff, so far. But we have a lot of cases where native C++ exceptions are allowed to propagate to the .Net world and we rely on .Net's ability to wrap these as System.Exception objects and for the most part this works fine. However we have been finding that destructors of objects in scope at the point of the throw are not being invoked when the exception propagates! After

Attempting to convert C++ into C# with interop

佐手、 提交于 2019-12-02 22:15:32
问题 I've got a program that calls to EGL in C++. I want to make the same call in C#, but there doesn't seem to be an equivalent concept in C#. I'm getting a read/write access denied error when the execution context enters the C++ EGL code. This is the code in the C++ program that I'm trying to convert to C#: PropertySet^ surfaceCreationProperties = ref new PropertySet(); surfaceCreationProperties->Insert(ref new String(EGLNativeWindowTypeProperty), somethingOtherThanAWindow); mEglSurface =

How to declare a Font variable properly? [duplicate]

与世无争的帅哥 提交于 2019-12-02 21:59:56
问题 This question already has an answer here : How to use Font as a variable? (1 answer) Closed 5 years ago . I would have a quick question : Could anyone tell me what is wrong with this line : Font ^printFont = gcnew System::Drawing::Font("Arial", 10); My compiler says "Identifier 'printFont' is unidentified". I also have the namespaces and the dll file included : #using <System.Drawing.dll> using namespace System; using namespace System::Drawing; using namespace System::Drawing::Text; using

Copy unmanaged data into managed array

こ雲淡風輕ζ 提交于 2019-12-02 20:37:59
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 produces error C2664: parameter 1 cannot be converted from "const void*" to "__w64 int". So how can it be

Concurrent File write between processes

限于喜欢 提交于 2019-12-02 20:04:43
问题 I need to write log data into a single file from different processes. I am using Windows Mutex which needs Common Language Runtime support for it. Mutex^ m = gcnew Mutex( false,"MyMutex" ); m->WaitOne(); //... File Open and Write .. m->ReleaseMutex() Do I really need to change from C++ to C++/CLI for synchronization? It is ok if the atomic is not used. But I need to know whether using this Mutex will slow down the performance compared to local mutex. 回答1: Adding CLR support to your C++

When and why would you seal a class?

为君一笑 提交于 2019-12-02 17:22:18
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? Louis Kottmann On a class that implements security features, so that the original object cannot be "impersonated". More generally, I recently exchanged with a person at Microsoft, who told me they tried

C++ Windows Forms application unhandled exception error when textbox empty

ぐ巨炮叔叔 提交于 2019-12-02 16:24:53
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 7 years ago . I'm building a temperature conversion application in Visual Studio for a C++ course. It's a Windows Forms application. My problem is, when I run the application if I don't have anything entered into either the txtFahrenheit or txtCelsius2 textboxes I get the following error: "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"

Error in C++/CLI, Can't take address of function unless creating delegate instance using Pthread

旧巷老猫 提交于 2019-12-02 15:17:51
问题 I'm using C++/CLI on Visual C++ 2008 Professional, and since I'm using Windows Forms that means I have managed code and I'm trying to call the static function LoginAccounts, but I get an error probably because I'm mixing Managed with Unmanaged Code, but I can't figure out what to do though. I'm using PThread for Windows System::Void testing_Click(System::Object^ sender, System::EventArgs^ e) { pthread_create(&t, NULL, &Contas::LoginAccounts, this); //Error in this line } Error 13 error C3374: