background preloader

P/Invoke

Facebook Twitter

Understanding SynchronizationContext (Part I) SynchronizationContext - MSDN Lets You Down I don't know why, but there is really not much information about this new class within the .NET Framework.

Understanding SynchronizationContext (Part I)

The MSDN documentation contains very little information on how to use SynchronizationContext. Initially, I must say that I had a hard time understanding the reason for this new class and how to use it. After reading a lot on the subject, I finally understood the purpose of this class and how it should be used. I decided to write this article to help other developers understand how to use this class, and what it can and cannot do for you. Using SynchronizationContext to Marshal Code from One Thread to Another Let's get some technical points out of the way so we can show how to use this class. Who puts the SynchronizationContext into the UI thread? Because my code uses SynchronizationContext.Current, let me explain what this static property gives us. OK, let's look at the code that places a SynchronizationContext within our UI thread: ...

How to Implement Pointer To Pointer in C# Without Using Unsafe Code. 1.

How to Implement Pointer To Pointer in C# Without Using Unsafe Code.

Introduction. 1.1 In C#, pointer management has traditionally been done using the IntPtr type. 1.2 There is a large number of Marshal class methods that work with IntPtrs and they are very useful indeed. 1.3 However, IntPtr dereferencing (i.e. the referencing of data pointed to by a pointer which is itself pointed to by another pointer) is something for which a direct solution is not readily available. 1.4 But after doing a little bit of research and experimentation, I found that this can be achieved by the judicious use of GCHandle and Marshal class methods. IntPtr.ToPointer Method (System) Converts the value of this instance to a pointer to an unspecified type.

IntPtr.ToPointer Method (System)

[CLSCompliantAttribute(false)] public void* ToPointer() Return Value The following example copies a string in reverse, by using byte pointers to read and write characters between the source and destination strings. Source and destination pointers are returned by using the ToPointer method. The reverse string copy is an unsafe operation and, in C#, it is performed inside an unsafe code region. Advanced P/Invoke on the Microsoft .NET Compact Framework. Jon Box, Dan Fox Quilogy Contributions by: Jonathan Wells Microsoft Corporation March 2003 Applies to: Microsoft® .NET Compact Framework 1.0 Microsoft® Visual Studio® .NET 2003 Summary: Explore advanced interoperability on the .NET Compact Framework. (23 printed pages) Contents Introduction Marshaling Complex Types Marshaling Strings in Structures Marshaling Fixed-Length Strings In Structures Summary Introduction In our previous paper, "An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework," we discussed how the Platform Invoke service of both the Microsoft .NET Compact Framework and the Microsoft .NET Framework allow managed code to invoke functions residing in unmanaged DLLs, allowing both custom and operating system (Windows CE) APIs to be accessible to applications written for either framework.

Advanced P/Invoke on the Microsoft .NET Compact Framework

Marshaling Complex Types As an example, consider the user notification API available on Windows CE. Marshaling Strings in Structures Using a Thunking Layer. Marshaling data in the Compact Framework. Introduction In many situations, when we create applications for different embedded systems or mobile platforms, we can't develop all parts of the product using managed code only.

Marshaling data in the Compact Framework

For example, we need several modules written in native language which perform some low level operations, or we already have these libraries written in C++. So we have to use more than one programming language in our product and also use data marshaling in it. In this articlen we will review some aspects of using data types and ways of using them during marshalling data to and from unmanaged code. Making your interop calls more efficient Marshaling is the act of taking data from one environment to another. You should remember that such Platform Invoke calls are slower than direct native calls and than regular managed calls. Blittable types It is recommended to use simple data types (int, byte, boolean, characters, and strings). Method inlining Sequential layout One calling convention Data alignment. Marshaling Structures in the .NET Compact Framework.

[This documentation is for preview only, and is subject to change in later releases.

Marshaling Structures in the .NET Compact Framework

Blank topics are included as placeholders.] When marshaling a string to a wchar_t*, you can specify either of the following attributes, which will marshal as a pointer to a Unicode string: [MarshalAs(UnmanagedType.LPWStr)] -or- Creating a Microsoft .NET Compact Framework-based Process Manager Application. Alex Yakhnin IntelliProg, Inc.

Creating a Microsoft .NET Compact Framework-based Process Manager Application

March 2003. An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework. Jon Box, Dan Fox Quilogy Contributions by: Jonathan Wells Microsoft Corporation Jim Wilson JW Hedgehog, Inc.

An Introduction to P/Invoke and Marshaling on the Microsoft .NET Compact Framework

March 2003.