Ejemplo n.º 1
0
        internal static string GetString<T>(T handle, GetStringFunc<T> func, bool nullable = false)
        {
            IntPtr val = IntPtr.Zero;
            try
            {
                func(handle, out val).ThrowIfError("Failed to get value");

                if (val == IntPtr.Zero)
                {
                    return nullable ? null : string.Empty;
                }

                return Marshal.PtrToStringAnsi(val);
            }
            finally
            {
                Interop.Libc.Free(val);
            }
        }
Ejemplo n.º 2
0
        private static void GetStringArrayFromNative(out string[] stringArray, GetStringFunc callback)
        {
            int arraySize = 0;

            arraySize = callback(null, maxLen, arraySize);
            if (arraySize > 0)
            {
                string[] devices = new string[arraySize];
                for (int i = 0; i < arraySize; i++)
                {
                    devices[i] = new StringBuilder().Append('c', maxLen).ToString();
                }
                callback(devices, maxLen, arraySize);
                stringArray = new string[arraySize];
                for (int i = 0; i < arraySize; i++)
                {
                    stringArray[i] = devices[i].ToString();
                }
            }
            else
            {
                stringArray = new string[0];
            }
        }