Sign in to follow this  
Zeke

Win32, Combobox and GetItemData/SetItemData

Recommended Posts

Hi there, I am trying to store a ptr to a D3DDISPLAYMODE in the data portion of a combo box item. The following code snippet will best describe my problem.

void COptionsDlg::InitAdapterModeCombo()
{
	HWND hWndModeList=GetDlgItem(m_hDlg,IDC_ADAPTERMODES);
	int iModes=m_d3d->GetDevice()->GetAdapterModeCount(D3DADAPTER_DEFAULT);
	if(iModes)
	{
		int x;
		for(x=0;x<iModes;x++)
		{
			D3DDISPLAYMODE mode=new D3DDISPLAYMODE;
			m_d3d->GetDevice()->EnumAdapterModes(D3DADAPTER_DEFAULT,x,mode);
			char modebuf[128]={0};
			_snprintf(modebuf,sizeof modebuf,"%ldx%ld",m_AdapterModes[x].Width,m_AdapterModes[x].Height);
			SendMessage(hWndModeList,CB_ADDSTRING,0,(LPARAM)modebuf);
			SendMessage(hWndModeList,CB_SETITEMDATA,x,(LPARAM)mode);
			//the following line gives me the correct D3DDISPLAYMODE
			D3DDISPLAYMODE* mode2=(D3DDISPLAYMODE*)SendMessage(hWndModeList,CB_GETITEMDATA,x,0);
		}
		SendMessage(hWndModeList,CB_SETCURSEL,0,0);
	}
	//the following gives me a NULL pointer
	D3DDISPLAYMODE* mode2=(D3DDISPLAYMODE*)SendMessage(hWndModeList,CB_GETITEMDATA,0,0);
}


I dont understand why as soon as I set the data I can retrieve it but once I am out of that loop it returns me a NULL pointer. Does anyone have any idea's?

Share this post


Link to post
Share on other sites
try something like storing your modes somewhere (a std:vector, maybe) and store the mode's index in the combo box item.

Share this post


Link to post
Share on other sites
The buffer, modebuf goes out of scope and gets returned to the stack to be used by the next proc that comes along. You can solve it by declaring data on the heap ( remembering to clean it up afterwards) or have some form of global string manager.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this