Passing a vector/array from unmanaged C++ to C#

As long as the managed code does not resize the vector, you can access the buffer and pass it as a pointer with vector.data() (for C++0x) or &vector[0]. This results in a zero-copy system. Example C++ API: #define EXPORT extern “C” __declspec(dllexport) typedef intptr_t ItemListHandle; EXPORT bool GenerateItems(ItemListHandle* hItems, double** itemsFound, int* itemCount) { auto … Read more

Better way to cast object to int

You have several options: (int) — Cast operator. Works if the object already is an integer at some level in the inheritance hierarchy or if there is an implicit conversion defined. int.Parse()/int.TryParse() — For converting from a string of unknown format. int.ParseExact()/int.TryParseExact() — For converting from a string in a specific format Convert.ToInt32() — For … Read more

How to list available video modes using C#?

If you mean video modes are available resolutions, try to invoke EnumDisplaySettingsEx details can be found here: http://msdn.microsoft.com/en-us/library/dd162612(VS.85).aspx small program that lists available resolutions: using System; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; namespace ListResolutions { class Program { [DllImport(“user32.dll”)] public static extern bool EnumDisplaySettings( string deviceName, int modeNum, ref DEVMODE devMode); const int ENUM_CURRENT_SETTINGS = … Read more

Which blocking operations cause an STA thread to pump COM messages?

BlockingCollection will indeed pump while blocking. I’ve learnt that while answering the following question, which has some interesting details about STA pumping: StaTaskScheduler and STA thread message pumping However, it will pump a very limited undisclosed set of COM-specific messages, same as the other APIs you listed. It won’t pump general purpose Win32 messages (a … Read more

Restore a minimized window of another application

Working code using FindWindow method: [DllImport(“user32.dll”)] public static extern IntPtr FindWindow(string className, string windowTitle); [DllImport(“user32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool ShowWindow(IntPtr hWnd, ShowWindowEnum flags); [DllImport(“user32.dll”)] private static extern int SetForegroundWindow(IntPtr hwnd); [DllImport(“user32.dll”)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool GetWindowPlacement(IntPtr hWnd, ref Windowplacement lpwndpl); private enum ShowWindowEnum { Hide = 0, ShowNormal = 1, ShowMinimized = 2, … Read more

c++/cli pass (managed) delegate to unmanaged code

Yes, you want Marshal::GetFunctionPointerForDelegate(). Your code snippet is missing the managed function you’d want to call, I just made one up. You will also have to declare the managed delegate type and create an instance of it before you can get a function pointer. This worked well: #include “stdafx.h” using namespace System; using namespace System::Runtime::InteropServices; … Read more