Results 1 to 5 of 5

Thread: VC++ Function Crashes VB when called.

  1. #1
    Dreamlax
    Guest

    VC++ Function Crashes VB when called.

    I'm an oldie with VB, but a newbie at VC++. I have an app in VB, which I want to do the graphics in a faster language, such as VC++. I've written the function out and I get 1 warning when I compile it, and it says the thingy isn't initialised. Here's the function (RGBVal is just another function which I made so I could customise it):

    Code:
    DLLExport long XTitle( HDC DestDC, long Width)
    {
    	int X;
    	HGDIOBJ OldColour;
    	HBRUSH Colour;
    	POINT *PrevPoints;
    
    	Colour = CreateSolidBrush ( 0 );
    	OldColour = SelectObject ( DestDC, Colour );
    
    	for (X = 0; X < 21; X++)
    	{
    		Colour = CreateSolidBrush ( RGBVal(0, X / 20 * 128, X / 20 * 255) );
    		DeleteObject ( SelectObject ( DestDC, Colour ) );
    		MoveToEx ( DestDC, X, 0, PrevPoints );
    		LineTo ( DestDC, X, Width );
    	}
    	for (X = 0; X < 6; X++)
    	{
    		Colour = CreateSolidBrush ( RGBVal(0, X / 5 * 128, X / 5 * 255) );
    		DeleteObject ( SelectObject ( DestDC, Colour ) );
    		MoveToEx ( DestDC, X, 0, PrevPoints );
    		LineTo ( DestDC, X, Width );
    	}
    	DeleteObject ( SelectObject( DestDC, OldColour ) );
    
    	return 1;
    }
    It says PrevPoints isn't initiallised. I ignored it and tried to use that function in VB, but it crashes VB and says that it can't "write" (quotes included) to 0xcccccccc.

    If what I'm doing is completely wrong, don't laugh! I'm relatively new to VC and not sure how it works.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Here is the problem.

    POINT *PrevPoints;
    MoveToEx ( DestDC, X, 0, PrevPoints );

    The PrevPoints pointer points to nowhere. When MoveToEx then tries to write to that memory location it results in an access violation.
    Since you don't use those points, you could simply pass NULL to MoveToEx, but you can also give it a valid address, that is an address of a variable that exists.

    POINT PrevPoint;
    MoveToEx ( DestDC, X, 0, &PrevPoints );

    The & is the address-of operator, it gives you the memory location of a variable.
    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.

  3. #3
    Dreamlax
    Guest
    Cheers!. I didn't know that MoveToEx took NULL as a parameter... but it all works now.

    maybe you can help me one more time. I'm trying to use CallWindowProc, GetWindowLong and SetWindowLong to subclass a window, but whenever I attempt to pass the pointer of the WindowProc to SetWindowLong it says it can't convert from 'long' to 'some big fat thing'.

    Here's the code:
    Code:
        long OldWindoProc;
        OldWindoProc = GetWindowLong ( Windo, GWL_WNDPROC );
        SetWindowLong ( Windo, GWL_WNDPROC, *WindoProc);
    and in the WindoProc function:
    Code:
    long WindoProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
        //Blah blah
        WindoProc = CallWindowProc ( OldWindoProc, hWnd, uMsg, wParam, lParam );
    }
    As I said, I'm a newbie at VC++... and I have no books or tutorials, except MSDN, which doesn't help much because most of the time I don't know what I should be looking for!

    Cheers

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Use only WindoProc (not *WindoProc), and you need to explicitly cast it to long:
    WNDPROC oldProc = (WNDPROC)SetWindowLong(hwnd, GWL_WNDPROC, (LONG)WindoProc);

    BTW this code is not compatible to 64-bit windows, you should use SetWindowLongPtr instead:
    WNDPROC oldProc = (WNDPROC)SetWindowLongPtr(hwnd, GWL_WNDPROC, (INT_PTR)WindoProc);
    This works with both 32 and 64 bit windows.
    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
    Dreamlax
    Guest
    Cool beans bro!

    Thanks for that!

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