Click here to Skip to main content
15,908,112 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: A question about autorun CD Pin
Blake Miller29-Jan-07 4:13
Blake Miller29-Jan-07 4:13 
QuestionDirectX textures Pin
Waldermort26-Jan-07 22:14
Waldermort26-Jan-07 22:14 
QuestionSimplest way to add tabs to SDI Pin
neilsolent26-Jan-07 22:13
neilsolent26-Jan-07 22:13 
Questionwin32 api edit box issue Pin
asp.netProgrammer26-Jan-07 18:18
asp.netProgrammer26-Jan-07 18:18 
AnswerRe: win32 api edit box issue Pin
asp.netProgrammer26-Jan-07 18:41
asp.netProgrammer26-Jan-07 18:41 
QuestionCaption (title bar) buttons Pin
Hans Dietrich26-Jan-07 18:04
mentorHans Dietrich26-Jan-07 18:04 
AnswerRe: Caption (title bar) buttons Pin
Dominik Reichl27-Jan-07 6:25
Dominik Reichl27-Jan-07 6:25 
QuestionHow do you fix code that works on everything but the listview you need? Pin
___Charles___26-Jan-07 17:41
___Charles___26-Jan-07 17:41 
Hi all, I have a program that needs to pick up the contents of a
SysListView in another process. As you are all aware this requires me
allocating memory for my strings and the LVITEM structure in the other
process, then writing to them and sending the message and then reading
from them.

My code works, this might seem like a strange post but wait the plot
thickens.I tested this program on the task manager, It has four of
these SysListViews hanging around so It seemed as good a candidate as
any for my experiment. I watch the process locate the window (the title
changes so I had to do an enumwindows to find it) then I did an
enumchildwindows in order to find all the SyslistViews. Now I getting
somewhere I think to my self. I call my home-grown C dll with my window
handles and I get the thread and process responsible for these
critters.

I set my debug priveleges and and ask for my window handle "all
access". I allocate my memory watch my lil pointers come back and send
my message and readprocessmemory back into my address space, and
gleefully see the pointers filled with strings from the task manager.

This is where it gets interesting. I change my window title to the name
of the the title to the part of the name of the MDI window that I am
interested in. I get my handles, my process handle, and allocate my
memory, The first call works great, then the problems start ;( The
second call to virtualallocex succeeds but the pointes now have the
"bad ptr" following the address that was allocated, "that wasn't there
on the other window" I think to myself, and sure enough the strings
come back empty.

So I decide to google this one. Two days of googling and no cigar.
While googling I also decided that virtualallocex wasn't to be trusted
to find the base address in the other process, so I decided to walk the
vad and find my own address, this worked as well, and I locate memory
locations large enough to hold my memory , but still no cigar. It works
for the task manager but not the target program ;(

By this time I have almost read anything I could find on virtualallocex
and by passing most of the other virtual memory functions, I fiddled
with virtualqueryex and virtualprotectex, trying to find the problem.
FormatMessage came to mind pretty quickly but "The operation completed
successfully" isn't much help when trying to debug code that is
anything but successful.

At this point let me try to give you a description of my environment:
Window XP PRO SP2 latest updates.
VS.NET 2003, latest updates Enterprise Architect.
An old, P III 500 that if I could get the project to work would provide
me with some cash to buy one of the latest beasts available.
256 Mb of ram (always a drag that swapping ;()

The program I am trying to infiltrate is
EFX Navigator on www.efxgroup.com

For my info tell me what you need, I can get back to you.

Here is my soucre code, The first part is a VB.net module the second
part is as VC++ Win32 dll (hence why I posted here)
This code is anything but tidy but with all the debuging I'm doing
cleaning up old code (that works on the taskmanager and other programs)
doesn't seem like a good approach

Any way here it is:
VB.net Code First
-----------------------------------------------------------------------------------------------------------------------------------------------------
Imports System
Imports System.Runtime.InteropServices
Imports System.Text
Module MainModule
'FormatMessage
<dllimport("kernel32.dll", entrypoint:="FormatMessageA" ,="" _
="" charset:="CharSet.Ansi," exactspelling:="True," callingconvention:="CallingConvention.StdCall)"> _
Private Function FormatMessage( _
ByVal dwFlags As Int32, ByVal lpSource As Int32, _
ByVal dwMessageId As Int32, ByVal dwLanguageId As Int32, _
ByVal lpBuffer As StringBuilder, _
ByVal nSize As Int32, ByVal Arguments As Int32) As Int32
End Function
Private Const FORMAT_MESSAGE_ALLOCATE_BUFFER As Long = &H100
Private Const FORMAT_MESSAGE_FROM_SYSTEM As Long = &H1000
Private Const FORMAT_MESSAGE_IGNORE_INSERTS As Long = &H200
Private Declare Function GetLastError Lib "kernel32.dll" () As
Integer
'declare API functions
Private Delegate Function EnumChildProcDelegate _
(ByVal hWnd As IntPtr, _
ByVal lParam As Integer) As Boolean

Private Declare Function EnumChildWindows Lib "user32" _
(ByVal hWndParent As IntPtr, _
ByVal lpEnumFunc As EnumChildProcDelegate, _
ByVal lParam As Integer) As Boolean

Declare Function GetClassName Lib "user32.dll" Alias
"GetClassNameA" _
(ByVal hwnd As IntPtr, _
ByVal lpClassName As StringBuilder, _
ByVal nMaxCount As Long) As Long

Private Declare Ansi Function FindWindow Lib "user32" Alias
"FindWindowA" _
(ByVal lpClassName As StringBuilder, ByVal lpWindowName As
StringBuilder) As IntPtr

Public Declare Sub GetWindowText Lib "user32.dll" _
Alias "GetWindowTextA" (ByVal hWnd As IntPtr, _
ByVal lpString As StringBuilder, _
ByVal nMaxCount As Integer)

Private Declare Function SendMessage Lib "user32" Alias
"SendMessageA" _
(ByVal hwnd As IntPtr, ByVal wMsg As Integer, _
ByVal wParam As Integer, _
ByVal lParam As String) As Integer

Public Declare Function GetWindow Lib "user32.dll" _
Alias "GetWindow" (ByVal hwnd As Integer, _
ByVal wCmd As Integer) As Integer
'Top Level enumeration
Public Delegate Function EnumWindowsCallback(ByVal hWnd As IntPtr,
_
ByVal lParam As
Integer) As Boolean

Public Declare Function EnumWindows Lib "user32.dll" _
Alias "EnumWindows" (ByVal callback As EnumWindowsCallback, _
ByVal lParam As Integer) As Integer

<dllimport("user32.dll", entrypoint:="EnumWindows" ,
setlasterror:="True," _
="" charset:="CharSet.Ansi," exactspelling:="True," callingconvention:="CallingConvention.StdCall)"> _
Public Function EnumWindowsDllImport(ByVal callback As
EnumWindowsCallback, _
ByVal lParam As
Integer) As Integer
End Function
'My DLL Declares
Private Declare Function CopyListViewToListBox Lib "Extract Data
Library.dll" _
Alias "CopyListViewToListBox" (ByVal SourceHwnd As IntPtr, ByVal
TargetHwnd As IntPtr, _
ByVal Append As Boolean) As Integer

Private Declare Function FillListBox Lib "Extract Data Library.dll"
_
Alias "FillListBox" (ByVal targetHwnd As IntPtr, ByVal item As
String, _
ByVal subitem As String, ByVal Append As
String) As Integer

'Variables
Dim iFound As Int32
Dim aihWnds As ArrayList
Private Const WM_GETTEXT = &HD

'Listbox Functions
Private Declare Function LockWindowUpdate Lib "user32" (ByVal
hwndLock As IntPtr) _
As Long
'Listbox Constants
Const LB_RESETCONTENT = &H184
Const LB_GETCOUNT = &H18B
Const LB_GETTEXT = &H189
Const LB_ADDSTRING = &H180
Const LB_GETITEMDATA = &H199
Const LB_SETITEMDATA = &H19A

Function GetGlass(ByVal hwnd) As String 'classe de la fenetre
Dim sClassName = New StringBuilder(256)
GetClassName(hwnd, sClassName, 256)
sClassName = Left$(sClassName, InStr(sClassName, Chr(0)) - 1)
Return sClassName
End Function

Function GetCaption(ByVal hwnd As IntPtr) As String 'titre du
document
Dim sCaption = New StringBuilder(256)
GetWindowText(hwnd, sCaption, 256)
sCaption = sCaption.ToString
If Len(sCaption) > 1 Then
Debug.WriteLine(sCaption)
If (InStr(sCaption, Chr(0)) - 1) > 0 Then 'trim null
sCaption = Left$(sCaption, InStr(sCaption, Chr(0)) - 1)
End If
End If
Return sCaption
End Function

Function GetCaptionFromMessage(ByVal hwnd) As String 'texte titre
fenetre
Dim sCaption = New String(Chr(0), 100)
Dim retval = SendMessage(hwnd, WM_GETTEXT, 100, sCaption)
sCaption = Left$(sCaption, InStr(sCaption, Chr(0)) - 1)
Return sCaption
End Function

Function EnumChildrenProc(ByVal hwnd As IntPtr, ByVal lParam As
Integer) As Boolean
Dim retval As Long
'error handling goes here good old formatmessage from system
Dim ClassName As New StringBuilder(256)
retval = GetClassName(hwnd, ClassName, 256)

If ClassName.ToString = "SysListView32" Then
'found listbox
Debug.WriteLine(hwnd)
'FillListbox(hwnd)
MainModule.aihWnds.Insert(iFound, hwnd)
iFound = iFound + 1
End If
'If iFound = 1 Then
'Return False 'Found Both of the Listbox Stop enumeration
'Else
Return True 'Still haven't got them both
'End If
End Function

Sub GetListBoxes()
'need to loop through all windows to get the first characters
and compare to level II ...
'Dim sWindowCaption = "Windows Task Manager"
'Dim hwnd = FindWindow(vbNullString, sWindowCaption)
Dim proc As New EnumWindowsCallback(AddressOf
MainModule.EnumWindowProc)
EnumWindows(proc, 0)
'DuplicateListBox(MainForm.Listboxes(0).Handle,
MainForm.Listboxes(1))
End Sub
Private Function EnumWindowProc(ByVal hwnd As IntPtr, ByVal lParam
As Integer) As Boolean
If InStr(GetCaption(hwnd).ToString, "Level II") Then
ProcessEFXChildren(hwnd)
Return False
End If
Return True
End Function

Private Function ProcessEFXChildren(ByVal hwnd As IntPtr)
iFound = 0
aihWnds = New ArrayList
Dim i As Integer
Dim proc As New EnumChildProcDelegate(AddressOf
MainModule.EnumChildrenProc)
Dim retval = EnumChildWindows(hwnd, proc, 0&)
For i = 0 To iFound - 1
CopyListViewToListBox(aihWnds(i),
MainForm.Listboxes(0).Handle, False)
Next
End Function
End Module

---------------------------------------------------------------------------------------------------------------------------------------------

VC++.NET DLL CODE

// Extract Data Library.cpp : Defines the entry point for the DLL
application.
//
//Defines
#define WIN32_LEAN_AND_MEAN
//Includes
#include "stdafx.h"
#include <stdio.h>
#include <windows.h>
#include <commctrl.h>
#define My_EXPORTS //Define in order to
export functions
#include "Extract Data Library.h"
#include <string>
#include <cstdlib>
#include "stdlib.h"
//Declares Exported Functions

//Delcares Internal Functions
DWORD DoFormatMessage();
DWORD DoDebugFormatMessage();
int SetDebugPrivileges(void);
MEMORY_BASIC_INFORMATION WalKingTheVAD(HANDLE hProcess, DWORD
RequiredSpace);
//Exported Variables

//Global Variables

//DLL ENTRRY POINT
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
break;
}
return TRUE;

}

//Code Starts here
My_API_Export int __stdcall CopyListViewToListBox(HWND SourceHwnd, HWND
TargetHwnd, BOOL Append)
{
SetDebugPrivileges();
HWND listview= SourceHwnd;

int count=(int)SendMessage(listview, LVM_GETITEMCOUNT, 0, 0);
int i;

LVITEM lvi, *_lvi;
char item[512], subitem[512];
char *_item, *_subitem;
unsigned long pid;
HANDLE process;
MEMORY_BASIC_INFORMATION mbi;
LPVOID BaseAddress = 0;
GetWindowThreadProcessId(listview, &pid);
//process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|
//
PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
process=OpenProcess(PROCESS_ALL_ACCESS,false, pid);
//DoFormatMessage();

mbi = WalKingTheVAD(process, (DWORD)(sizeof(LVITEM)) );
if (mbi.BaseAddress ==NULL)
{
MessageBox(NULL, "Insufficient Memory in Target Process
to Extract
Data","Fatal Error", MB_ICONERROR);
return -1;
}
BaseAddress = mbi.AllocationBase;
_lvi=(LVITEM*)VirtualAllocEx(process, BaseAddress,
sizeof(LVITEM),

MEM_COMMIT, PAGE_READWRITE);
if (_lvi == NULL) DoFormatMessage();
mbi = WalKingTheVAD(process, (DWORD)(100) );
if (mbi.BaseAddress ==NULL)
{
MessageBox(NULL, "Insufficient Memory in Target Process
to Extract
Data","Fatal Error", MB_ICONERROR);
return -1;
}
BaseAddress = mbi.AllocationBase;
_item=(char*)VirtualAllocEx(process, BaseAddress, 100,
MEM_COMMIT,

PAGE_READWRITE);
DoFormatMessage();
_subitem=(char*)VirtualAllocEx(process,NULL, 100, MEM_COMMIT,

PAGE_READWRITE);
DoFormatMessage();
/*
_lvi=(LVITEM*)VirtualAllocEx(process, NULL, sizeof(LVITEM),

MEM_COMMIT, PAGE_READWRITE);
_item=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,

PAGE_READWRITE);
_subitem=(char*)VirtualAllocEx(process, NULL, 512, MEM_COMMIT,

PAGE_READWRITE);
*/
lvi.cchTextMax=512;

for(i=0; i
AnswerRe: How do you fix code that works on everything but the listview you need? Pin
Mike O'Neill27-Jan-07 12:53
Mike O'Neill27-Jan-07 12:53 
QuestionInvoke another exe from currently running exe Pin
kiranin26-Jan-07 17:36
kiranin26-Jan-07 17:36 
AnswerRe: Invoke another exe from currently running exe Pin
ThatsAlok26-Jan-07 19:32
ThatsAlok26-Jan-07 19:32 
QuestionCOM in VC++ Pin
deeps_cute26-Jan-07 17:02
deeps_cute26-Jan-07 17:02 
QuestionVisual C++ MFC WinSock 2 Reference Pin
Obi Wan 226-Jan-07 13:50
Obi Wan 226-Jan-07 13:50 
AnswerRe: Visual C++ MFC WinSock 2 Reference Pin
Mark Salsbery26-Jan-07 14:03
Mark Salsbery26-Jan-07 14:03 
GeneralRe: Visual C++ MFC WinSock 2 Reference Pin
Obi Wan 227-Jan-07 4:47
Obi Wan 227-Jan-07 4:47 
GeneralRe: Visual C++ MFC WinSock 2 Reference Pin
Mark Salsbery27-Jan-07 7:30
Mark Salsbery27-Jan-07 7:30 
QuestionCListView; getting rid of spurious horizontal scrollbar [modified] Pin
Joe Woodbury26-Jan-07 12:34
professionalJoe Woodbury26-Jan-07 12:34 
AnswerRe: CListView; getting rid of spurious horizontal scrollbar Pin
Ravi Bhavnani26-Jan-07 12:59
professionalRavi Bhavnani26-Jan-07 12:59 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Joe Woodbury26-Jan-07 13:06
professionalJoe Woodbury26-Jan-07 13:06 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Ravi Bhavnani26-Jan-07 13:09
professionalRavi Bhavnani26-Jan-07 13:09 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Joe Woodbury26-Jan-07 13:23
professionalJoe Woodbury26-Jan-07 13:23 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Ravi Bhavnani26-Jan-07 13:27
professionalRavi Bhavnani26-Jan-07 13:27 
AnswerRe: CListView; getting rid of spurious horizontal scrollbar Pin
Mark Salsbery26-Jan-07 13:54
Mark Salsbery26-Jan-07 13:54 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Joe Woodbury26-Jan-07 15:12
professionalJoe Woodbury26-Jan-07 15:12 
GeneralRe: CListView; getting rid of spurious horizontal scrollbar Pin
Mark Salsbery26-Jan-07 16:27
Mark Salsbery26-Jan-07 16:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.