Click here to Skip to main content
15,890,336 members
Articles / Programming Languages / C#

Modifying an array of numeric types (default marshaling)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
14 Aug 2013CPOL 4K   2  
Modifying an array of numeric types.

Define some functions in the native DLL (let's call it “Win32Native.dll”) as shown below. These two functions receives integer buffer, and double buffer, respectively.

C#
extern "C" __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray)
extern "C" __declspec(dllexport) double ModifyDoubleArrayValues ( int nSize, double* pArray)

Implementing Functions

C++
extern "C" __declspec(dllexport) int ModifyIntegerArrayValues ( int nSize, int* pArray) 
{ 
    int nResult = 0 ; 
    for ( int i = 0; i < nSize; i++ ) 
    { 
        nResult += pArray[ i ]; 
        pArray[i] += 100; 
    } 
    return nResult; 
}
extern "C" __declspec(dllexport) double ModifyDoubleArrayValues ( int nSize, double* pArray) 
{ 
    double dblResult = 0 ; 
    for ( int i = 0; i < nSize; i++ ) 
    { 
        dblResult += pArray[ i ]; 
        pArray[i] += 200 ; 
    } 
    return dblResult ; 
}

Points of Interest

  • CoTaskMemAlloc is used to allocated the memory required.
  • CoTaskMemFree is used to free any previously allocated buffer; if null is passed then, CoTaskMemFree is not called.

If you want to use a heap that is shared between native and managed, it is more common to use the COM heap.

Writing the client code (the managed part)

We can simply create a console base application which can use this DLL. Let’s name it MarshallingTest.

See the code snippet below.

C#
using System; 
using System.Runtime.InteropServices; 
using System.Text;
namespace MarshallingTest 
{ 
    class Program 
    { 
        [DllImport("Win32Native.dll")] 
        public static extern void ModifyIntegerArrayValues(int nCount, [In, Out] int[] arrStr);
        [DllImport("Win32Native.dll")] 
        public static extern int ModifyDoubleArrayValues(int nSize, [In, Out] double[] nArr); 
     
        static void Main(string[] args) 
        { 
            int nSize = 5 ; 
            int[] arrInt = new int[nSize]; 
            for (int nI = 0; nI < nSize; nI++) arrInt[nI] = (nI + 1) * 10;
            Console.WriteLine("\nValues of Integer array, before calling function"); 
            for (int i = 0; i < nSize; i++) 
            { 
                Console.WriteLine(string.Format("Array[{0:D2}] : {1:D3}", i, arrInt[i])); 
            }
            ModifyIntegerArrayValues(nSize, arrInt);
            Console.WriteLine("\nValues of Integer array, after calling function"); 
            for (int i = 0; i < nSize; i++) 
            { 
                Console.WriteLine(string.Format("Array[{0:D2}] : {1:D3}", i, arrInt[i])); 
            }
            double [] arrDbl= new double [nSize]; 
            for (int nI = 0; nI < nSize; nI++) arrDbl[nI] = (nI + 1) * 5.0 ;
            Console.WriteLine("\nValues of Double array, before calling function"); 
            for (int i = 0; i < nSize; i++) 
            { 
                Console.WriteLine(string.Format("Array[{0:D2}] : {1:F2}", i, arrDbl[i])); 
            }
            ModifyDoubleArrayValues(nSize, arrDbl);
            Console.WriteLine("\nValues of double array, after calling function"); 
            for (int i = 0; i < nSize; i++) 
            { 
                Console.WriteLine(string.Format("Array[{0:D2}] : {1:F2}", i, arrDbl[i])); 
            } 
        } 
    } 
}

Point of Interest

  • namespace System.Runtime.InteropServices; defines the declarations necessary for Interop operations, like DllImport.
  • DllImport defines the DLL entry point.
  • Marshal.Copy function is used to copy buffer from the managed buffer to the unmanaged buffer and vice versa.
  • Marshal.FreeCoTaskMem frees the memory allocated by the native DLL.

Compile and execute, and you will get the following output.

image

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
India India
More than 10 years of experience in designing and development of GUIs and Middleware for industrial control systems.

Comments and Discussions

 
-- There are no messages in this forum --