Browse by Tags
All Tags »
Interop (
RSS)
I encountered an interesting gotcha today, which I thought would be interesting to share with you. The symptoms were the following: I wrote a simple C++ function exported from a DLL, and tried to invoke it using a P/Invoke wrapper. The C++ function returns a bool , and was declared as follows (the code was simplified for expository purposes): extern "C" __declspec(dllexport) bool IsPrime(int n) { if (n <= 1) return false; if (n == 2) return true;...
There is so much material on the web (and even on this blog) about memory leak diagnostics in managed code, and a considerable number of tools that make diagnostics increasingly easier. Modern memory profilers can open application dumps, attach to live processes, display live memory graphs, compare snapshots, identify problematic retention patterns, and so much more. Unfortunately, these tools presently don’t work with Windows Store apps. Moreover, the UI model of Windows Store apps poses a significant...
What if P/Invoke is not enough? Hold on, why should it not be enough? What needs could we possibly have but calling global C-style functions exported from DLLs? Well, suppose you want to use a C++ class exported from a DLL. Or maybe the C++ class is not yet exported and you are looking for a way to make it available to managed code. The problem with a C++ class as opposed to a global function is that … it is not a global function. For example, a typical C++ class with a constructor and a couple of...
We’ve got P/Invoke doing some heavy lifting of managed-to-unmanaged signature translation . The opposite direction is surprisingly easy as well. Typically, you would encounter the need for unmanaged code to call managed code as part of a callback. (There is the scenario where unmanaged code is the interop initiator, which we will discuss in another post.) In the unmanaged world, callbacks are function pointers; in the managed world, callbacks are delegates. Recall that a delegate “knows” not only...
With Windows 8 around the corner, managed code slowly taking over legacy systems written in C++, games developed in a mixture of .NET and C++, and the rest of this technology soup – I thought it would be a good time to provide a quick refresher of the available interoperability mechanisms between managed and unmanaged code. Nothing here is very new, but I get so many questions about it that at the very least I would have something to refer people to. P/Invoke is best suited for managed code invoking...
More than a year after writing my first post touching on the subject of Managed Debugging Assistants (MDA) through the “Callback on Garbage Collected Delegate” case study , it’s time for a brief mention of another useful MDA – “P/Invoke Stack Imbalance” . This MDA fires whenever a P/Invoke call causes an imbalance on the stack. What does a stack imbalance mean? The top of the thread’s stack is pointed to by the ESP register (RSP on x64), and a stack imbalance occurs if the ESP value before making...
This is a bug Dima encountered several months ago, and I’ve been looking for an opportunity to document ever since. tl;dr – when unmanaged C++ code throws an exception which propagates through an interop boundary to managed code, C++ destructors are not called. To fix, don’t allow C++ exception propagation outside module boundaries, or compile with /EHa. It was recorded before on this StackOverflow thread , where the conclusion is that “ the CLR hooks into SEH handling to catch native exceptions...
Daniel Moth recently blogged about the sad fact that there are no managed APIs for working with more than 64 logical processors, even though as you probably know, Windows 7 (and of course Windows Server 2008 R2) features support for up to 256 logical processors. Frankly, these APIs aren’t designed to be very friendly, but sure enough they give you the ability to create threads (within a single process) that will run on different processor groups. As each processor group is limited to 64 processors...
Incredibly, I haven’t got a chance to blog about the Windows API Code Pack yet – even though it’s been out on MSDN Code Gallery for a couple of months already. It’s an open source .NET library which provides interop wrappers to Windows 7 (and Windows Vista) features. In fact, it would be unfair to say that these are wrappers – some of the features are organized and designed to make access from managed code significantly easier than from the native Win32/COM counterparts. By the time of this writing...
Joshua Goodman's session on CLR futures was packed - the crowd of developers wanted to learn what's been cooking under the hood for the past couple of years. Without a major CLR upgrade since 2005 (CLR 2.0), we all had our appetites whetted. So to begin with, Joshua announced that as of 4.0, multiple versions of the CLR can be hosted in the same process. For example, an application leveraging CLR 4.0 will run on top of CLR 4.0, but will be able to load and interact with a module that requires...
The first session I attended on the first day of PDC was Misha Shneerson's presentation on a feature affectionately called No-PIA, or formally enabled by type equivalence and type embedding. The primary motivation for this feature is to simplify COM interoperability scenarios and improve their performance, but it is useful for managed-to-managed applications as well, particularly those with extensibility requirements. What No-PIA means in brief is that instead of generating an interop assembly...
I recently delivered a half-day instructor-led training on .NET Interoperability , including P/Invoke, COM Interop and C++/CLI. I'm sharing the exercises and solutions with you in the hope you might find them useful. All the exercises are taken from (almost) real-life scenarios I had to implement at some point in the past. Among the exercises: Wrap System.IO.FileSystemWatcher for use from native C++ code Provide a managed class that acts as a byte array allocated from a Win32 low-fragmentation...
I've discussed dump analysis on several occasions, including detailed walkthroughs of native and managed issues - crashes, hangs, deadlocks and the rest of the bug zoo. ( Alon has just recently been to this bug zoo and promised me pictures.) Oftentimes it's useful to generate a dump programmatically. For example, if your application fails and you want to record the data and send it over the network or persist it to some durable storage, you can do so without relying on Windows Error...
Most people have encountered the need for interoperability between managed and unmanaged code. There are plenty of patterns and tutorials which explain every detail of writing managed code which can call into unmanaged code. The techniques we can use, from most common to least explored, are: Straightforward P/Invoke ( static extern , [DllImport] , sprinkle a [MarshalAs] or two and we're set - there are even tools to help); COM interop (import the type library and you're good to go); C++/CLI...