You first need to get a pointer to the actual Surface interface.
Here is some sample code, maybe this will clear things up.
CBackBuffer.h
Code:
#ifndef BACKBUFFER
#define BACKBUFFER
#include <d3dx9.h>
#include "D3DError.h"
class CBackBuffer
{
protected:
IDirect3DDevice9 *Device;
IDirect3DSwapChain9 *SwapChain;
IDirect3DSurface9 *Surface;
D3DSURFACE_DESC BufferDesc;
D3DLOCKED_RECT BLRect;
void *Buffer;
int MemPitch;
public:
CBackBuffer(IDirect3DDevice9 *device);
int Pitch(void) {return MemPitch;};
int Width(void) {return BufferDesc.Width;};
int Height(void) {return BufferDesc.Height;};
void *Lock(void);
HRESULT Unlock();
};
#endif
CBackBuffer.cpp
Code:
#include "CBackBuffer.h"
#include <stdio.h>
CBackBuffer::CBackBuffer(IDirect3DDevice9 *device)
{
Device=device;
Device->GetSwapChain(0,&SwapChain);
SwapChain->GetBackBuffer(D3DADAPTER_DEFAULT,D3DBACKBUFFER_TYPE_MONO,&Surface);
if (!Surface) ::MessageBox(0,"GetBackBuffer Failed",0,0);
if FAILED(Surface->GetDesc(&BufferDesc)) ::MessageBox(0,"GetDesc Failed",0,0);
}
void *CBackBuffer::Lock(void)
{
D3DLOCKED_RECT lrect;
HRESULT hr;
hr=Surface->LockRect(&lrect,0,0);
if FAILED(hr)
{
char *text=D3DError::GetTextFromD3DError(hr);
::MessageBox(0,text,0,0);
return NULL;
}
MemPitch=lrect.Pitch>>2;
return lrect.pBits;
}
HRESULT CBackBuffer::Unlock(void)
{
HRESULT hr=Surface->UnlockRect();
return hr;
}
Examine that and see if you can figure out your problem. Something does not look right in your code and I think you are skipping a step. You cannot lock a buffer that you do not have access to via a COM interface pointer.
I use IDirect3DSurface9 as the Surface interface pointer and then use BufferDesc which is a D3DSURFACE_DESC object. Since I want the back buffer in the swap chain I also retrieve a pointer to that interface. Basically I walk down the hierarchy chain from the actual swap chain down to the surface, down to the buffer, down to the locked buffer, down to a pointer to the buffer.