?
Solved

Application Handle

Posted on 2001-06-09
14
Medium Priority
?
350 Views
Last Modified: 2013-11-25
In Visual Basic, if I want the handle of my app, I can just use the "Me" term to indicate the program I'm running. Can anyone show me how to get my application's handle, thread and process with Microsoft Visual C++ 6?
0
Comment
Question by:microfleet
[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
  • 6
  • 4
  • 2
  • +1
14 Comments
 
LVL 5

Expert Comment

by:robpitt
ID: 6172260
You should take a look at...
  GetCurrentProcess()
  GetCurrentThread()
  GetCurrentProcessId()
  GetCurrentThreadId()
  GetModuleFileName(NULL,...)
  GetModuleHandle(NULL)

That any help?

Rob
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6172264

Perhaps GetCurrentProcess() is what you mean?

http://msdn.microsoft.com/library/psdk/winbase/prothred_58kz.htm
0
 

Author Comment

by:microfleet
ID: 6172500
Well, I've tried the GetCurrentProcess() and I looked at GetWindow(GW_OWNER) but when I make a simple test by just trying to maximize the window by using the Process handle, nothing happens. Well, sometimes it crashes. Could you give me an example of using one of those 'GET' functions to retrieve the handle?
0
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
LVL 5

Expert Comment

by:djbusychild
ID: 6172533
how are you using this exactly? what are you passing into GetWindow ? did you check the return result?
0
 

Author Comment

by:microfleet
ID: 6172615
HANDLE hProcess = GetCurrentProcess();
HANDLE hWND = GetWindow(GW_OWNER);

I tried both those methods. The first returns a number that when used to try to maximize the app, causes the program to crash. The second returns 0.


0
 

Author Comment

by:microfleet
ID: 6172664
HANDLE hProcess = GetCurrentProcess();
HANDLE hWND = GetWindow(GW_OWNER);

I tried both those methods. The first returns a number that when used to try to maximize the app, causes the program to crash. The second returns 0.


0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6172699
if you're the top-level window you're going to get NULL since your "OWNER" window is the desktop.

what is returned from GetCurrentProcess is not the same thing as your window handle.
0
 
LVL 5

Expert Comment

by:djbusychild
ID: 6172708
what are you trying to do? If you're using an encapsulated window class like the ones provided in WTL or MFC, you always have a handle to the actual window that you can refer to... if you're responding to an event, the handle gets passed in.
0
 
LVL 5

Expert Comment

by:robpitt
ID: 6172990
We need more information. You haven't really told us what your doing or trying to do or what methods your employing.

Is this a plain Win32 API application or is it encapsulated via for example MFC or WTL?
What have you allready written?
And what are you now trying to add?

Rob
0
 

Author Comment

by:microfleet
ID: 6173348
It's an MFC exe. I basically let the Microsoft C++ wizard create a dialog based application. And now I'm simply trying to learn how to handle the language. As I learn something, I put it in a database I've created, so I'll have it as a reference. At the moment, I have no real need to use the handle but I would still like to know how to retrieve it. Is there not just a simple line of code that allows you to quickly get the handle?

And thanks djbusychild for letting me know what my methods were returning.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 800 total points
ID: 6175046
In MFC, you use AfxGetMainWnd to obtain a CWnd* to the main window of you application program.  For instance,

CWnd* pWnd= AfxGetMainWnd();
pWnd->SendMessage(WM_SYSCOMMAND, SC_MAXIMIZE, 0);

From your question it seems that you may be confused about the difference between a window handle and a process handle.  Do you need help in this area?

-- Dan


0
 

Author Comment

by:microfleet
ID: 6175758
Thanks Dan, you bet I'm confused about a lot of the C++ language. I don't understand many things, like how I can create one MFC app and call the gethostname function and it work fine, then use the same call in another MFC app (with all includes) and it not be able to retrieve the hostname. Or how I can create a CHAR variable with 128 characters in one app, with the initial string being just a few jumbled symbols, and then create the same variable in another app and it initially set the variable with "yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy." If you know of any book that can help me with the basics of the MFC and why things might not work together, I'd appreciate it. Thanks again for the help.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6178295
Thanks for awarding the points.  Now let's see what I can do about your other questions:

>> gethostname function ...in another MFC app...and it not be able to retrieve the hostname

You may have forgotten to call WSAStartup.  In general, when programming in VC++, you are best off using the MFC classes like CSocket since they take care of details like this.  That won't help with gethostbyname...  But another good socket diagnostic is to use the WSAGetLastError when something doesn't work.

>>...create a CHAR variable with 128 characters...in another app and it initially set the variable ith "yyyyy...

Common problems with VB programmers is not realizing the difference between the variable itself and a pointer to the variable (VB has no pointers at all).  But this particular problem is likely related to a class member.  Class members must be initialized.  Just put..

  char szMyVar[128];

in the .h file and and initialize it in th constructor:

CMyClass::CMyClass
{
 ...
   szMyVar[128];
 ...
}

But spend some time learning to use the MFC CString class because it intializes itself and it works much more like VB strings.

>> If you know of any books...

I really don't have any particular recommendations.  But MSDN has tons of tutorial material.  The best teacher is yourself -- use the AppWizard to create lots of little dialog-based programs to test stuff.

-- Dan
0
 

Author Comment

by:microfleet
ID: 6179837
Thanks Dan. I think I'm making MSDN my homepage for awhile.
0

Featured Post

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!

Question has a verified solution.

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

A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

770 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