|
Already using ReadProcessMemory, here's the code (source: Stealing Program's Memory[^])
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <windows.h>
#include <commctrl.h>
int main(void) {
HWND hwnd=FindWindow(NULL, "Stealing Program's Memory: ListView");
HWND listview=FindWindowEx(hwnd, NULL, "SysListView32", NULL);
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;
GetWindowThreadProcessId(listview, &pid);
process=OpenProcess(PROCESS_VM_OPERATION|PROCESS_VM_READ|
PROCESS_VM_WRITE|PROCESS_QUERY_INFORMATION, FALSE, pid);
_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<count; i++) {
lvi.iSubItem=0;
lvi.pszText=_item;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
SendMessage(listview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
lvi.iSubItem=1;
lvi.pszText=_subitem;
WriteProcessMemory(process, _lvi, &lvi, sizeof(LVITEM), NULL);
SendMessage(listview, LVM_GETITEMTEXT, (WPARAM)i, (LPARAM)_lvi);
ReadProcessMemory(process, _item, item, 512, NULL);
ReadProcessMemory(process, _subitem, subitem, 512, NULL);
printf("%s - %s\n", item, subitem);
}
VirtualFreeEx(process, _lvi, 0, MEM_RELEASE);
VirtualFreeEx(process, _item, 0, MEM_RELEASE);
VirtualFreeEx(process, _subitem, 0, MEM_RELEASE);
return 0;
}
Its working fine for the demo application, I used create a from in VB and used it, it worked there fine too, but not sure what's causing it to show null values for one the application that has AFX window !!
|
|
|
|
|
How do you know that WriteProcessMemory() and ReadProcessMemory() are working?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
could you please let me know the way to chk it out?
Thanks
|
|
|
|
|
read the documentation!
your code is not safe.
(almost) all Win32 functions have a way to report success/failure and/or an error code.
you should use it. ALWAYS.
|
|
|
|
|
In the same way you'd check the return value from any other function.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
Hi just checked it, they seem to return 1
|
|
|
|
|
both of them returned 1, I just noticed LVS_OWNERDRAWFIXED for my source application's list-view, any ideas on the modifications required in the code to get the list of Items
|
|
|
|
|
If you think that style is causing a problem, create another control without it and see what happens.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Man who follows car will be exhausted." - Confucius
|
|
|
|
|
|
i load an Cimage file in my SDI MFC (non View/Doc arch.) application & draw some shapes & text on it... tell me how i can save both shapes,texts & CImage in any (jpg,bmp) file format...
|
|
|
|
|
I got the answer to this problem while trying to solve my problem:
CImage image;
HRESULT hr = image.Save(_T("C:\\test.jpg"), Gdiplus::ImageFormatJPEG);
|
|
|
|
|
i'm drawing shapes on it(like circle,rectangle etc) using OnPaint(); .... will it work now??
|
|
|
|
|
No; when you use the CDC in OnPaint, your drawing is going to the screen.
To draw on a bitmap you need to select the bitmap into a device context, then draw on that device context. Like:
CDC *mdc = GetDC ();
HGDIOBJ tmp = mdc->SelectObject(hbm);
image.Attach (hbm);
HRESULT hr = image.Save(_T("C:\\test.tif"), Gdiplus::ImageFormatTIFF);
|
|
|
|
|
i'm not using cdc....i'm using CPaintDC dc...so now wht i have to do??
|
|
|
|
|
CPaintDC is derived from CDC, so you can perform all the same operations on it. It's an object rather than a pointer, so you have to use "." to invoke methods instead of "->".
You can select a bitmap into the CPaintDC:
dc.SelectObject (hbm);
Where hbm is the handle of a bitmap. You can also select a pointer to a CBitmap. (I don't see an overload for CImage).
You then use the drawing methods of CDC. Then (as in my example) you can attach your bitmap to a CImage object, and save it.
There may be a better way of doing this, but this should work.
|
|
|
|
|
what is hbm? plz write full code in detail...
|
|
|
|
|
hbm is a handle to a bitmap. The Windows type is HBITMAP.
|
|
|
|
|
Runtime error: tht hbm is not initialized....?
|
|
|
|
|
|
Hi all,
I've been attempting to draw on an 8bpp grayscale bitmap without success. Here are some of my attempts. Maybe someone can point out what I'm doing wrong.
===================================================
Attempt 1: Create, select, and draw:
In constructor:
CBitmap bm;
bm.CreateBitmap (200, 200, 1, 8, NULL);
In OnDraw:
CDC *mdc=new CDC ();
HGDIOBJ tmp = mdc->SelectObject(bm);
Result: tmp is NULL, indicating failure.
===================================================
Attempt 2: CreateDIBSection
In constructor:
HBITMAP hbm;
BITMAPINFOHEADER bih;
BITMAPINFO bi;
HANDLE hb;
CDC* myDc = new CDC ();
HDC hdc = myDc->GetSafeHdc ();
void* bits;
RGBQUAD rq [256];
initBi ();
hbm = CreateDIBSection (hdc, &bi, DIB_RGB_COLORS, &bits, NULL, 0);
...
void CEightBitDrawingView::initBi()
{
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
memset ((void *) rq, 0, 256 * sizeof (RGBQUAD));
bi.bmiHeader = bih;
bi.bmiColors = rq;
}
Result: This doesn't even compile because the BITMAPINFO bmiColors member is defined as:
RGBQUAD bmiColors[1];
so won't accept more than one RGB color. In fact, nothing I assign to this member compiles! (Could they possibly make it any more complex!?)
Any suggestions would be appreciated! Thanks!
===================================================
|
|
|
|
|
Alan Balkany wrote: CDC *mdc=new CDC ();
HGDIOBJ tmp = mdc->SelectObject(bm);
- do you create the DC anywhere or you just instantiate it? You need to create the DC, a CDC instance alone isn't enough. Where does bm come from? Acording to what you said bm would be a local variable in the constructor, so how would it get to OnDraw?
About the RGBQUAD thing:
Alan Balkany wrote: RGBQUAD bmiColors[1];
this means that an array of RGBQUADs containing a variable number of colors will follow directly the BITMAPINFO structure. I think the biClrUsed member would indicate this in the struct (see the documentation for the structure).
You have to allocate enough memory to contain the bitmap info struct AND the palette after it, you can set the members in the palette -if you like- thorough the bmiColors array. Something like:
BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 255);
...fill the bmiHeader ...
bmi->bmiCOlors[0] = firstColor;
bmi->bmiCOlors[1] = secondColor;
...
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Code-o-mat, thanks for clearing up the mystery about bmiColors!
I just created a new DC because (it appears that) existing DCs will default to the color depth of my display. If this isn't correct, what's the right way to obtain a DC to draw on an 8bpp bitmap?
(The bitmap is actually declared in the class, and was just included in the constructor's code to show the type.)
|
|
|
|
|
I needed to do this some time ago and as far as i can remember i went the DIB section way myself, creating a palette that went from 0,0,0 to 255,255,255. If you create a screen-compatible DC and then select your bitmap into it i think you should be able to draw on it.
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|
Thanks for the help, Code-o-mat! I can now generate bitmaps, but they're coming out incorrectly.
Short version: I created an 8bpp bitmap, then I BitBlt it to the screen and save it into a file. The copy on the screen has color, which suggests it's not just 8bpp. The version saved to the file is 8bpp, but it's totally black, where it should have at least *some* of the drawing.
Question: How can I draw on an 8bpp grayscale bitmap and save it to a file? Can anyone see what I'm doing wrong?
========================================================
The Details
Declarations:
CBitmap bm;
HBITMAP hbm;
BITMAPINFO* bi = (BITMAPINFO *)malloc(sizeof(BITMAPINFO) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER bih;
Constructor:
initBi ();
hbm = CreateDIBSection (hdc, bi, DIB_RGB_COLORS, &bits, NULL, 0);
...
void CEightBitDrawingView::initBi()
{
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
bi->bmiHeader = bih;
for (int i = 0; i < 256; i++)
{
bi->bmiColors [i].rgbRed = i;
bi->bmiColors [i].rgbGreen = i;
bi->bmiColors [i].rgbBlue = i;
}
}
OnDraw:
void CEightBitDrawingView::OnDraw(CDC* pDC)
{
CClientDC dc(this);
CDC *mdc = GetDC ();
HGDIOBJ tmp = mdc->SelectObject(hbm);
mdc->FloodFill (0, 0, RGB (255, 255, 255));
CBrush* br = new CBrush (RGB (255, 0, 255));
CRect rect (30, 60, 130, 160);
mdc->FillRect (&rect, br);
mdc->TextOutW (50, 50, L"Testing...");
CImage image;
image.Attach (hbm);
HRESULT hr = image.Save(_T("C:\\test.tif"), Gdiplus::ImageFormatTIFF);
GetClientRect(&rect);
dc.BitBlt(0,0,rect.right,rect.bottom, mdc, 0,0,SRCCOPY);
mdc->SelectObject (tmp);
delete br;
}
|
|
|
|
|
Am not sure where you are going wrong, i did a quick proggie to check and it produced a correct bitmap:
BITMAPINFO *bmi = (BITMAPINFO *)malloc(sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256);
BITMAPINFOHEADER &bih(bmi->bmiHeader);
bih.biSize = sizeof (BITMAPINFOHEADER);
bih.biWidth = 200;
bih.biHeight = -200;
bih.biPlanes = 1;
bih.biBitCount = 8;
bih.biCompression = BI_RGB;
bih.biSizeImage = 0;
bih.biXPelsPerMeter = 14173;
bih.biYPelsPerMeter = 14173;
bih.biClrUsed = 0;
bih.biClrImportant = 0;
for (int I = 0; I < 255; I++)
{
bmi->bmiColors[I].rgbBlue = bmi->bmiColors[I].rgbGreen = bmi->bmiColors[I].rgbRed = (BYTE)I;
bmi->bmiColors[I].rgbReserved = 0;
}
void *Pixels;
HBITMAP hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &Pixels, NULL, 0);
BYTE *Pix = (BYTE *)Pixels;
for (int Y = 0; Y < abs(bih.biHeight); Y++)
for (int X = 0; X < bih.biWidth; X++, Pix++)
*Pix = (BYTE)Y;
free(bmi);
CImage Image;
Image.Attach(hbmp);
Image.Save(_T("r:\\test.bmp"), Gdiplus::ImageFormatBMP);
DeleteObject(hbmp);
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> "It doesn't work, fix it" does not qualify as a bug report. <
> Amazing what new features none of the programmers working on the project ever heard of you can learn about when reading what the marketing guys wrote about it. <
|
|
|
|
|