Beispiel #1
0
 public virtual extern void DetectOutboundCodePage([In] MLCPF dwFlags,
                                                   [In][MarshalAs(UnmanagedType.LPWStr)] string lpWideCharStr,
                                                   [In] uint cchWideChar,
                                                   [In] IntPtr puiPreferredCodePages,
                                                   [In] uint nPreferredCodePages,
                                                   [In] IntPtr puiDetectedCodePages,
                                                   [In][Out] ref uint pnDetectedCodePages,
                                                   [In] ref ushort lpSpecialChar);
 public virtual extern HRESULT DetectOutboundCodePageInIStream(
     [In] MLCPF dwFlags,
     [In, MarshalAs(UnmanagedType.Interface)] IStream pStrIn,
     [In] IntPtr puiPreferredCodePages,
     [In] uint nPreferredCodePages,
     [In] IntPtr puiDetectedCodePages,
     [In, Out] ref uint pnDetectedCodePages,
     [In] ref ushort lpSpecialChar
     );
        public static Encoding[] DetectOutgoingEncodings(string input, int[] preferredEncodings, bool preserveOrder)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
                return new Encoding[] { Encoding.ASCII };

            List<Encoding> result = new List<Encoding>();

            // get the IMultiLanguage3 interface
            IMultiLanguage3 multilang3 = new CMultiLanguageClass();
            if (multilang3 == null)
                throw new COMException("Failed to get IMultilang3");
            try
            {
                int[] resultCodePages = new int[preferredEncodings.Length];
                uint detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar = (ushort)'?';

                // get unmanaged arrays
                IntPtr pPrefEncs = Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length);
                IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    Marshal.Copy(preferredEncodings, 0, pPrefEncs, preferredEncodings.Length);

                    Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

                    MLCPF options = MLCPF.MLDETECTF_VALID_NLS | MLCPF.MLDETECTF_PREFERRED_ONLY;
                    if (preserveOrder)
                        options |= MLCPF.MLDETECTF_PRESERVE_ORDER;

                    options |= MLCPF.MLDETECTF_PREFERRED_ONLY;

                    // finally... call to DetectOutboundCodePage
                    multilang3.DetectOutboundCodePage(options,
                        input, (uint)input.Length,
                        pPrefEncs, (uint)preferredEncodings.Length,
                        pDetectedEncs, ref detectedCodepages,
                        ref specialChar);

                    // get result
                    if (detectedCodepages > 0)
                    {
                        int[] theResult = new int[detectedCodepages];
                        Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);

                        // get the encodings for the codepages
                        for (int i = 0; i < detectedCodepages; i++)
                            result.Add(Encoding.GetEncoding(theResult[i]));
                    }
                }
                finally
                {
                    if (pPrefEncs != IntPtr.Zero)
                        Marshal.FreeCoTaskMem(pPrefEncs);
                    Marshal.FreeCoTaskMem(pDetectedEncs);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            // nothing found
            return result.ToArray();
        }
        private static Encoding DetectOutgoingEncoding(string input, int[] preferredEncodings, bool preserveOrder)
        {
            if (input == null)
                throw new ArgumentNullException("input");

            // empty strings can always be encoded as ASCII
            if (input.Length == 0)
                return Encoding.ASCII;

            Encoding result = Encoding.ASCII;

            // get the IMultiLanguage3 interface
            IMultiLanguage3 multilang3 = new CMultiLanguageClass();
            if (multilang3 == null)
                throw new COMException("Failed to get IMultilang3");
            try
            {
                int[] resultCodePages = new int[preferredEncodings != null ? preferredEncodings.Length : Encoding.GetEncodings().Length];
                uint detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar = (ushort)'?';

                // get unmanaged arrays
                IntPtr pPrefEncs = preferredEncodings == null ? IntPtr.Zero : Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length);
                IntPtr pDetectedEncs = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    if (preferredEncodings != null)
                        Marshal.Copy(preferredEncodings, 0, pPrefEncs, preferredEncodings.Length);

                    Marshal.Copy(resultCodePages, 0, pDetectedEncs, resultCodePages.Length);

                    MLCPF options = MLCPF.MLDETECTF_VALID_NLS;
                    if (preserveOrder)
                        options |= MLCPF.MLDETECTF_PRESERVE_ORDER;

                    if (preferredEncodings != null)
                        options |= MLCPF.MLDETECTF_PREFERRED_ONLY;

                    multilang3.DetectOutboundCodePage(options,
                        input, (uint)input.Length,
                        pPrefEncs, (uint)(preferredEncodings == null ? 0 : preferredEncodings.Length),

                        pDetectedEncs, ref detectedCodepages,
                        ref specialChar);

                    // get result
                    if (detectedCodepages > 0)
                    {
                        int[] theResult = new int[detectedCodepages];
                        Marshal.Copy(pDetectedEncs, theResult, 0, theResult.Length);
                        result = Encoding.GetEncoding(theResult[0]);
                    }
                }
                finally
                {
                    if (pPrefEncs != IntPtr.Zero)
                        Marshal.FreeCoTaskMem(pPrefEncs);
                    Marshal.FreeCoTaskMem(pDetectedEncs);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multilang3);
            }
            return result;
        }
Beispiel #5
0
        private static Encoding[] FindEncodings(string input, int[] preferredEncodings, bool preserveOrder)
        {
            // empty strings can always be encoded as ASCII
            if (string.IsNullOrEmpty(input))
            {
                return(new[]
                {
                    Default
                });
            }

            bool            bPrefEnc = !preferredEncodings.IsNullOrEmpty();
            List <Encoding> result   = new List <Encoding>();

            // get the IMultiLanguage3 interface
            IMultiLanguage3 multiLang3 = new CMultiLanguageClass();

            if (multiLang3 == null)
            {
                throw new COMException("Failed to get " + nameof(IMultiLanguage3));
            }

            try
            {
                int    count             = bPrefEnc ? preferredEncodings.Length : SystemEncodingCount;
                int[]  resultCodePages   = new int[count];
                uint   detectedCodePages = (uint)resultCodePages.Length;
                ushort specialChar       = '?';

                // get unmanaged arrays
                IntPtr preferred = bPrefEnc ? Marshal.AllocCoTaskMem(sizeof(uint) * preferredEncodings.Length) : IntPtr.Zero;
                IntPtr detected  = Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

                try
                {
                    if (bPrefEnc)
                    {
                        Marshal.Copy(preferredEncodings, 0, preferred, preferredEncodings.Length);
                    }

                    Marshal.Copy(resultCodePages, 0, detected, resultCodePages.Length);
                    MLCPF options = MLCPF.MLDETECTF_VALID_NLS;
                    if (preserveOrder)
                    {
                        options |= MLCPF.MLDETECTF_PRESERVE_ORDER;
                    }
                    if (bPrefEnc)
                    {
                        options |= MLCPF.MLDETECTF_PREFERRED_ONLY;
                    }

                    // finally... call to DetectOutboundCodePage
                    multiLang3.DetectOutboundCodePage(options,
                                                      input,
                                                      (uint)input.Length,
                                                      preferred,
                                                      (uint)(bPrefEnc ? preferredEncodings.Length : 0),
                                                      detected,
                                                      ref detectedCodePages,
                                                      ref specialChar);

                    // get result
                    if (detectedCodePages > 0)
                    {
                        int[] theResult = new int[detectedCodePages];
                        Marshal.Copy(detected, theResult, 0, theResult.Length);

                        // get the encodings for the code pages
                        for (int i = 0; i < detectedCodePages; i++)
                        {
                            result.Add(Encoding.GetEncoding(theResult[i]));
                        }
                    }
                }
                finally
                {
                    if (!preferred.IsZero())
                    {
                        Marshal.FreeCoTaskMem(preferred);
                    }
                    Marshal.FreeCoTaskMem(detected);
                }
            }
            finally
            {
                Marshal.FinalReleaseComObject(multiLang3);
            }

            return(result.ToArray());
        }