Solved

Getting Programname.

Posted on 2003-11-10
8
291 Views
Last Modified: 2010-04-05
Hey, I use a hook to log the keyboard. This function send the keys to my app. Is there anyway that it also sends the application name, the program where the key is pressed.

This is the function.

function KeyHookFunc(Code, VirtualKey, KeyStroke: Integer): LRESULT; stdcall;
var
  KeyState1: TKeyBoardState;
  AryChar: array[0..1] of Char;
  Count: Integer;
begin
  Result := 0;
  if Code = HC_NOREMOVE then Exit;
  Result := CallNextHookEx(hKeyHook, Code, VirtualKey, KeyStroke);
  if Code < 0 then
    Exit;
  if Code = HC_ACTION then
  begin
    if ((KeyStroke and (1 shl 30)) <> 0) then
        hMemFile  := OpenFileMapping(FILE_MAP_WRITE, False, 'Global7v9k');
        PHookRec1 := MapViewOfFile(hMemFile, FILE_MAP_WRITE, 0, 0, 0);
        if PHookRec1 <> nil then
        begin
          hApp  := PHookRec1.AppHnd;
        end;
    if ((KeyStroke and (1 shl 30)) <> 0) then
    begin
      GetKeyboardState(KeyState1);
      Count := ToAscii(VirtualKey, KeyStroke, KeyState1, AryChar, 0);
      if Count = 1 then
      begin
        PostMessage(hApp, WM_USER + 1678, Ord(AryChar[0]), 0);
      end;
    end;
  end;
end;

Thanks.
0
Comment
Question by:JuicyJJ
[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
  • 4
  • 3
8 Comments
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9716894
DWORD GetModuleFileName(
  HMODULE hModule,    // handle to module
  LPTSTR lpFilename,  // path buffer
  DWORD nSize         // size of buffer
);
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9716929
Or try something like that:

var hSnapshoot: THandle;
    pe32: TProcessEntry32;
    the32: THREADENTRY32;
    dw_pid: DWORD;

    dw_pid := GetCurrentProcessId;
    pe32.dwSize := SizeOf(TProcessEntry32);
    if (Process32First(hSnapshoot, pe32)) then
    repeat
      if dw_pid = pe32.th32ProcessID then begin
        ShowMessage('Process filename: ' + PChar(pe32.szExeFile));
        break;
      end;
    until not Process32Next(hSnapshoot, pe32);

    CloseHandle (hSnapshoot);
0
 
LVL 6

Accepted Solution

by:
DaFox earned 125 total points
ID: 9716936
Hi,

you could either get the title of the application placed in the caption bar or get the filename of the application.

1. window title
GetWindowText({...})

2. filename
var
  ModuleHandle : THandle;
  FileName : PChar;
begin
  FileName := StrAlloc(255);
  ModuleHandle := GetClassLong(WindowHandle, GCL_HMODULE);
  GetModuleFileName(ModuleHandle, FileName, 255);
  // FileName should contain the executable or dll name and path
  StrDispose(FileName);
end;

Markus
0
Independent Software Vendors: 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!

 

Author Comment

by:JuicyJJ
ID: 9716955
Hey thanks.
I guess GetModuleFileName is something as GetClassName, but I don't know what params to use, what the handle is.

With that other code you posted, in what unit are tprocessentry32 and threadentry32 located.

Thanks.
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9717085
>With that other code you posted, in what unit are tprocessentry32 and threadentry32 located.
TlHelp32
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9717086
Hi,

>> but I don't know what params to use, what the handle is.

Try GetActiveWindow().

Markus
0
 
LVL 6

Expert Comment

by:GloomyFriar
ID: 9717228
:(
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9717456
PS: My solution above gets the application filename, because there's no way to get the application name of any windows program a user may run on his machine.
This snippet below tries to get the application name (!) stored in the version info of the exe. Please not that this is not 100% accurate, e.g. the "product name" of notepad is "operating system windows xxx"... So, it may not fit your needs, just wanted to add this...

Here's the code:

procedure TForm1.Button1Click(Sender: TObject);
var
  aFileName: array [0..MAX_PATH] of Char;
  pdwHandle: DWORD;
  nInfoSize: DWORD;
  pFileInfo: Pointer;
  pVarFInfo: PChar;
  nVarFInfo: DWORD;
  nVarTrans: DWORD;
  aVarFPath: array [0..MAX_PATH] of Char;
begin
  if not OpenDialog1.Execute then exit;
  StrCopy(aFileName, PChar(OpenDialog1.FileName));
  // in your hook proc you should use this next line
  // GetModuleFileName(GetActiveWindow, aFileName, MAX_PATH);
  pdwHandle := 0;
  nInfoSize := GetFileVersionInfoSize(aFileName, pdwHandle);
  if nInfoSize <> 0 then pFileInfo := GetMemory(nInfoSize) else pFileInfo := nil;
  if Assigned(pFileInfo) then
  try
    if GetFileVersionInfo(aFileName, pdwHandle, nInfoSize, pFileInfo) then
    begin
      pVarFInfo := nil;
      nVarFInfo := 0;
      if VerQueryValue(pFileInfo, '\VarFileInfo\Translation', Pointer(pVarFInfo), nVarFInfo) then
      begin
        nVarTrans := HiWord(PDWORD(pVarFInfo)^) or (Word(PDWORD(pVarFInfo)^) shl 16);
        wvsprintf(aVarFPath, '\StringFileInfo\%8.8x\', PChar(@nVarTrans));
        // (CompanyName, FileDescription, FileVersion, InternalName,
        //  LegalCopyright, OriginalFilename ProductName, ProductVersion)
        lstrcat(aVarFPath, 'ProductName');
        pVarFInfo := nil;
        nVarFInfo := 0;
        if VerQueryValue(pFileInfo, aVarFPath, Pointer(pVarFInfo), nVarFInfo) then
        begin
          Label2.Caption := 'Dateiversion (var): ' + TCaption(pVarFInfo);
        end;
      end;
    end;
  finally
    FreeMemory(pFileInfo);
  end;
end;

Regards,
Markus
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

707 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