none
Using IntPtr as float[]

    Question

  • Hi all,

    Code Snippet

    char ch = 'a';

    float[] widthOfChar = new float[1];

    IntPtr widthPtr = new IntPtr();

    if (GetCharWidthFloat(graphics.GetHdc(), ch, ch, out widthPtr) == 1)

    {

        if (widthPtr != IntPtr.Zero)

            Marshal.Copy(widthPtr, widthOfChar, 0, 1);


        advWidths.Add(widthOfChar[0]);

    }


    [DllImport("gdi32.dll", CharSet=CharSet.Auto)]

    static private extern int GetCharWidthFloat(IntPtr hcd, uint first, uint last, out IntPtr widthsBuffer);


    At Marshal.Copy it throws exception:
    Exception System.AccessViolationException was thrown in debuggee:
    Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

    Copy()
    ...

    What I'm doing wrong?
    Thanks
    Tuesday, April 03, 2007 12:09 PM

Answers

  • Code Snippet

    IntPtr hDC = graphics.GetHdc();           

    char ch = 'a'inner[i];

    float[] widthOfChar = new float[1];

    IntPtr widthPtr = Marshal.AllocHGlobal(sizeof(float));

    if (GetCharWidthFloat(hDC, ch, ch, widthPtr) == 1)

    {

        if (widthPtr != IntPtr.Zero)

            Marshal.Copy(widthPtr, widthOfChar, 0, 1);


        advWidths.Add(widthOfChar[0]);

    }

    else

        throw new Win32Exception(Marshal.GetLastWin32Error());


    Marshal.FreeHGlobal(widthPtr);

    graphics.ReleaseHdc(hDC);


    Code Snippet

    [DllImport("gdi32.dll", CharSet=CharSet.Auto, SetLastError=true)]

    static private extern int GetCharWidthFloat(IntPtr hDC, uint first, uint last, IntPtr widthsBuffer);


    This code finally works Smile
    Tuesday, April 03, 2007 2:10 PM