c++-cli

Why do global native objects cause problems in C++/CLI dlls?

徘徊边缘 提交于 2019-12-23 18:30:28
问题 I have read somewhere that it has something to do with non-simple global native objects that have constructors/destructors. Can someone explain what it is exactly that may cause trouble? 回答1: An Example: Once I tried to link a native C++ lib into a C++/CLI application. That lib contained a const static std::string . The application did always crash. Reason: When the application starts, it initializes the const static objects. To make sure that the static objects get deleted properly on

C++/CLI: inherit from one CLR class, multiple C++ classes

偶尔善良 提交于 2019-12-23 16:26:53
问题 In C++/CLI, I want a class hierarchy similar to the following: Foo FooA : Foo, ClrClassA FooB : Foo, ClrClassB Is it possible for FooA to share a (non CLR) base class while also inheriting from separate CLR classes? If not, what would be the best way for FooA and FooB to share common code? 回答1: Generally speaking, composition is often better than inheritance as it tends to lead to less tightly coupled designs. If you're mixing managed and unmanaged code, it's generally easier in my experience

Do you recommend Enabling Code Analysis for C/C++ on Build?

依然范特西╮ 提交于 2019-12-23 16:14:10
问题 I'm using Visual Studio 2010, and in my C++/CLI project there are two Code Analysis settings: Enable Code Analysis on Build Enable Code Analysis for C/C++ on Build My question is about the second setting. I've enabled it and it takes a long time to run and it doesn't find much. Do you recommend enabling this feature? Why? 回答1: Never did anything for me. In theory, it's supposed to help catch logical errors, but I've never found it to report anything. 回答2: The two options you specify control

Access violation exception when deleting an array

别来无恙 提交于 2019-12-23 16:00:11
问题 I'm getting "Access violation reading location" exception when deleting the allocated memory as follow. I have a native dll compiled against Visual Studio 2010(toolset: v100) C++ compiler.I have a managed dll wrapper for it which is compiled against toolset v90 as I want to target .net 2.0. The managed wrapper passes the reference to pointer (double *&myArray) to one of the native dll function call, which internally creates a dynamic array and initializes the data. However, when managed

How to call static method from another class?

喜欢而已 提交于 2019-12-23 15:08:55
问题 I am trying to call a static method from a.h to b.cpp. from what I have researched, it is as simple as just putting a :: scope resolution but however I tried and it throws me an error "C++ requires a type specifier for all declarations". below is what I have. a.cpp float method() { //some calculations inside } a.h static float method(); b.cpp a::method(); <- error! "C++ requires a type specifier for all declarations". but if I type without the :: a method(); <- doesn't throw any errors. I

C++\CLI - How to convert UInt64^% to unsigned long long*

↘锁芯ラ 提交于 2019-12-23 13:27:34
问题 I have a C++ function that calculates a buffer size: CalcBuffer(unsigned long long* bufferSize); I want to pass this result into a different function later in my C# code. My C++\CLI Wrapper looks like this: CalcBufferWrapper([Out] UInt64^% bufferSize){ CalcBuffer(bufferSize); } But I'm getting this error: argument of type "System::UInt64 ^" is incompatible with parameter of type "unsigned long long *". Obviously, I need to use a different type, but which type? 回答1: UInt64^% doesn't mean what

Why is my UDP broadcast failing?

你离开我真会死。 提交于 2019-12-23 13:04:20
问题 I'm trying to send a UDP broadcast but wireshark isn't reporting any traffic. Here's the snippet that does the sending: void SendBroadcast() { String^ ip = "255.255.255.255"; int port = 30718; String^ message = "test"; UdpClient^ udpClient = gcnew UdpClient(); udpClient->EnableBroadcast = true; IPEndPoint^ ipDest = gcnew IPEndPoint(IPAddress::Parse(ip), port); cli::array<unsigned char>^ dgram = Encoding::ASCII->GetBytes(message); int bytesSent = udpClient->Send(dgram, dgram->Length, ipDest);

Why is my UDP broadcast failing?

别等时光非礼了梦想. 提交于 2019-12-23 13:02:06
问题 I'm trying to send a UDP broadcast but wireshark isn't reporting any traffic. Here's the snippet that does the sending: void SendBroadcast() { String^ ip = "255.255.255.255"; int port = 30718; String^ message = "test"; UdpClient^ udpClient = gcnew UdpClient(); udpClient->EnableBroadcast = true; IPEndPoint^ ipDest = gcnew IPEndPoint(IPAddress::Parse(ip), port); cli::array<unsigned char>^ dgram = Encoding::ASCII->GetBytes(message); int bytesSent = udpClient->Send(dgram, dgram->Length, ipDest);

c++ sealed and interface

爱⌒轻易说出口 提交于 2019-12-23 12:43:32
问题 I noticed that there are sealed and interface keywords in C++. Is this just for CLR C++? If not, when were sealed and interface added to the C++ standard? Do they have the same meaning in C++ as they do in C#? If not, how do I get the equivalent in standard C++? 回答1: sealed and interface keywords are only for C++/CLI. See Language Features for Targeting the CLR for more details. In standard C++ interface could be replaced with pure virtual class and multiple inheritance. Sealed keyword could

Convert C++/CLI String Array into a vector of strings

感情迁移 提交于 2019-12-23 10:54:34
问题 I have a parameter in C++/CLI as follows: array<String^>^ list I want to be able to convert this into a vector of strings. How would I go about doing this? Not as good with C++/CLI as I want to be. 回答1: MSDN provides some detail on how to marshal data. They also provide some standard implementation for msclr::marshal_as w.r.t. std::string . The cli::array is a little more complex, the key for the general case here is to pin the array first (so that we don't have it moving behind our backs).