ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> perl-win32-users
perl-win32-users
Re: Win32 API problem (pack,unpack)
by $Bill Luebkert other posts by this author
Aug 29 2004 2:51AM messages near this date
view in the new Beta List Site
(no subject) | Win32 API problem (pack,unpack)
Jeremy A wrote:
>  Hi all,
>  
>  I have some more  Win32 API problem's. it could be something to do with the 
>  way I am packing/unpacking.
>  
>  
>  the Following documentation, I found on MSDN.
>  ------------------------------------------------------------------------------------------
----------------------------------
>  GetModuleFileName
>  
>  The GetModuleFileName function retrieves the fully-qualified path for the 
>  file containing the specified module.
>  
>  To specify the process that contains the module, use the 
>  GetModuleFileNameEx function.
>  
>  
>  DWORD GetModuleFileName(
>     HMODULE hModule,
>     LPTSTR lpFilename,
>     DWORD nSize
>  );
>  
>  Parameters
>  hModule
>  [in] Handle to the module whose path is being requested. If this parameter 
>  is NULL, GetModuleFileName retrieves the path of the executable file of the 
>  current process.
>  lpFilename
>  [out] Pointer to a buffer that receives a null-terminated string that 
>  specifies the fully-qualified path of the module. If the length of the path 
>  exceeds the size specified by the nSize parameter, the function succeeds 
>  and the string is truncated to nSize characters and may not be null terminated.
>  To separate the path from the file name and extension, use the 
>  PathRemoveFileSpec function.
>  
>  The path can have the prefix "\\?\", depending on how the module was 
>  loaded. For more information, see Naming a File.
>  
>  nSize
>  [in] Size of the lpFilename buffer, in TCHARs.
>  Return Values
>  If the function succeeds, the return value is the length of the string 
>  copied to the buffer, in TCHARs. If the buffer is too small to hold the 
>  module name, the string is truncated to nSize, and the function returns nSize.
>  
>  If the function fails, the return value is zero. To get extended error 
>  information, call GetLastError.
>  -------------------------------------------------------------------------
>  OpenProcess
>  
>  The OpenProcess function opens an existing process object.
>  
>  
>  HANDLE OpenProcess(
>     DWORD dwDesiredAccess,
>     BOOL bInheritHandle,
>     DWORD dwProcessId
>  );
>  
>  Parameters
>  dwDesiredAccess
>  [in] Access to the process object. This access right is checked against any 
>  security descriptor for the process. This parameter can be one or more of 
>  the process access rights.
>  bInheritHandle
>  [in] If this parameter is TRUE, the handle is inheritable. If the parameter 
>  is FALSE, the handle cannot be inherited.
>  dwProcessId
>  [in] Identifier of the process to open.
>  Return Values
>  If the function succeeds, the return value is an open handle to the 
>  specified process.
>  
>  If the function fails, the return value is NULL. To get extended error 
>  information, call GetLastError.
>  ------------------------------------------------------------------------------------------
-------------------------
>  
>  The following is my perl code, given that I know the Window Handle ($HWND).
>  The script crashes with a message about memory addresses.
>  
>  
>  
>  
>           my $GetWindowThreadProcessId = new Win32::API("user32", 
>  "GetWindowThreadProcessId", [N,P], 'N');
>  
>                   my $P = pack 'L', 0;
>                   my $TID = $GetWindowThreadProcessId->Call($HWND, $P); # or 
>  die "GetWindowThreadProcessId->Call: $!";
>                   $P = unpack 'L', $P;
>  
>                   my $OpenProcess = new Win32::API("kernel32", 
>  "OpenProcess",[N,N,N],'N');
>  
>                   my $PH = $OpenProcess->Call(PROCESS_ALL_ACCESS,0,$P);
>                   print $PH,"\n";
>  
>                   my $GetModuleFileName = new Win32::API("kernel32", 
>  "GetModuleFileName",[N,P,N],'P');
>  
>                   my $FileName = pack 'P','\0';
>                   my $STR = pack 'P','\0';
>                   $FileName = $GetModuleFileName->Call($PH,$STR,80);
>                   $STR = unpack 'P',$STR;
>                   $FileName = unpack 'P',$FileName;
>                   print $FileName,":",$STR,"\n";
>  
>  
>  Thanks in advance for all help,

This works for the caller task (rather than a module handle) :

use strict;
use Win32::API;

# HWND FindWindow(LPCTSTR lpClassName,	LPCTSTR lpWindowName);

my $FindWindow = new Win32::API('USER32', 'FindWindow', 'PP', 'N') or
  die "FindWindow: $!";
my $hWnd = $FindWindow-> Call(0, 0);
print "hWnd=$hWnd\n";

# DWORD GetWindowThreadProcessId(HWND hWnd, LPDWORD lpdwProcessId);

my $GetWindowThreadProcessId = new Win32::API('user32',
  'GetWindowThreadProcessId', 'LP', 'N') or
  die "get GetWindowThreadProcessId: $!";
my $lpdwProcessId = pack 'L', 0;
my $CTID = $GetWindowThreadProcessId-> Call($hWnd, $lpdwProcessId) or
  die "GetWindowThreadProcessId-> Call: $!";
my $dwProcessId = unpack 'L', $lpdwProcessId;
printf "CTID=$CTID; lpdwProcessId=$dwProcessId\n";

# HANDLE OpenProcess(DWORD dwDesiredAccess, BOOL bInheritHandle,
#   DWORD dwProcessId);

my $OpenProcess = new Win32::API('kernel32', 'OpenProcess', 'ILI', 'N') or
  die "get OpenProcess: $!";
use constant SYNCHRONIZE =>  0x00100000;
use constant STANDARD_RIGHTS_REQUIRED =>  0x000F0000;
use constant PROCESS_ALL_ACCESS =>  (STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE |
  0xFFF);
my $dwDesiredAccess = PROCESS_ALL_ACCESS;
my $bInheritHandle = 0;
printf "dwDesiredAccess=%08X\n", $dwDesiredAccess;
my $handle = $OpenProcess-> Call($dwDesiredAccess, $bInheritHandle,
  $dwProcessId);
print "handle='$handle'\n";

# DWORD GetModuleFileName(HMODULE hModule, LPTSTR lpFilename, DWORD nSize);

my $GetModuleFileName = new Win32::API('kernel32', 'GetModuleFileName', 'LPI',
  'N') or die "get GetModuleFileName: $!";
my $nSize = 256;
my $lpFilename = ' ' x $nSize;

# maybe your handle will work on the next line, I oculd only get it to work
# using the default handle (0)

# my $ret = $GetModuleFileName-> Call($handle, $lpFilename, 256) or
my $ret = $GetModuleFileName-> Call(0, $lpFilename, $nSize) or
  die "GetModuleFileName-> Call: $!";
print "ret=$ret\n";
printf "FileName=%s\n", unpack 'A*', $lpFilename;

__END__

Output:
hWnd=1115222
CTID=3020; lpdwProcessId=3348  (3348 is Netscape ???)
dwDesiredAccess=001F0FFF
handle='1980'
ret=20
FileName=F:\perl\bin\perl.exe

-- 
  ,-/-  __      _  _         $Bill Luebkert    Mailto:dbecoll@[...].net
 (_/   /  )    // //       DBE Collectibles    Mailto:dbe@[...].com
  / ) /--<  o // //      Castle of Medieval Myth & Magic http://www.todbe.com/
-/-' /___/_<_</_</_    http://dbecoll.tripod.com/ (My Perl/Lakers stuff)
_______________________________________________
Perl-Win32-Users mailing list
Perl-Win32-Users@[...].com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved