Here's a minimal example I've used in the past. If you want perfect performance, it's probably a better idea to use a system-memory texture and then upload that instead, perhaps even with a ring-buffer of 2-3 textures to increase parallelism, but this is a bit shorter.
#include <windows.h>
#include <D3D9.h>
typedef IDirect3D9* (WINAPI * PFNDIRECT3DCREATE9)(UINT SDKVersion);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void renderFrame(BYTE *buffer, UINT rowBytes, UINT width, UINT height);
// Main
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// Load D3D9
HMODULE hD3D9Lib = LoadLibrary(TEXT("d3d9.dll"));
PFNDIRECT3DCREATE9 pDirect3DCreate9 = (PFNDIRECT3DCREATE9)GetProcAddress(hD3D9Lib, "Direct3DCreate9");
if(hD3D9Lib == NULL || pDirect3DCreate9 == NULL) {
MessageBox(NULL, TEXT("Failed to load D3D9"), TEXT("Error"), MB_OK);
return 0;
}
// D3D9
LPDIRECT3D9 pD3D9 = pDirect3DCreate9(D3D_SDK_VERSION);
if(pD3D9 == NULL) {
MessageBox(NULL, TEXT("Direct3DCreate9 failed"), TEXT("Error"), MB_OK);
return 0;
}
// Register windowclass
WNDCLASSEX wc;
ZeroMemory(&wc, sizeof(wc));
wc.cbSize = sizeof(wc);
wc.lpszClassName = TEXT("MyClass");
wc.hInstance = hInstance;
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx(&wc);
// Create window
int screenWidth = 1024;
int screenHeight = 768;
DWORD windowExStyle = WS_EX_OVERLAPPEDWINDOW;
DWORD windowStyle = WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN;
RECT windowRect;
SetRect(&windowRect, 0, 0, screenWidth, screenHeight);
AdjustWindowRectEx(&windowRect, windowStyle, FALSE, windowExStyle);
HWND hWnd = CreateWindowEx(
windowExStyle,
wc.lpszClassName,
TEXT("D3D9 Window"),
windowStyle,
CW_USEDEFAULT,
CW_USEDEFAULT,
windowRect.right - windowRect.left,
windowRect.bottom - windowRect.top,
NULL,
NULL,
hInstance,
NULL
);
// Device
LPDIRECT3DDEVICE9 pDevice;
D3DPRESENT_PARAMETERS pp;
ZeroMemory(&pp, sizeof(pp));
pp.Windowed = TRUE;
pp.hDeviceWindow = hWnd;
pp.SwapEffect = D3DSWAPEFFECT_DISCARD;
pp.BackBufferFormat = D3DFMT_X8R8G8B8;
pp.BackBufferCount = 1;
pp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;//D3DPRESENT_INTERVAL_ONE for vsync
pp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER;
HRESULT hResult = pD3D9->CreateDevice(
D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&pp,
&pDevice
);
if(FAILED(hResult)) {
MessageBox(NULL, TEXT("CreateDevice"), TEXT("Error"), MB_OK);
return 0;
}
pD3D9->Release();
// Get back-buffer
LPDIRECT3DSURFACE9 pBackBuffer;
hResult = pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
if(FAILED(hResult)) {
MessageBox(NULL, TEXT("GetBackBuffer"), TEXT("Error"), MB_OK);
return 0;
}
// Main loop
ShowWindow(hWnd, nCmdShow);
while(true) {
MSG msg;
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) != 0) {
if(msg.message == WM_QUIT)
break;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else {
// Draw frame
pDevice->BeginScene();
D3DSURFACE_DESC backBufferDesc;
pBackBuffer->GetDesc(&backBufferDesc);
D3DLOCKED_RECT lockedRect;
hResult = pBackBuffer->LockRect(&lockedRect, NULL, D3DLOCK_NOSYSLOCK);
if(SUCCEEDED(hResult)) {
renderFrame((BYTE*)lockedRect.pBits, lockedRect.Pitch, backBufferDesc.Width, backBufferDesc.Height);
pBackBuffer->UnlockRect();
}
// End frame
pDevice->EndScene();
// Present to screen
if(FAILED(pDevice->Present(NULL, NULL, NULL, NULL))) {
Sleep(100);
}
}
}
// Release
pBackBuffer->Release();
pDevice->Release();
UnregisterClass(wc.lpszClassName, hInstance);
FreeLibrary(hD3D9Lib);
return 0;
}
// Window procedure
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch(msg) {
// Window destroyed
case WM_DESTROY:
PostQuitMessage(0);
return 0;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}
// Render
void renderFrame(BYTE *buffer, UINT rowBytes, UINT width, UINT height) {
for(UINT i=0;i<height;++i) {
UINT32 *line = (UINT32*)(buffer + i * rowBytes);
UINT red = rand() & 0xff;
UINT green = rand() & 0xff;
UINT blue = rand() & 0xff;
for(UINT j=0;j<width;++j) {
line[j] = (red << 16) | (green << 8) | blue;
--red &= 0xff;
++green &= 0xff;
}
}
}

Find content
Not Telling