CodePrairie .NET

South Dakota .NET User Group

Marshaling, P/Invoke and WIN32API in .NET

Last post 12-15-2005 7:11 AM by chrisortman. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 12-15-2005 6:25 AM

    • Kwen
    • Top 10 Contributor
    • Joined on 04-14-2005
    • Parker, SD
    • Posts 8

    Marshaling, P/Invoke and WIN32API in .NET

    The full list of the WIN32API functions I need to reference in .NET is this: CreateSolidBrush, ScrollDC, FillRect, DrawText, InvalidateRect, DrawEdge, GetSysColorBrush, InvertRect, GetSysColor, FrameRect, SelectObject, and DeleteObject.
    I found some MSDN articles that explained how to get RichTextBoxes to print their contents in .NET, and tried to use those as a template for my changes.  I am currently attempting to just get 2 from the above list working, DrawText and FillRect (and hopefully once I get those two working I'll understand it well enough to do the others.)
    Here are the VB6 declarations of the two:
    Public Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hbrush As Long) As Long
    Public Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long

    Here is what I am trying to use in .NET:

    <DllImport("User32.dll", EntryPoint:="FillRect", SetLastError:=True, CharSet:=CharSet.Unicode, _
    , CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function FillRect(ByVal hDC As IntPtr, ByRef lpRect As STRUCT_RECT, ByVal hbrush As Int32) As
    End Function

    <DllImport("user32.dll", entrypoint:="DrawTextA", SetLastError:=True, CharSet:=CharSet.Unicode, _
    , CallingConvention:=CallingConvention.StdCall)> _
    Public Shared Function DrawText(ByVal hDC As IntPtr, ByVal lpStr As String, ByVal nCount As Int32, ByRef lpRect As STRUCT_RECT, _
    ByVal wFormat As Int32) As Int32
    End Function

    Here is STRUCT_RECT:

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure STRUCT_RECT
    Public left As
    Public top As
    Public right As
    Public bottom As
    End Structure

    Currently neither one of them is working, and they both return zero.  Anything that jumps out at you that I am going wrong?

  • 12-15-2005 7:11 AM In reply to

    Re: Marshaling, P/Invoke and WIN32API in .NET

       Hmmm, sucks when there's no let's just start with FillRect....

    The signature looks like this:
    static extern int FillRect(IntPtr hDC, [In] ref RECT lprc, IntPtr hbr);

    Unfortunately, no example was given, but let's change your current declaration to match the above and take off all the extra attributes. I don't think VB supports [In] so probably have to just use ByRef there.

    I can't see the name of the structure making a difference, but maybe renaming it to just Rect which is the way this example is

    I have found that with Interop type stuff some things only work well from VB and others from C# don't ask me why, I could never figure it out...but an alternative might be to write a C# library that wraps these functions for you.

Page 1 of 1 (2 items)
Powered by Community Server (Commercial Edition), by Telligent Systems