Ejemplo n.º 1
0
 public virtual extern void DetectOutboundCodePage([In] SMlCpf 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);
Ejemplo n.º 2
0
        public static Encoding[] DetectOutgoingEncodings(string input, int[] preferedEncodings, bool preserveOrder)
        {
            if (input == null)
            {
                throw new ArgumentNullException("input");
            }

            if (input.Length == 0)
            {
                return new Encoding[] { Encoding.ASCII }
            }
            ;

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

            IMultiLanguage3 multilang3 = new FMultiLanguageClass();

            if (multilang3 == null)
            {
                throw new System.Runtime.InteropServices.COMException("Failed to get IMultilang3");
            }
            try {
                int[]  resultCodePages   = new int[preferedEncodings.Length];
                uint   detectedCodepages = (uint)resultCodePages.Length;
                ushort specialChar       = (ushort)'?';

                IntPtr pPrefEncs     = Marshal.AllocCoTaskMem(sizeof(uint) * preferedEncodings.Length);
                IntPtr pDetectedEncs = preferedEncodings == null ? IntPtr.Zero : Marshal.AllocCoTaskMem(sizeof(uint) * resultCodePages.Length);

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

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

                    SMlCpf options = SMlCpf.MLDETECTF_VALID_NLS | SMlCpf.MLDETECTF_PREFERRED_ONLY;
                    if (preserveOrder)
                    {
                        options |= SMlCpf.MLDETECTF_PRESERVE_ORDER;
                    }

                    if (preferedEncodings != null)
                    {
                        options |= SMlCpf.MLDETECTF_PREFERRED_ONLY;
                    }

                    multilang3.DetectOutboundCodePage(options,
                                                      input, (uint)input.Length,
                                                      pPrefEncs, (uint)(preferedEncodings == null ? 0 : preferedEncodings.Length),
                                                      pDetectedEncs, ref detectedCodepages,
                                                      ref specialChar);

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

                        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);
            }
            return(result.ToArray());
        }