?
Solved

Application Handle

Posted on 2001-06-09
14
Medium Priority
?
354 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
  • 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
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!

 
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

864 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