Wombat.MamaMsg.addVectorString C# (CSharp) Method

addVectorString() public method

Add an array of strings (char*) to the message.
/// Throw if the string array is null. /// /// Throw if there are no items in the string array. ///
public addVectorString ( string name, ushort fid, string val ) : void
name string /// The field name, can be null or blank if the field Id is supplied. ///
fid ushort /// The field id, can be 0 if the name is specified and field names are turned on. ///
val string /// The array of strings to add. ///
return void
        public void addVectorString(
            string name,
            ushort fid,
            string[] val)
        {
            // Check that the arguments are valid
            if (val == null)
            {
                throw new ArgumentNullException("val");
            }

            // Check the array length
            int numberStrings = val.Length;
            if (numberStrings < 1)
            {
                throw new ArgumentOutOfRangeException("val");
            }

            // Verify that the native message has been created
            EnsurePeerCreated();

            // Allocate memory on the global native heap to pass the messages to the native layer
            IntPtr globalHeapMemory = Marshal.AllocHGlobal(numberStrings * IntPtr.Size);
            try
            {
                // Create a new array to hold all of the native handles
                IntPtr[] nativeHandleArray = new IntPtr[numberStrings];
                try
                {
                    // Copy the strings over with a conversion to ANSI
                    for (int nextItem = 0; nextItem < numberStrings; nextItem++)
                    {
                        nativeHandleArray[nextItem] = Marshal.StringToHGlobalAnsi(val[nextItem]);
                    }

                    // Copy this to the global heap
                    Marshal.Copy(nativeHandleArray, 0, globalHeapMemory, numberStrings);

                    // Call the native function
                    int code = NativeMethods.mamaMsg_addVectorString(nativeHandle, name, fid, globalHeapMemory, numberStrings);
                    CheckResultCode(code);
                }

                finally
                {
                    // Free the individual strings
                    foreach (IntPtr nativeHandle in nativeHandleArray)
                    {
                        if (nativeHandle != null)
                        {
                            Marshal.FreeHGlobal(nativeHandle);
                        }
                    }
                }
            }

            finally
            {
                // Free the memory on the global heap now that the function has completed
                Marshal.FreeHGlobal(globalHeapMemory);
            }
        }
MamaMsg