Call C++ Functions in a DLL using C#

P/Invoke is an efficient method for calling native code functions in an unmanaged DLL. The below code samples show the source code a native code library which defines a very simple C++ function which accepts a char* argument. MyLib.h __declspec(dllexport) int Hello(char* pszBuffer, int nLengthObj); MyLib.cpp #include “stdafx.h” #include “MyLib.h” #include __declspec(dllexport) int SayHello(char* pszBuffer, […]

Read more

Measure Memory Usage of .NET Applications

There are two main methods for measuring the memory usage of a .NET application, using the GC class or using System.Diagnostics Retrieve Your App’s Memory Utilization using the GC class The .NET Framework’s GC class contains many useful memory-related methods, including GetTotalMemory(), which returns the amount of memory the garbage collector believes is allocated to […]

Read more

C# Dynamic Binding – Language Binding

Language binding is a form of dynamic binding which occurs when a dynamic object does not implement IDynamicMetaObjectProvider. Language binding comes in handy when working around imperfectly designed types or the inherent limitations in the .NET type system. A common problem is that when using numeric types is they have no common interface. Using dynamic […]

Read more

C# Dynamic Binding – Custom Binding

Custom binding is a form of dynamic binding which occurs when a dynamic object implements IDynamicMetaObjectProvider (IDMOP). Whilst you can implement IDynamicMetaObjectProvider on types that you which are written in C#, the more common use-case is that you have been given an IDMOP object from a dynamic language (such as IronPython or IronRuby) that is […]

Read more

C# Dynamic Binding

Dynamic binding in C# defers binding (ie the process of resolving types, operators and members) from compile time to runtime. Dynamic binding is used when you know w that a certain function, member, or operation exists at compile time but the compiler does not. This normally occurs when interoperating with dynamic languages (like IronPython) or […]

Read more

Interoperating C# with Dynamic Languages

Despite the fact that C# 4.0 supports dynamic binding using the dynamic keyword, it doesn’t go as far as to allow executing an expression described in a string at runtime such as: string expr = “2 * 4″; // We can’t “execute” expr This is due to the fact that the code to translate a string to […]

Read more

C# Await

Await is a new keyword introduced with C# 5.0 to facilitate its new asynchronous programming model. In C3 5.0 asynchronous functions are declared with the new async keyword, and must contain one or more await expressions. In C# 5.0, await is used in conjunction with Tasks (parallel programming), in very general terms it allows you […]

Read more

C# Async

Async is a new keyword added in C# 5.0 which is a key part of the asynchronous programming model. In C#, Async is modifier which is added to a method which denotes that the method contains control flow which involves waiting for asynchronous operations and will therefore be rewritten by the compiler to ensure that […]

Read more

SQL Azure Query Optimization

This article demonstrates how to optimize the performance of SQL Azure databases.

Read more

Compressing in Memory using MemoryStream

Sometimes you need to compress entirely in memory. Here’s how to use a MemoryStream for this purpose: byte[] data = new byte[1000]; // We would expect a good compression ratio from an empty array! var msObj = new MemoryStream(); using (Stream ds = new DeflateStream (msObj, CompressionMode.Compress)) ds.Write (data, 0, data.Length); byte[] compressed = msObj.ToArray(); Console.WriteLine (compressed.Length); […]

Read more