• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
noatom

Error while getting items from listview

3 posts in this topic

I have to get the items from a listivew,the problem is that the listview is in another window,which is not mine.I managed to get the hwnd of the main window,and using it i scanned for child windows.I found the listview's hwnd,then I used this code to get the first item's text:

 

LV_ITEM itemInfo = {0};

itemInfo.iItem = 0;
itemInfo.iSubItem = 0;
itemInfo.mask = LVIF_TEXT;
itemInfo.cchTextMax = 256;

ListView_GetItem(hwnd,&itemInfo);
}
cout << itemInfo.pszText << endl;


The error:
Unhandled exception at 0x5745d540 (msvcr100d.dll) in c++test2.exe: 0xC0000005: Access violation reading location 0x00000000.

 

What is wrong?

0

Share this post


Link to post
Share on other sites
At least one problem is that you aren't allocating room for the string. If you want to receive text via the pszText member, you need to point it to a buffer first.
1

Share this post


Link to post
Share on other sites

I've been having the same trouble a while ago, but it can be done as explained here.

 

Here's the code of my program to show you how it's done, i know the code look bad, i wrote this fast and haven't polished it afterward... but it work!

The important bits are in GenIconsList().

 

#include "DesktopIconsSaver.h"

/**************************************************
main procedure.....................................
**************************************************/
BOOL WINAPI DllMain(HINSTANCE hInst, DWORD fdwreason, LPVOID lpReserved)
{
    switch(fdwreason)
	{
    case DLL_PROCESS_ATTACH: break;
    case DLL_PROCESS_DETACH: break; 
    case DLL_THREAD_ATTACH:  break;
    case DLL_THREAD_DETACH:  break;
    }
    return TRUE;
}

///////////////////////////////////////////////////////////////////////////////////////////////

HWND FindDesktopListView()
{
	HWND hProgManWindow = FindWindow("Progman", "Program Manager");

	HWND Res = NULL;
	if(hProgManWindow)
		Res = GetWindow(GetWindow(hProgManWindow, GW_CHILD), GW_CHILD);

	return Res;
}

///////////////////////////////////////////////////////////////////////////////////////////////

void GetDesktopSize(POINT *p)
{
	RECT r;
	GetClientRect(FindDesktopListView(), &r);

	p->x = r.right - r.left;
	p->y = r.top - r.bottom;
}

///////////////////////////////////////////////////////////////////////////////////////////////

BOOL GetItemPosition(HANDLE hProc, HWND h, int Indx, LPPOINT lpPos, LPPOINT pPos)
{
	DWORD NumRead = 0;
	ZeroMemory(pPos, sizeof(POINT));
	
	BOOL Res = ListView_GetItemPosition(h, Indx, (POINT FAR*)lpPos);
	ReadProcessMemory(hProc, lpPos, pPos, sizeof(POINT), &NumRead);

	return Res;
}

///////////////////////////////////////////////////////////////////////////////////////////////

UINT GetItemText(HANDLE hProc, HWND h, int Indx, LV_ITEM* lpItem, LPSTR lpTxt, LPSTR pTxt)
{
	DWORD NumRead = 0;
	DWORD NumWrite = 0;
	ZeroMemory(pTxt, TEXT_BUFFER_SIZE);
	
	LV_ITEM Item;
	ZeroMemory(&Item, sizeof(LV_ITEM));
	Item.mask = LVIF_TEXT;
	Item.pszText = lpTxt;
	Item.cchTextMax = TEXT_BUFFER_SIZE;
	WriteProcessMemory(hProc, lpItem, &Item, sizeof(LV_ITEM), &NumWrite); 
	
	DWORD Res = SendMessage(h, LVM_GETITEMTEXT, Indx, (LPARAM)(LV_ITEM FAR*)lpItem);
	
	ReadProcessMemory(hProc, lpTxt, pTxt, TEXT_BUFFER_SIZE, &NumRead);

	return Res;
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool GenIconsList()
{
	IconsData.clear();

	FILE *f = fopen("Debug.txt", "wt+");

	// Retreive the desktop listview handle
	HWND h = FindDesktopListView(); 
	if(h == NULL)
		return false;

	{
		char Msg[1024];
		ZeroMemory(&Msg[0], 1024);
		sprintf(&Msg[0], "h = %X\n", h);
		fputs(&Msg[0], f);
	}

	// Check how many icons are on the desktop
	int NumItems = ListView_GetItemCount(h);
	if(NumItems == 0)
		return false;

	{
		char Msg[1024];
		ZeroMemory(&Msg[0], 1024);
		sprintf(&Msg[0], "NumItems = %d\n", NumItems);
		fputs(&Msg[0], f);
	}

	// Get the process identifier from the window
	DWORD pid = 0;
	GetWindowThreadProcessId(h, &pid);
	if(pid == 0)
		return false;

	{
		char Msg[1024];
		ZeroMemory(&Msg[0], 1024);
		sprintf(&Msg[0], "pid = %X\n", pid);
		fputs(&Msg[0], f);
	}

	// Open the process
	HANDLE hProc = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, pid);
	if(hProc == NULL){
		char Msg[1024];
		ZeroMemory(&Msg[0], 1024);
		sprintf(&Msg[0], "Last Error = %d\n", GetLastError());
		fputs(&Msg[0], f);


		return false;
	}
	
	{
		char Msg[1024];
		ZeroMemory(&Msg[0], 1024);
		sprintf(&Msg[0], "hProc = %X\n", hProc);
		fputs(&Msg[0], f);
	}

	// Allocate memory inside the process
	LPPOINT   lpPos = (LPPOINT) VirtualAllocEx(hProc, NULL, sizeof(POINT),    MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
	LPSTR     lpTxt = (LPSTR)   VirtualAllocEx(hProc, NULL, TEXT_BUFFER_SIZE, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
	LV_ITEM* lpItem = (LV_ITEM*)VirtualAllocEx(hProc, NULL, sizeof(LV_ITEM),  MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);

	// Local variables
	CIconsDataStruct Dat;
	
	// Get the position and text of each icons
	for(int Cpt = 0; Cpt < NumItems; Cpt++){
		GetItemPosition(hProc, h, Cpt, lpPos, &Dat.Position);
		GetItemText(hProc, h, Cpt, lpItem, lpTxt, &Dat.Text[0]);
		IconsData.push_back(Dat);
	}
	
	// Free allocated memory 
	VirtualFreeEx(hProc, lpItem, sizeof(LV_ITEM),  MEM_DECOMMIT);
	VirtualFreeEx(hProc, lpTxt,  TEXT_BUFFER_SIZE, MEM_DECOMMIT);
	VirtualFreeEx(hProc, lpPos,  sizeof(POINT),    MEM_DECOMMIT);

	// Close the process
    CloseHandle(hProc);

	return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool EXP_FUNC _Save(char *fname)
{
	// Generate the icons data list
	GenIconsList();

	// Create the save file and write icons positions and text into it
	FILE *f = fopen(fname, "wb+");
	if(!f)
		return false;

	// Write the auto-alignment flag
	DWORD Flags = ListView_GetExtendedListViewStyle(FindDesktopListView());
	BOOL AutoArrange = (Flags & AUTO_ARRANGE_FLAG) > 0;
	fwrite(&AutoArrange, 1, sizeof(BOOL), f);

	// Get how many icons we have to save
	int NumItems = IconsData.size();

	// Write the number of icons saved
	fwrite(&NumItems, 1, sizeof(int), f);

	// Get the position and text of each icons
	for(int Cpt = 0; Cpt < NumItems; Cpt++){

		// Write the icon position
		fwrite(&IconsData[Cpt].Position, 1, sizeof(POINT), f);
			
		// Write the icon text length
		int TxtLen = strlen(&IconsData[Cpt].Text[0]);
		fwrite(&TxtLen, 1, sizeof(int), f);

		// Write the icon text 
		fwrite(&IconsData[Cpt].Text[0], 1, TxtLen+1, f);
	}

	// Close the file
	fclose(f);
		
	return true;
}

///////////////////////////////////////////////////////////////////////////////////////////////

bool EXP_FUNC _Load(char *fname)
{
	FILE *f = fopen(fname, "rb");
	if(!f)
		return false;

	BOOL AutoArrange = FALSE;
	fread(&AutoArrange, 1, sizeof(BOOL), f);

	DWORD Flags = ListView_GetExtendedListViewStyle(FindDesktopListView());
	if(AutoArrange){
		Flags |= AUTO_ARRANGE_FLAG;
	} else {
		Flags &= ~(AUTO_ARRANGE_FLAG);
	}
	ListView_SetExtendedListViewStyle(FindDesktopListView(), Flags);

	if(AutoArrange){
		GenIconsList();

		for(UINT Cpt = 0; Cpt < IconsData.size(); Cpt++)
			ListView_Update(FindDesktopListView(), Cpt);
	}

	// Get the desktop size
	POINT LVSize;
	GetDesktopSize(&LVSize);
	LVSize.y = abs(LVSize.y);

	// Get how many icons we have to load
	int NumItems = 0;
	fread(&NumItems, 1, sizeof(int), f);

	vector<CIconsDataStruct> SavedIconsData;

	// Get the position and text of each icons
	for(int Cpt = 0; Cpt < NumItems; Cpt++){

		CIconsDataStruct IconDat;
		// Read the icon position
		fread(&IconDat.Position, 1, sizeof(POINT), f);
			
		// Read the icon text length
		int TxtLen = 0;
		fread(&TxtLen, 1, sizeof(int), f);

		// Read the icon text 
		ZeroMemory(&IconDat.Text[0], TEXT_BUFFER_SIZE);
		fread(&IconDat.Text[0], 1, TxtLen+1, f);

		// Add the icon to the list
		SavedIconsData.push_back(IconDat);
	}

	// Close the file
	fclose(f);

	/////////////////////////////////////////////////////////////////////////////////////

	// Get the desktop listview handle
	HWND h = FindDesktopListView();
	
	// Used to avoid an infinite loop (unlikely)
	int NumLoops = 0;

RetryJmp:
	NumLoops++;

	// Generate the icons data list
	GenIconsList();

	// Replace all matching icons that have changed position
	for(UINT j = 0; j < IconsData.size(); j++){
		for(UINT i = 0; i < SavedIconsData.size(); i++){

			char *pIconText = &IconsData[j].Text[0];
			char *pSavedIconText = &SavedIconsData[i].Text[0];
				
			POINT IconPos = IconsData[j].Position;
			POINT SavedIconPos = SavedIconsData[i].Position;
				
			if(strcmp(pSavedIconText, pIconText) == 0){
				if(memcmp(&SavedIconPos, &IconPos, sizeof(POINT)) != 0){
					POINT pt = SavedIconPos;
					if(pt.x >= LVSize.x || pt.y >= LVSize.y){
						char ErrMsgBuf[1024];
						ZeroMemory(&ErrMsgBuf[0], 1024);
						sprintf(&ErrMsgBuf[0], "Warning: The icon named %s was saved at position (%d, %d) wich fall outside\n the desktop area. Skiping item.", pSavedIconText, SavedIconPos.x, SavedIconPos.y);
						MessageBox(0, &ErrMsgBuf[0], "Warning", MB_OK);
						continue;
					}

					SendMessage(h, LVM_SETITEMPOSITION, j, MAKELPARAM((int)pt.x, (int) pt.y));				
				}
			}		
		}
	}

	// Avoid looping indefinitely
	if(NumLoops > 1000)
		return false;

	// If an icon is at the wrong place, repeat the above process
	GenIconsList();

	// Restart the process above if something don't match 
	for(UINT j = 0; j < IconsData.size(); j++){
		for(UINT i = 0; i < SavedIconsData.size(); i++){

			char *pIconText = &IconsData[j].Text[0];
			char *pSavedIconText = &SavedIconsData[i].Text[0];
				
			POINT IconPos = IconsData[j].Position;
			POINT SavedIconPos = SavedIconsData[i].Position;
				
			if(strcmp(pSavedIconText, pIconText) == 0){
				if(memcmp(&SavedIconPos, &IconPos, sizeof(POINT)) != 0){
					POINT pt = SavedIconPos;
					if(pt.x >= LVSize.x || pt.y >= LVSize.y)
						continue;

					goto RetryJmp;				
				}
			}		
		}
	}

	return true;
}

Edited by Vortez
0

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  
Followers 0