Solved

Application Handle

Posted on 2001-06-09
14
342 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
 
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

As more and more people are shifting to the latest .Net frameworks, the windows presentation framework is gaining importance by the day. Many people are now turning to WPF controls to provide a rich user experience. I have been using WPF controls fo…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now