We are a software development company with more than a decade of experience. We believe that the solution to outsourcing is to establish proper communication, thus ensuring quality and on-time delivery of customer expectations.

Menu

Accessing unmanaged code in separate context in WPF

In order for Windows Forms controls to work correctly in .NET Framework it is necessary to access their methods and properties from the same context that they were created in, otherwise they may display unpredictable behaviour. Usually this is a concern when using multiple threads, some of which need to modify a control’s state. All of this is also relevant for Windows Presentation Foundation (WPF) technology. However, in WPF such an issue may also arise when using a DLL with unmanaged code — this does not occur in WinForms. We’ll show an example how one can solve this issue by calling methods from the DLL in a separate context.

Let’s say we have a DLL written in C, called “native.dll”, and we need to call method getSomeList() of the DLL, which returns a list of items to populate a drop-down control in WPF. In order to implement this, we need to take the following key steps:

Create new context for invoking the DLL method.

Invoke the DLL method in the newly created context.

Access the control’s properties and methods in the context that the control was created in.