FontSub.dll : CreateFontPackage method run/compile time errors. RRS feed

  • Question

  • Please refer to this thread for background info...  I am uncertain which forum is best for this issue.  The problem is two-fold.  Each problem is probably best resolved in a different forum.  I have been trying to resolve this issue for a forum participant without success.

    1.  Calling the method in .NET 4 using either VB/C# will cause a Stack Imbalance compiler error .  Okay, that means check the passed parameter types, and match them up.  Something else is up.  I suspect an attribute to override cdecl.  I don't think this method cleans up the stack properly when passed incorrect parameters.  Compile against "Any CPU", and it clears on Windows 7, but not XP, for me at least.

    2.  No returned results in the array.  An initialized array passed to the method is always returned corrupted, set to Nothing/null.  The length variable is always returned as zero.  The array is corrupted no matter if the method returns with or without an error code.  This also makes the Stack Imbalance a good indicating that something is occurring within the method that shouldn't.  I have suspected a type is used as if it were another type.  That and the flags being passed are not correct.

    Please let me know if you wish to me to post code.  There are samples at the link, and additional samples at links within that thread.

    class Program
            [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
            internal delegate IntPtr ALLOCPROC(Int32 Size);

            [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
            internal delegate IntPtr REALLOCPROC(IntPtr MemBlock, IntPtr Size);

            [UnmanagedFunctionPointerAttribute(CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
            internal delegate void FREEPROC(IntPtr MemBlock);

            [DllImport("FontSub.dll", CharSet = CharSet.Auto, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
            internal static extern uint CreateFontPackage(
                [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 1)]
                Byte[] puchSrcBuffer,
                UInt32 ulSrcBufferSize,
                [Out, MarshalAs(UnmanagedType.LPArray)]
                out Byte[] puchFontPackageBuffer,
                out UInt32 pulFontPackageBufferSize,
                out UInt32 pulBytesWritten,
                UInt16 usFlags,
                UInt16 usTTCIndex,
                UInt16 usSubsetFormat,
                UInt16 usSubsetLanguage,
                UInt16 usSubsetPlatform,
                UInt16 usSubsetEncoding,
                [In, MarshalAs(UnmanagedType.LPArray, SizeParamIndex = 11)]
                UInt16[] pusSubsetKeepList,
                UInt16 usSubsetKeepListCount,
                ALLOCPROC lpfnAllocate,
                REALLOCPROC lpfnReAllocate,
                FREEPROC lpfnFree,
                IntPtr lpvReserved

            static void Main(string[] args)

            private static void Test_CreateFontSub()
                byte[] result = File.ReadAllBytes(@"C:\Windows\Fonts\Arial.ttf");

                ALLOCPROC allocproc = new ALLOCPROC(Marshal.AllocHGlobal);
                REALLOCPROC reallocproc = new REALLOCPROC(Marshal.ReAllocHGlobal);
                FREEPROC freeproc = new FREEPROC(Marshal.FreeHGlobal);
                ushort[] subset = { };
                //byte[] buffer;
                byte[] buffer = { };
                uint buffer_size, bytes_written;
                IntPtr lpvReserved = IntPtr.Zero;

                uint rc = CreateFontPackage(
                    result, (uint)result.Length,
                    out buffer, out buffer_size, out bytes_written,
                    /* flags */ 0,
                    0, /* TTC index is used only if  */
                    0, /*  TTFCFP_SUBSET */
                    0, /* Subset langugage */
                    0, /* Subset platform: TTFCFP_UNICODE_PLATFORMID */
                    1, /* Subset encoding: TTFCFP_UNICODE_CHAR_SET */
                    allocproc, reallocproc, freeproc, lpvReserved);
                if (rc != 0)
                    Console.WriteLine("Failed with code {0}", rc);

    Thanks, ahead of time.

    Rudy   =8^D

    Mark the best replies as answers. "Fooling computers since 1971."

    Tuesday, March 8, 2011 6:31 PM


All replies