Jump to content

  • Log In with Google      Sign In   
  • Create Account

Error while getting items from listview


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 09 May 2013 - 04:58 PM

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?



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9604

Like
1Likes
Like

Posted 09 May 2013 - 05:03 PM

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.

#3 noatom   Members   -  Reputation: 782

Like
0Likes
Like

Posted 10 May 2013 - 06:14 AM

just found out it won't work since it's a different process...



#4 Vortez   Crossbones+   -  Reputation: 2698

Like
0Likes
Like

Posted 11 May 2013 - 10:25 AM

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, 11 May 2013 - 10:29 AM.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS