Thread: Pass 2D-Array by Ref/Pointer

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    1

    Angry Pass 2D-Array by Ref/Pointer

    Hi guys,

    I am trying to pass a 2D Byte Array by pointer from VB 6 to C++ DLL for faster processing.

    Here is the VB code i'm using:
    Code:
    Private Declare Function ImageBrightness Lib "ipong.dll" (ByRef lpDib As Any, uBPSL As Long, uHeight As Long, Value As Long) As Long
    
    Private Sub Command1_Click()
        Dim bDib() As Byte
        ReDim bDib(5, 1)
        bDib(0, 0) = 255
        bDib(1, 0) = 255
        bDib(2, 0) = 255
        bDib(3, 0) = 0
        bDib(4, 0) = 0
        bDib(5, 0) = 255
        bDib(0, 1) = 0
        bDib(1, 1) = 255
        bDib(2, 1) = 0
        bDib(3, 1) = 255
        bDib(4, 1) = 0
        bDib(5, 1) = 0
        Call ImageBrightness(bDib(0, 0), 6, 2, 0)
    End Sub
    And here is my c code (although the algorithm does invert not brightness)...
    Code:
    #include "stdafx.h"
    #include "resource.h"
    #include "windows.h"
    #include "oleauto.h"
    
    BOOL __declspec (dllexport) __stdcall ImageBrightness(BYTE **bDib, UINT uBPSL, UINT uHeight, UINT Value);
    
    
    HINSTANCE ghInst;
    
    int APIENTRY DllMain(HINSTANCE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
    {
    	if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    		ghInst = hModule; 
    	return TRUE;
    }
    
    BOOL __declspec (dllexport) __stdcall ImageBrightness(BYTE **bDib, UINT uBPSL, UINT uHeight, UINT Value)
    {
    
    	UINT nW, nH;
    	nW = uBPSL - 2;
    	nH = uHeight - 1;
    	
    	UINT x, y, x_1, x_2;
    
    	for(x=0;x<nW;x=x+3)
    	{
    		for(y=0;y<nH;y++)
    		{
    			x_1 = x + 1;
    			x_2 = x + 2;
    			//Invert each byte
    			bDib[x_2][y] = 255 - bDib[x_2][y];
    			bDib[x_1][y] = 255 - bDib[x_1][y];
    			bDib[x][y]   = 255 - bDib[x][y];
    
    		}
    	}
    
    	return true;
    
    }
    When i call the ImageBrightness function my vb executable crashes. I understand how pointers work but not completely sure how to deal with this scenario. Any help would be much appreciated.

    Cheers
    option

  2. #2
    Registered User Queatrix's Avatar
    Join Date
    Apr 2005
    Posts
    1,342
    In you'r ImageBrightness(), put MessageBox() calls all over it, to see where it crashes; So you know where the problem is.

    However, just by looking at it, I would say you are trying to I/O beyond your matrix' size. In C/C++ I know that you will crash you'r program very easily if you try.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Speed test result
    By audinue in forum C Programming
    Replies: 4
    Last Post: 07-07-2008, 05:18 AM
  3. 2d array problem with vc++
    By LiLgirL in forum C++ Programming
    Replies: 10
    Last Post: 03-16-2004, 08:17 PM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. 2d Array access by other classes
    By deaths_seraphim in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2001, 08:05 AM