c++-cli

Managed C++ Static Constructor not called in .net4

泄露秘密 提交于 2020-01-02 18:38:08
问题 I've recently moved a project I'm working on from .NET 3.5 to .NET 4. I'm using C#, Managed C++ and Unmanaged C++. In one of my Managed C++ (interop) I'm having a static constructor: public ref class StaticPool : public BaseStaticPools { public: static StaticPool() { InitializePools(); } static Poolable^ Dequeue() { return (Poolable^)Dequeue(Poolable::typeid); } private: static void InitializePools() { BaseStaticPools::CreatePool(Poolable::typeid); } }; In .NET 3.5 once Dequeue() had been

C++ CLI Collection initializer syntax

你离开我真会死。 提交于 2020-01-02 08:46:08
问题 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 回答1: 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^>

Access violation when using pin_ptr?

拈花ヽ惹草 提交于 2020-01-02 07:23:28
问题 When I use pin_ptr to pass an array in native c code, I get access violation. The code is as bellow: array<float>^ LogLikelihoodScore(array<array<unsigned char>^>^ modelsBuffer , array<float>^ featuresArray, int numberOfFrames) { int i, j, modelsNum = modelsBuffer->Length, len; float **models = (float**) malloc(modelsNum * sizeof(void*)); for(i = 0; i < modelsNum; i++) { pin_ptr<unsigned char> ptr = &modelsBuffer[i][0]; models[i] = (float*) ptr; } array<float>^ scores = gcnew array<float>

How to debug and solve a 'DisconnectedContext' crash?

我的梦境 提交于 2020-01-02 03:59:26
问题 I have a GUI app which connects to a sensor, gathers data and processes it in the background using BackgroundWorker threads. As it stands I'm posting data to the GUI using the ProgressChanged which seemed to be working well to begin with. I've since upped the data rates and have discovered a problem; if the software is left to run for a few minutes, the amount of processor usage appears to ramp up until it reaches near 100% on both cores on my machine and at that point, I get an error which

How to wrap C library callbacks in C++/CLI

ぐ巨炮叔叔 提交于 2020-01-01 17:13:50
问题 Given the following C library with a callback event that ask to set a buffer, how to write a proper C++/CLI wrapper in a type safe manner? // The callback signature typedef void (__cdecl *BUFFERALLOCATOR)(void *opaque, void **buffer); // A struct that contains the context of the library struct lib_context_base_s { // The stored callback function pointer BUFFERALLOCATOR buffer_allocator; // Opaque pointer that contain the local context. Needed in C because // C doesn't have closures (functions

What is the C++ equivalent of the C# checked

强颜欢笑 提交于 2020-01-01 14:53:26
问题 I'm looking to avoid an overflow in managed C++ (CLI). In C# there is an unchecked keyword, and in C++ overflows do not end up in exceptions. For reference, unchecked is documented here. Basically if you do: unchecked { int1 = 2147483647 + 10; //this overflows in CLI but is ok in C# and C++ } In C# it will not overflow but convert to int by taking the least significant bits. This is appropriate when you compute hash codes for example. Note: I realize there is no equivalent C++ keyword, but

Is it possible to get a pointer to String^'s internal array in C++/CLI?

試著忘記壹切 提交于 2020-01-01 11:50:57
问题 The goal is to avoid copying the string data when I need a const wchar_t* . The answer seems to be yes, but the function PtrToStringChars doesn't have its own MSDN entry (it's only mentioned in the KB and blogs as a trick). That made me suspicious and I want to check with you guys. Is it safe to use that function? 回答1: Yes, no problem. It is actually somewhat documented but hard to find. The MSDN docs for the C++ libraries aren't great. It returns an interior pointer, that's not suitable for

Error Compiling C++/CLI Delegate call using Predicate with Array::FindAll()

北城以北 提交于 2020-01-01 09:25:08
问题 The following code results in C3867 (...function call missing argument list...) and C3350 (...a delegate constructor expects 2 argument(s)...). What am I doing wrong? public ref class Form1 : public System::Windows::Forms::Form { public: bool IsEven(int i){ return (i % 2) == 0; } Form1(void) { numbers = gcnew array<int>{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; array<int> ^even = Array::FindAll( numbers, gcnew Predicate<int>(IsEven)); } }; 回答1: In C++/CLI you have to pass the actual instance of the

Implementing IEnumerable<T> in C++/CLI

天大地大妈咪最大 提交于 2020-01-01 08:04:46
问题 I'm having problems implementing IEnumerable<T> in my custom collection class in C++/CLI. Here is the relevant part of the code: using namespace System::Collections::Generic; ref class MyCollection : IEnumerable<MyClass^> { public: MyCollection() { } virtual IEnumerator<MyClass^>^ GetEnumerator() { return nullptr; } }; When compiled, this results in the following errors: error C2392: 'System::Collections::Generic::IEnumerator ^MyCollection::GetEnumerator(void)': covariant returns types are

Implementing IEnumerable<T> in C++/CLI

蓝咒 提交于 2020-01-01 08:03:12
问题 I'm having problems implementing IEnumerable<T> in my custom collection class in C++/CLI. Here is the relevant part of the code: using namespace System::Collections::Generic; ref class MyCollection : IEnumerable<MyClass^> { public: MyCollection() { } virtual IEnumerator<MyClass^>^ GetEnumerator() { return nullptr; } }; When compiled, this results in the following errors: error C2392: 'System::Collections::Generic::IEnumerator ^MyCollection::GetEnumerator(void)': covariant returns types are