views:

66

answers:

1
struct tagBITMAPINFO {
    BITMAPINFOHEADER    bmiHeader;
    RGBQUAD             bmiColors[1];
} BITMAPINFO;

tagBITMAPINFOHEADER{
        DWORD      biSize;
        LONG       biWidth;
        LONG       biHeight;

} BITMAPINFOHEADER


BITMAPINFO bmiCurrWindow;

capGetVideoFormat((*m_pCapWndArray)[i].hCapWnd, &bmiCurrWindow, formatsize);


CopyMemory(&(*m_pCapWndArray)[i].bmiHeader, &(bmiCurrWindow.bmiHeader), sizeof(BITMAPINFOHEADER));

bmiCurrWindow is a structure 'BITMAPINFO' which has member 'bmiHeader' of type BITMAPINFOHEADER;

after copy memory i want to change the value of bmiCurrWindow. i.e i want bmiCurrWindow.bmiHeader.biWidth=480; bmiCurrWindow.bmiHeader.biHeight=640;

But if i write the above code,while executing it gives me access voilation error.

How do i resove it ?

+1  A: 

The correct (and documented) way to use capGetVideoFormat() to to call it twice - once to get the size of memory required, then allocate memory of that size, and then call it a second time to fill in the memory. This is because the BITMAPINFO::bmiColors member is variable-length, so you have to make sure you have enough memory to receive all of that data. The way your code is currently written, you are allocating a BITMAPINFO on the stack, but the actual BITMAPINFO declaration only specifies room for 1 RGBQUAD item in its bmiColors member. If your video format has more than 1 color in it, you are corrupting stack memory. You need to dynamically allocate the BITMAPINFO on the heap instead, for example:

DWORD dwSize = capGetVideoFormat((*m_pCapWndArray)[i].hCapWnd, NULL, 0); 
if (dwSize > 0)
{
  BITMAPINFO *bmpInfo = (BITMAPINFO*) malloc(dwSize);
  if (bmpInfo != NULL)
  {
    capGetVideoFormat((*m_pCapWndArray)[i].hCapWnd, bmpInfo, dwSize);
    (*m_pCapWndArray)[i].bmiHeader = bmpInfo->bmiHeader;
  }
  free(bmpInfo);
}
Remy Lebeau - TeamB