Running exe, pls help

trabzone
trabzone used Ask the Experts™
on
I write an exe program and it uses thread that has high priority. Say it program1

i want to write another program that runs the program1 above but now its thread becomes low priority and new program takes a bitmap from program1. Bitmap is saved as bitmap1 on that program and shown an panel1.

How can i do that?

Any way to do this?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
hello trabzone, You did not give enough information to get the full idea of what you need, You say put Bitmap on panel? Here is some code that your program2 will use to start your "program1" and use the Command Lines to send information to program1. Program1 will read the start up command lines and send a WM_COPYDATA message to your program2 with the Bitmap bytes in it's Data pointer.


PROGRAM1


procedure TForm1.FormCreate(Sender: TObject);
var
Str1: String;
begin
if UpperCase(ParamStr(1)) = '/N' then
  begin
{if the first ParamStr is N then don't do HIGH PRIORITY Thread}
  Label3.Caption := 'ParamStr(1) was /N';
  SetThreadPriority(hThread, THREAD_PRIORITY_IDLE);
  end else
  SetThreadPriority(hThread, THREAD_PRIORITY_HIGHEST);
if ParamStr(2) <> '' then
  begin
{the second paramStr has the program2 handle}
  Str1 := Copy(ParamStr(2), 2, 16);
  if IsWindow(StrToInt(Str1)) then
  ProgHwnd := StrToInt(Str1) else
    begin
    ProgHwnd := 0;
    Button_SendBmp.Enabled := False;
    end;
  end else Button_SendBmp.Enabled := False;
end;


procedure TForm1.Button_SendBmpClick(Sender: TObject);
var
Bmp1: TBitmap;
MemStream: TMemoryStream;
CopyData1: TCopyDataStruct;
begin
{this is a button click to send the bitmap}
if not IsWindow(ProgHwnd) then Exit;
Bmp1 := TBitmap.Create;
MemStream := TMemoryStream.Create;
try
  Bmp1.Canvas.Brush.Color := clRed;
  Bmp1.Height := 100;
  Bmp1.Width := 130;
{you can use any bitmap even load form file}
  Bmp1.SaveToStream(MemStream);
{get your bitmap data into a memory stream}
  CopyData1.cbData := MemStream.Size;
  GetMem(CopyData1.lpData, CopyData1.cbData);
  try
    MemStream.Position := 0;
    MemStream.Read(CopyData1.lpData^, CopyData1.cbData);
    if SendMessage(ProgHwnd, WM_COPYDATA,3,Integer(@CopyData1)) = 17 then
      Label2.Caption := 'Bitmap was recieved' else
      Label2.Caption := 'ERROR, Bitmap was not recieved';
    finally
    FreeMem(CopyData1.lpData);
    end;
  finally
  Bmp1.Free;
  MemStream.Free;
  end;
end;


- - - - - - - - - - - - - - -  - - - -
PROGRAM2

  private
    { Private declarations }
    procedure GetCopyData(var Msg: TWmCopyData); message WM_COPYDATA;



procedure TForm2.button_RunAppClick(Sender: TObject);
var
CmdLine2: String;
begin
{you need to start Program1 with the command lines of /N and the Handle of the app}
CmdLine2 := '/N /'+IntToStr(Handle);
ShellExecute(Handle, 'open', 'C:\Program Files\Program1.exe', PChar(CmdLine2), nil, SW_SHOWNORMAL);
end;


procedure TForm1.GetCopyData(var Msg: TWmCopyData);
var
Bmp1: TBitmap;
MemStream: TMemoryStream;
Str1: String;
begin
Msg.Result := 0;
if Msg.From = 3 then
  begin
  Bmp1 := TBitmap.Create;
  MemStream:= TMemoryStream.Create;
  try
    MemStream.Write(Msg.CopyDataStruct.lpData^, Msg.CopyDataStruct.cbData);
    MemStream.Position := 0;
    Bmp1.LoadFromStream(MemStream);
    if not Bmp1.Empty then
      begin
      Msg.Result := 17;
      Label1.Caption := 'ERROR bitmap is empty';
      end;
    Bmp1.SaveToFile('C:\Stuff\Bitmap1.bmp');
    PaintBox1.Canvas.Draw(1,1, Bmp1);
{you can put this PaintBox on your panel, or use a TImage on a panel}
    finally
    Bmp1.Free;
    MemStream.Free;
    end;
  end;
end;


- - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
this works for me, ask questions if you need more information

Author

Commented:
i'm too sorry
i was in a hurry when i wrote this question

thanx for your answer Slick812

program1 can be any program. I write program1 but i want to write new program that is independent from program1
so that i dont want to change program1's source codes

only changing with program2's source code, how can i take a bitmap from program1's interface
also i want to change program1's main threads priority using program2. I know it can. But how!

Run program1 with clciking a button on program2 (also changing priority) then take the bitmap.
The is the problem that i cannot solve...

Thanx..
I wish you would have given more info before, , , ,


procedure TForm1.sbut_RunAppClick(Sender: TObject);
var
CmdLine: String;
startUpInfo: TStartupInfo;
processInfo: TProcessInformation;
wHandle: Integer;
WinDC: HDC;
Rect1: TRect;
Bmp1: TBitmap;
begin
FillChar(startUpInfo, SizeOf(startUpInfo), Chr(0));
StartUpInfo.cb := SizeOf(StartUpInfo);
StartUpInfo.dwFlags     := STARTF_USESHOWWINDOW;
StartUpInfo.wShowWindow := SW_SHOWNORMAL;

CmdLine := 'C:\Program Files\Program1.exe';
CreateProcess(nil, PChar(CmdLine), nil, nil, false,
       IDLE_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInfo);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
Sleep(120);
wHandle := GetForegroundWindow;
windows.GetClientRect(wHandle, Rect1);
if (Rect1.Right > 20) and (Rect1.Bottom > 20) then
  begin
  Bmp1 := TBitmap.Create;
  WinDC := GetDC(wHandle);
  try
    Bmp1.Width := Rect1.Right;
    Bmp1.Height := Rect1.Bottom;
    BitBlt(Bmp1.canvas.Handle,0,0, Rect1.Right, Rect1.Bottom, WinDC, 0, 0, SRCCOPY);
    PaintBox1.Canvas.Draw(0,0, Bmp1);
    Bmp1.SaveToFile('C:\Folder\Bmp1.bmp');
    finally
    Bmp1.Free;
    ReleaseDC(wHandle, winDC);
    end;
  end;
end;
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Author

Commented:
thanx for your answer

but that is not the answer exactly
priority part is working properly but i cannot take the bitmap.
Also i want to take bitmap directly from program1.
is there any way to do this?
access the program1 directly
take the bitmap

i try and make some changes on the procedure that you write. That is usign handles and hprocess can i take the bitmap directly?
But i cannot make it but still continue my operation
does anyone knows how can i do?
I gave you the code to get whatever was on the Interface of Program1. and I have trouble understanding your question -
"Also i want to take bitmap directly from program1.
is there any way to do this?
access the program1 directly
take the bitmap"

whats in program1 - bitmaps and other things - are not easy to get to from another program, it's an effort sometimes to get to things in your own program. Bitmaps are system objects, but you need a "Bitmap Handle" to access the bitmap. Unless you make program1 to give you the handle of the bitmap, I do not know a way to scan a program (look through) for a created bitmap. It is posible to get things - bitmaps, icons - from a programs resources, but that seems difficult to me.

Author

Commented:
Thanx for your answer, slick812

I'm so sorry for my english,

bitmap in program1 changes for example it connects to a scanner
there is a button and panel1
when you click a button then it scans. This part is program1.

Now i want to make program2 that runs program1 and takes the scanned bitmap from program1. Thats all. But using handles of program1.

Thereis a way to do this. But how? I want to know this!
I do not have any way to get a bitmap handle from another program, unless your program1 sends the handle to program2, or makes it availible to program2, by a memory mapped file or something, I don't know how. One program can not access the memory space of another program in windows. How would you know what bitmap it was, if you could scan another program for a bitmap? Have you ever seen any program anywhere that can do this? I can't help you with this. It does not look like anyone else can either, since I'm the only one to comment here. Maybe you should delete this question, and ask this question again, giving more details of what you need.

Author

Commented:
ok thanx
some programs can change the text of edit box or click a button or take the bitmap of controls of the form or smt like that
then i want to know to take the bitmap of another program
and i ask this question
yes you can get "windows system" objects, like text in another programs edit box, and you can click another programs buttons, and I showed you how to get a bitmap of another programs Interface (window, form). But you say "take the bitmap of another program", if this bitmap is displayed on it's form (window) then you can get it by getting a bitmap of that programs form, but if it is not displayed (shown), then how would you know if it is there or not, and there may be more than one bitmap, how could you tell one bitmap from another?

Author

Commented:
ok then how can i click on a windows form?
or how can i change a text on another windows edit box?
if i learn it maybe i can do what i want
Ther is very much to explain to someone for knowing how to use the windows messages and the functions for using these messages, but here is some code that will use FindWindow, FindWindowEx and sendMessage to do a couple of things with the windows standard Calculator program. Calculator needs to be running for this to work.


procedure TForm1.sbut_CalButtonClick(Sender: TObject);
var
CalWnd, hCButton1: THandle;
Buf: Array[0..1024] of Char;
begin
CalWnd := FindWindow(nil, 'Calculator');
if CalWnd = 0 then
begin
ShowMessage('Calculator is not running');
Exit;
end;

hCButton1 := FindWindowEx(CalWnd,0,'BUTTON',nil);
// this gets the first button
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
// I have to go through the buttons, until button number 4
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
// this is button 4
SendMessage(hCButton1, WM_LBUTTONDOWN, 5, 5);
SendMessage(hCButton1, WM_LBUTTONUP, 5, 5);
// if you send the lbutton down and l button up
// message then the lbutton will click
hCButton1 := FindWindowEx(CalWnd,hCButton1,'BUTTON',nil);
// this is button 1
SendMessage(hCButton1, WM_LBUTTONDOWN, 5, 5);
SendMessage(hCButton1, WM_LBUTTONUP, 5, 5);
GetWindowText(CalWnd, @Buf, 1023);
{GetWindowText will get the Name of this window which will be Calculator}
ShowMessage('this is the button handle '+IntToStr(hCButton1)+' '+Buf);
end;



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

there are so many messages and message function, I can not begin to show then to you. .  . if you want to know more how the windows system operates (for programming) with windows messages, then you might look at

http://www.angelfire.com/hi5/delphizeus/moremess.html

or at the whole site

http://www.angelfire.com/hi5/delphizeus/index.html

Author

Commented:
thanx for all answers

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial