c++-cli

C# SendMessage to C++ WinProc

醉酒当歌 提交于 2019-12-05 15:47:26
I need to send a string from C# to a C++ WindowProc. There are a number of related questions on SO related to this, but none of the answers have worked for me. Here's the situation: PInvoke: [DllImport("user32", CharSet = CharSet.Auto)] public extern static int SendMessage(IntPtr hWnd, uint wMsg, IntPtr wParam, string lParam); C#: string lparam = "abc"; NativeMethods.User32.SendMessage(handle, ConnectMsg, IntPtr.Zero, lparam); C++: API LRESULT CALLBACK HookProc (int code, WPARAM wParam, LPARAM lParam) { if (code >= 0) { CWPSTRUCT* cwp = (CWPSTRUCT*)lParam; ... (LPWSTR)cwp->lParam <-- BadPtr ..

C++: Getting the c3859 error code while compiling in a mix of C++/CLI and C++

徘徊边缘 提交于 2019-12-05 15:44:38
问题 After I have done as suggested by Hans Passant in C++: Getting the "error C2065: 'pst' : undeclared identifier" while using pstsdk? (which works), my code now looks like this: private: System::Void readPstFileButton_Click(System::Object^ sender, System::EventArgs^ e) { pstsdk::pst myfile(marshal_as<std::wstring>(fileNameTextBox->Text)); } And I now get the following errors: error C3859: virtual memory range for PCH exceeded; please recompile with a command line option of '-Zm111' or greater

Find programmatically if under C++ or C++/CLI

≯℡__Kan透↙ 提交于 2019-12-05 14:58:48
问题 I would like my C++/CLI headers to compile even when under another platform. Of course I am not expecting to compile them but just ignore them. Would this be appropriate ? (_MSC_VER) #ifdef _MSC_VER using namespace System; namespace ENMFP { public ref struct Data { }; } #endif Thanks ! 回答1: You can use the __cplusplus_cli predefined macro documented here: #ifdef __cplusplus_cli using namespace System; namespace ENMFP { public ref struct Data { // ... }; } #endif // __cplusplus_cli 来源: https:/

How can I deterministically dispose of a managed C++/CLI object from C#?

落爺英雄遲暮 提交于 2019-12-05 14:36:00
问题 I have a managed object in a C++/CLI assembly. Being C++/CLI, it implements the Disposable pattern through its "destructor" (yes, I'm aware it's not the same as a standard C++ destructor). From C++/CLI, I would simply delete the object. However, I am using this object as a member variable in a C# class. From my C# class, then, I would like to call the equivalent of the Dispose() method on the C++/CLI object when I am finished using it. Since it is (and must be) a member variable of the class,

How to check a generic type in C++/CLI?

三世轮回 提交于 2019-12-05 14:33:20
In C++/CLI code I need to check if the type is a specific generic type. In C# it would be: public static class type_helper { public static bool is_dict( Type t ) { return t.IsGenericType && t.GetGenericTypeDefinition() == typeof(IDictionary<,>); } } but in cpp++\cli it does not work the same way, compiler shows the syntax error: class type_helper { public: static bool is_dict( Type^ t ) { return t->IsGenericType && t->GetGenericTypeDefinition() == System::Collections::Generic::IDictionary<,>::typeid; } }; The best way I find is compare strings like this: class type_helper { public: static bool

Error <mutex> is not supported when compiling with /clr or /clr:pure

人盡茶涼 提交于 2019-12-05 14:07:02
I have a C++ dll which I will call Dll A where I have used: #include <mutex> Dll As properties are set to "No Common Language Runtime Support" and it builds successfully. I have another Dll B which includes DLL A in its references. Dll Bs properties are set to: "Common Language Runtime Support (/clr)" as it includes C++/CLI code. When I build DLL B I get the error: C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\mutex(8): fatal error C1189: #error : <mutex> is not supported when compiling with /clr or /clr:pure. I know that I cannot include in a CLR supported DLL but is there a

C++/CLI use of Action<…,…> and Func<…> unsupported?

别说谁变了你拦得住时间么 提交于 2019-12-05 13:58:38
it does not look like there is support for the Action and Func delegates in the System namespace in C++/CLI. At least not for multiple generic arguments such as: System::Action<int, int>^ action = nullptr; System::Func<int, int>^ func = nullptr; Both result in errors such as: error C2977: 'System::Action' : too many generic arguments error C2955: 'System::Action' : use of class generic requires generic argument list Only single argument Action works: System::Action<int>^ action = nullptr; Anyone, knows why or what is missing to make this work. I am using Visual Studio 2008 and the project has

How do I convert a cli::array to a native array from native code?

最后都变了- 提交于 2019-12-05 13:12:02
I'm writing a native wrapper around a managed component written in C++\CLI. I have the following function in managed code: array<Byte>^ Class::Function(); I want to expose this function from a native C++ class with the following signature: shared_array<unsigned char> Class::Function(); I've gotten as far as calling the managed function from native code, but I'm not sure how to safely copy the managed array into an unmanaged one. gcroot<cli::array<System::Byte>^> managedArray = _managedObject->Function(); There are two usual approaches: Perform the marshaling with native code, which requires

How do I use ExpectedException in C++/CLI NUnit tests?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 12:38:00
How do you do the equivalent of: [Test, ExpectedException( typeof(ArgumentOutOfRangeException) )] void Test_Something_That_Throws_Exception() { throw gcnew ArgumentOutOfRangeException("Some more detail"); } ...in C++ (the example there is C#)? As far as I can see, there's no typeof() function for the C++ implementation of NUnit. To avoid anyone else hunting around for ages trying to find it, here's the solution: [Test, ExpectedException( ArgumentOutOfRangeException::typeid )] void Test_Something_That_Throws_Exception() { throw gcnew ArgumentOutOfRangeException("Some more detail"); } Simply use

Getting sensible info from CLR-to-SEH exceptions in a mixed mode C++ project

吃可爱长大的小学妹 提交于 2019-12-05 12:29:22
Mixed mode C++ project. Native code is calling managed code. Managed code might throw an exception. I can catch said exception in native mode using a vectored exception handler; I can see its PEXCEPTION_POINTERS . The telling code 0xE0434F4D, meaning it's a CLR exception, is there. Question: is there any way to get any sensible information (exception class, message, stack trace etc.) from the attendant data? There's one parameter in the ExceptionInformation , and it looks like a pointer to something... No, that's too late. All you got is the exception code. You might get something in