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
Solved

Application Handle

Posted on 2001-06-09
14
347 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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 
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 200 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Add values of each row in an array 3 64
FMX TCameraComponent Problem 2 85
Why isn't object file created? 6 69
Precision Problem in C++ 7 44
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

829 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