Re: Unknown pointer problem between Visual Basic and C

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance

From: Maximiliano Rios (maximiliano_rios_at_hotmail.com)
Date: 02/11/05


Date: Fri, 11 Feb 2005 12:02:57 -0300

Hi Craig, really thanks a lot for answering.
I'm going to explain more detailed:

how application works:

Objective: Draw into a picture area. I can draw everything, except complex
GIS data, provided for a third party library. That library is C coded; I
can't access the methods from Visual Basic directly, so, I've built an small
VB compatible C library to pass information. That library works ok with
every method, except drawing from Visual Basic (HDC seems to be wrong). I
use same library from C#, and it works ok!

>From Visual Basic
- I get a device context and create Compatible DC.
- For drawing, I create a pen and assign to DC
- For drawing, I create a solid brush an assign to DC
- I create a compatible bitmap, white, solid, with picture dimensions and
assign to DC
- For checking, I draw a simple diagonal line, it works and I can see the
line into the picture
- I send to the "Visual Basic wrapper" built in C, dimensions and DC pointer
- Third party library fails. Really, doesn't hang, but send to me a result
code with an unspecified error trying to draw.

>From Dot Net C#
- I create a bitmap object
- I Get a graphic object from this bitmap
- I create a white SolidBrush object
- I fill graphics rectangle with that brush, with a screen picture dimension
- I set background bitmap from picture to the created Bitmap object
- I send to the "Visual Basic wrapper" built in C, dimensions and DC pointer
- Third party library DRAW the picture perfect !!!!!!!

C code looks like this:

int _stdcall DrawLayer(int hDC, int aLayer, double... )

Int pointer is sent from Visual Basic and Dot Net, value is okay, I put
debug code to check it;

>From Visual Basic I send DC obtained from GetDC(picture1.hWND) or
picture1.hDC (both of them fails)
>From Dot Net I send IntPtr object. (works!)

Craig, your code, used in my Visual Basic code works fine, I can see an
small colored box, It probes than DC pointer is valid.

Thanks.

"Craig Kelly" <cnkelly.nospam@nospam.net> escribió en el mensaje
news:l0SOd.188112$w62.123661@bgtnsc05-news.ops.worldnet.att.net...
> "Maximiliano Rios" wrote:
>
> > But, I ever can't draw with a HDC sent from Visual Basic, everytime
> > fails. Every method works okay, but I send a pointer from a picture
> > (for example) or from a window, and I get an unexpected error when I
> > try to use it. Application doesn't cause GPF, but I get an error or
> > (the worst) nothing happens.
>
> A few thoughts...
>
> - Have you checked the AutoRedraw property of the PictureBox? If it's
> False, the picture box must be visible for the drawing to take effect.
>
> - Does your C code cache the HDC for use later? The HDC can change, so
you
> really need to pass it to the C code every time you perform your drawing
> operations.
>
> - Does your C code erase the HDC first?
>
> A simple test would be
>
> SomePictureBox.AutoRedraw = True
> SomePictureBox.Cls
> Call MyBigCFunction(SomePictureBox.hDC)
>
> To prove to yourself that this works, you can ever substitute a VB
function
> to draw to the HDC via the API. Before posting this, I cobbled together a
> test:
>
> Dim hbr As Long
> Dim r As RECT
>
> Picture1.Cls
>
> r.Bottom = (Rnd * 240) + 1
> r.Right = (Rnd * 240) + 1
>
> hbr = CreateSolidBrush(&HFFAAAA)
> FillRect Picture1.hdc, r, hbr
> Call DeleteObject(hbr)
>
>
> This worked fine. If none of the above helps, perhaps you could post some
> sample code to test?
>
> Craig
>
>



Relevant Pages

  • Re: Custom ScaleMode setting problem
    ... It sets up a picture box in the way that you require. ... In this way you can draw the finished graph at any physical location and size you require, opaquely or transparently, and you can draw it to the printer in a way that takes full advantage of the printer's high resolution. ... Private pOffsetX As Single, pOffsetY As Single ...
    (microsoft.public.vb.general.discussion)
  • Re: lines dont stay on form...
    ... Personally for such jobs I would draw ... On way to achieve what you're after and still use your existing "picture ... Private xCentre As Single, yCentre As Single ... Private Sub Label1_DblClick ...
    (microsoft.public.vb.general.discussion)
  • Re: Where to file one defgeneric for two classes and two packages
    ... Suppose I want to draw pictures and lines on a display. ... I just apply the draw method, but for lines, I may want to specify the ... Presumably your picture and line objects keep track of their own ... This is the kind of situation I envision calling a generic function ...
    (comp.lang.lisp)
  • Re: redraw pictureboxes in image????
    ... own suggested method to perform this job (a Picture Box containing two ... As for your current specific problem, use a Label to hold the "number in the ... at their defaults if you wish) and set its ToolTipText to whatever you want. ... Draw your line between the images using the VB Line method (type the word ...
    (microsoft.public.vb.general.discussion)
  • Re: Resize form problem
    ... The second function is to draw some vertical lines as sort of grid ... 'Work out marker string and position ... I placed that in the Resize Event as well. ... you're loading an image into the Picture property of a Picture Box and then ...
    (microsoft.public.vb.general.discussion)