public AsciiCompiledTextSearchNative(
            NativeMethods.SearchAlgorithmKind kind,
            string pattern,
            NativeMethods.SearchOptions searchOptions)
        {
            _patternHandle = new SafeHGlobalHandle(Marshal.StringToHGlobalAnsi(pattern));
            var patternLength = pattern.Length;

            _handle           = CreateSearchHandle(kind, _patternHandle, patternLength, searchOptions);
            _searchBufferSize = NativeMethods.AsciiSearchAlgorithm_GetSearchBufferSize(_handle);
        }
        private static unsafe SafeSearchHandle CreateSearchHandle(
            NativeMethods.SearchAlgorithmKind kind,
            SafeHGlobalHandle patternHandle,
            int patternLength,
            NativeMethods.SearchOptions searchOptions)
        {
            NativeMethods.SearchCreateResult createResult;
            var result = NativeMethods.AsciiSearchAlgorithm_Create(
                kind,
                patternHandle.Pointer,
                patternLength,
                searchOptions,
                out createResult);

            if (createResult.HResult < 0)
            {
                // The error is recoverable, since we are dealing with an invalid pattern
                // or something along the lines.
                var message = Marshal.PtrToStringAnsi(new IntPtr(createResult.ErrorMessage));
                throw new RecoverableErrorException(message);
            }

            return(result);
        }
 public AsciiStringSearchNative(NativeMethods.SearchAlgorithmKind kind, string pattern, NativeMethods.SearchOptions searchOptions)
 {
     _patternHandle = new SafeHGlobalHandle(Marshal.StringToHGlobalAnsi(pattern));
     _handle        = NativeMethods.AsciiSearchAlgorithm_Create(kind, _patternHandle.Pointer, pattern.Length, searchOptions);
     _patternLength = pattern.Length;
 }