Results 1 to 6 of 6

Thread: Converting C# code to VB.Net - need help with integers!

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32

    Converting C# code to VB.Net - need help with integers!

    Hi,

    I have a piece of C# code I found on the web to get a color at the pixel under the mouse pointer on the screen. It uses GDI32.dll and returns an integer which holds a windows COLORREF. For example if the pointer is over a pure white pixel the integer returned is 16777215 (0xFFFFFF). It works fine in C# but when I try to run the same code (after converting it to VB.Net of course) I always get the value -1. Why is this? Surely an integer in C# is the same as an integer in VB.Net - they both derive from the same class in the CLR don't they?

    Anyway, the C# code to get the color is as follows:
    <code>// The following goes in the declarations section...
    [DllImport("gdi32.dll")]
    public static extern int GetPixel(IntPtr hdc, int x, int y);

    // ...then later I do this to get the colour under the current pixel.
    int cr = GetPixel(hdcScreen, pt.X, pt.Y);</code>
    The same code in VB.Net looks like this:
    <code> 'Declare call the GDI32.DLL GetPixel function. Should return a valid integer.
    <DllImport("gdi32.dll")> _
    Public Shared Function GetPixel(ByRef hdc As IntPtr, ByVal x As Integer, ByVal y As Integer) As Integer

    ...etc...

    'Get the colour at the current pixel.
    Dim cr As Integer = GetPixel(hdcScreen, pt.X, pt.Y)

    End Function</code>

    Can anybody tell me why the VB code returns -1 and the C# code returns the correct value of 16777215? Do I need to do something else in my VB code like a cast or something?

    Pretty confused here so any help would be appreciated!

    TIA...

    Mike.

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I have seen people use the Int32, Int16, or others in VB.Net instead of the straight Integer variable. You might want to try one of those. I am not exactly sure why that is happening.

  3. #3
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Also, here is some info from Cander on the subject of API's for VB.Net (you can find more info on API calling if you search for call API):

    How do I use API calls in .NET?
    There seems to be the big myth running around that you cannot use API calls in .NET applications.
    This is false. You can use API functions in your .NET apps pretty much the same way you always
    have before, BUT, there are some rules you need to know first. First off, the .NET framework
    already provides classes to many functions that you used to use the API for previously. So before
    you use a call, make sure there is no .NET way to do it as adding API calls makes your app
    unmanaged by the framework and it loses some of the benfits(such as security) of it. You can
    check http://www.allapi.net for a list of functions that are provided by the framework to replace
    using the API. NOTE: As of this time they have not included anything in the list, but bookmark
    it for later reference when they do.

    If you do end up having to use and API call, here is what is different.
    a) When calling a function, you can specify if it will return Ansi or Unicode. VB6 by default
    returned Ansi.


    visual basic code:-------------------------------------------------------------------------------- Public Declare Ansi functionname .....
    --------------------------------------------------------------------------------

    b) You will need to change return value and parameter datatypes. Dataypes have changed, so you
    will need to change them as appropriate. For instance:
    If it was Long in VB6, change it to Integer
    If it was Integer, change it to Short
    If it was Any, you will need to find out what the correct data type will be.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    0xFFFFFFFF would be interpreted as -1, don't know about 0xFFFFFF, but I guess it's truncated to the 16-bit 0xFFFF which is interpreted as -1 too.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Member
    Join Date
    Aug 2002
    Posts
    32
    Thanks for the suggestions folks - turns out I had a ByRef parameter in my call to the GDI32 which should have been ByVal.
    Once I changed that it started working!

    One other thing though, how do you catch a mouse down event using the API? EVen better, does anyone have any example code I could snaffle - got a big client deadline and since I haven't done much (any!) API stuff before I'm a little lost as to how trap a mouse button when the user clicks outside the .Net form.

    TIA for any help...

    Mike.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    With a system-wide mouse hook.
    That's more than just a little bit of API, it involves DLLs and callbacks.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

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