Results 1 to 3 of 3

Thread: Should I use PInvoke to call API calls in .net

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982

    Should I use PInvoke to call API calls in .net

    This code was given to me by DiGiTaIErRoR on this forum.

    I need to rewrite this in VB.NET code. Can anyone help.

    I has been suggested to me that I can use PInvoke to call these apis. I wouldn't have thought that was a good idea if I want my code to be fully .net framework compliant.

    What are your thoughts.

    [code]Private Sub something()
    Dim x As Long

    Pal.palNumEntries = 10
    Pal.palVersion = &H300 'defines our palette entries and type of palette

    'define the colors in the palette
    Pal.palPalEntry(1).peRed = 1
    Pal.palPalEntry(1).peGreen = 2
    Pal.palPalEntry(1).peBlue = 3

    Pal.palPalEntry(2).peRed = 255
    Pal.palPalEntry(2).peGreen = 255
    Pal.palPalEntry(2).peBlue = 255

    'When you've set the colors in the palette, you now do:

    hPal = CreatePalette(Pal)

    x = GetNearestPaletteIndex(hPal, vbRed)

    'now to retieve the palette's 'approximate' of somecolor
    Dim R As Long: R = Pal.palPalEntry(x).peRed
    Dim g As Long: g = Pal.palPalEntry(x).peGreen
    Dim b As Long: b = Pal.palPalEntry(x).peBlue

    MsgBox R & " " & g & " " & b

    x = GetNearestPaletteIndex(hPal, vbWhite)

    'now to retieve the palette's 'approximate' of somecolor
    R = Pal.palPalEntry(x).peRed
    g = Pal.palPalEntry(x).peGreen
    b = Pal.palPalEntry(x).peBlue

    MsgBox R & " " & g & " " & b
    end sub
    [/Highlight]

    from MSDN

    GetNearestPaletteIndex
    The GetNearestPaletteIndex function retrieves the index for the entry in the specified logical palette most closely matching a specified color value.

    UINT GetNearestPaletteIndex(
    HPALETTE hpal, // handle of logical color palette
    COLORREF crColor // color to be matched
    );


    Things I do when I am bored: DotNetable

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539
    you should only use PInvoke when you absolutely have to.
    if what ur tryign to do cannot be done in managed code then u have to resort to unmanaged, and thats ok

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 1999
    Location
    England
    Posts
    982
    It would be interesting to know what percentage of the time people use unmanaged code.


    Things I do when I am bored: DotNetable

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width