[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Find Instance

Posted on 2002-05-15
8
Medium Priority
?
605 Views
Last Modified: 2010-04-04
Hi, everyone

I wanna find the instance of an exe.

But I only had is handle, I don't know what exe is and  where it is

And What is the difference between a handle and an instance??
0
Comment
Question by:DelphiCool
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7011210
>I don't know what exe is
its a executable file, just a program like notepad,
just an application

>and where it is
normally on the filesystem, if no instance from it is started

>what is an instance
if you execute an executeable file, then you have an instance of this application started, if you execute the executeable file moreoften without finishing an instance, then you've started some instaces from the same executable file, like
if you start notepad.exe twice, then you've two instances of the notepad.exe running on your system.

>what is a handle
a handle is an id to a os-object, there may various handles
(windowhandle, processhandle,..)

well to go back to your question
>I wanna find the instance of an exe

tell us what kind of handle you have

meikl ;-)


0
 

Author Comment

by:DelphiCool
ID: 7011236
Thanks kretzschmar

In fact, I enumerate all the handle of the running application.

I have alle their handle.
But I wanna have their instance.

cause the api GetModuleFileName work only with the instance

Thanks
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7011865
well, guessing you've a windowhandle

then look at the windows api GetWindowThreadProcessId

from the helpfile

The GetWindowThreadProcessId function retrieves the identifier of the thread that created the specified window and, optionally, the identifier of the process that created the window. This function supersedes the GetWindowTask function.

DWORD GetWindowThreadProcessId(

    HWND hWnd,     // handle of window
    LPDWORD lpdwProcessId      // address of variable for process identifier
   );    
 

Parameters

hWnd

Identifies the window.

lpdwProcessId

Points to a 32-bit value that receives the process identifier. If this parameter is not NULL, GetWindowThreadProcessId copies the identifier of the process to the 32-bit value; otherwise, it does not.

 

Return Values

The return value is the identifier of the thread that created the window.

--- end paste

in delphi it should work like
(not tested myself)

var
 ProcessHandle : THandle;
begin
 if YourHandle <> 0 then
   GetWindowThreadProcessId(YourHandle, @ProcessHandle);
 //now work with the ProcessHandle
end;

hope this helps

meikl ;-)


   
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:DelphiCool
ID: 7012066
thinks the solution is not too far from your post,
but it doesn't work.
Look try this code, you'll understand:


// NORMAL Method
procedure TMainForm.Button5Click(Sender: TObject);
 var p :array[0..255]of char;
  begin
    GetModuleFileName(hinstance,p, 255);
    messageboxa(handle, p, '', 0);
  end;


// METHOD with GetWindowThreadProcessId
procedure TMainForm.Button6Click(Sender: TObject);
 var p :array[0..255]of char;
     inst :thandle;
  begin
    GetWindowThreadProcessId(handle, @inst);
    GetModuleFileName(inst,p, 255);
    messageboxa(handle, p, '', 0);
  end;


I try on the handle of my application.
GetWindowThreadProcessId didn't return me the instance
cause the result of GetWindowThreadProcessId and the veritable instance are not the same ??
0
 
LVL 12

Accepted Solution

by:
Lee_Nover earned 240 total points
ID: 7012619
so you want to get a windows executable ?
if so then use:

uses windows, psapi;

function IsWinNT4Plus: Boolean;
var VI: TOSVersionInfo;
begin
     FillChar(VI, SizeOf(TOSVersionInfo), 0);
     VI.dwOSVersionInfoSize:=SizeOf(TOSVersionInfo);
     GetVersionEx(VI);
     with VI do
       Result:=(dwPlatformId = VER_PLATFORM_WIN32_NT)and(dwMajorVersion >= 4);
end;

function GetWindowFileName(const hWin: HWND): string;
var lpBuff: PChar;
    nCount: Integer;
    dwProcessId: Cardinal;
    hInst: Cardinal;
    hProcess: Cardinal;
begin
     Result:='';
     nCount:=1024;
     GetMem(lpBuff, nCount);
     try
        if IsNT4Plus then begin
           hInst:=GetWindowLong(hWin, GWL_HINSTANCE);
           GetWindowThreadProcessId(hWin, dwProcessId);
           hProcess:=OpenProcess(PROCESS_ALL_ACCESS, false, dwProcessId);
           if hProcess > 0 then
              try
                 GetModuleFileNameEx(hProcess, hInst, lpBuff, nCount);
              finally
                 CloseHandle(hProcess);
              end;
        end else begin
           GetWindowModuleFileName(hWin, lpBuff, nCount);
        end;
        Result:=string(lpBuff);
     finally
        FreeMem(lpBuff);
     end;
end;
0
 
LVL 17

Expert Comment

by:inthe
ID: 7012672
//What is the difference between a handle and an instance??

an instance is a handle,
a instance is a module handle of the exe/dll

for example take your example and modify :

var
 p :array[0..255]of char;
 begin
showmessage(inttostr(hinstance));

 GetModuleFileName(hinstance,p, 255);
showmessage(inttostr(GetModuleHandle(p)));


so if you want to have the "instance" of an exe then call GetModuleHandle().

how are you getting the window handle of the exe to begin with? ie are you using enumwindows etc?

0
 

Author Comment

by:DelphiCool
ID: 7013018
Thank you Lee_Nover, you've got it.
it's exactly what I was lookiing for.

Sorry for the other who tryed to help me
0
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7013116
something happens as i sleeped ;-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question