Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What i do is ...

HWND h = NULL; // Handle of process
h = ::GetTopWindow(0 );
while ( h )
{
	DWORD pid;
	DWORD dwTheardId = ::GetWindowThreadProcessId( h,&pid);
	if ( pid == pe32.th32ProcessID )//pe32.th32ProcessID is Process id of my currently running process
	{
		LPWSTR lpClassName = _T("");
		int i = GetClassName(h,lpClassName,MAX_PATH);
		if(i==0)
		{
		DWORD dw = GetLastError(); // Error Message:Invalid access to memory location.
		LPCTSTR lpError = ErrorMessageLog(dw,_T("GetClassName"));
		}
		pi.wstrClassName.assign(lpClassName);
		break;
	}
	h = ::GetNextWindow( h , GW_HWNDNEXT);
}


Is there another way to get the handle of class ??
Posted

How about GetCurrentProcess()?

Good luck!
 
Share this answer
 
Comments
ShilpiP 22-Dec-10 0:21am    
Please read the question again.I want HWND not HANDLE. I Mention it in my question.
E.F. Nijboer 22-Dec-10 5:23am    
But what is it what you want? Because the currently running process is your own process. At the moment your code executes it is obviously running. I think your question simply isn't all that clear. I guess you want the window that is currently on the foreground and luckely for you there is a function for that to ;-) called GetForegroundWindow:
http://msdn.microsoft.com/en-us/library/ms633505%28v=vs.85%29.aspx
ShilpiP 23-Dec-10 1:55am    
Nops,my requirement is:
1) Get all the process information of Internet Explorer that is currently running. This i can achieve with CreateToolhelp32Snapshot, OpenProcess, Process32First,Process32Next. Using these API I also get the HANDLE of process.
2)Whatever information of iexplore i need is now stored in struct variable. Name of process, Process id, Process parent id. Now i want the Class Name of process using GetClassName. for this i want the HWND not Handle.
In my code i get the HWND but when i call the function GetClassName than it is not succeded and give me error message.
GetClassName failed HRESULT is:998 Error Message:Invalid access to memory location.
This error message comes if HWND what i get is not the handle of iExplore.
Now i think i am clear to you... :)
E.F. Nijboer 23-Dec-10 4:49am    
Ah, ok. In your code above I see:
LPWSTR lpClassName = _T("");
int i = GetClassName(h,lpClassName,MAX_PATH);
But you actually need to allocate the length of lpClassName yourself because this isn't done by the GetClassName function.
http://msdn.microsoft.com/en-us/library/ms633582%28v=vs.85%29.aspx
Other than GetCurrentProcess mentioned above, I sometimes use FindWindow and DllMain. However, that is for Excel application instances.
 
Share this answer
 
it would be easier to use the EnumWindows function to enumerate all top level windows. and yes indeed you have to use an existing buffer to call GetClassName not a pointer.
struct _
{
  unsigned long  pid;
  static int FAR PASCAL wenum(HWND h,long p)
  {
    unsigned long  pid;
    _*  me = (_*)p;
    if(::GetWindowThreadProcessId( h,&pid) && (me->pid==pid))
    {
      TCHAR  c[0x100];
      int    l;
      if(l=::GetClassName(h,c,sizeof(c)/sizeof(c[0])))
      {
        // store the class
      }
    }
    return 1;
  }
} x = { idProcess };
EnumWindows(x.wenum,(long)&x);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900