Two dangerous functions: getcurrentdirectory and getparent

Source: Internet
Author: User

Recently, I used VC as a client.ProgramFor a long time, I did not do this kind of program, and it was very unfamiliar. Because of the second-hand projects of others who took over, most of themCodeAll of them have been completed, but they are very difficult to run. Many unexpected and unexpected problems make people love and hate. I encountered some strange problems caused by two functions. I analyzed the cause and found out some common errors that caused some function errors. Let's talk about these two functions.

1. getcurrentdirectory

 
DWORD winapi getcurrentdirectory (_ in DWORD nbufferlength, _ out lptstr lpbuffer );

This is a function prototype. It mainly serves to obtain the current directory of the program, most of which refers to the running directory of the current running program. I remember that this part is about most cases, not all. In some special cases, other paths will be returned. Our program needs to be guided by a monitoring program during operation. When I wrote the monitoring program and let him guide into the main program, I found a strange problem, and the entire interface of the program was disrupted, none of the images exist. Search for images in the directory and run the main program directly. We have no clue about tracking in the main program, and every function runs unexpectedly, and there is nothing wrong with it. However, exceptions occur under the guidance of the monitoring program. It is determined that the image cannot be found due to a problem in the path sending surface. We checked the function to obtain the path and found that getcurrentdirectory was used. This function to obtain the current directory should be normal, I think the shell we use in the monitor program calls the main program. It should be caused by forgetting to set the working directory. Therefore, it is still fruitless to modify the monitor program and set the working directory, special debugging found that the path obtained by this function is the location of the monitoring program. It seems that the getcurrentdirectory function itself should be faulty and we use the error. Later, I checked some information and Microsoft admitted that this function is sometimes not so appropriate. The value obtained by this function has a lot of impact, so if you only want to get the current program running path, we recommend that you use the getmodulefilename function for processing. This is accurate because it is the storage path of the currently running program. Our program also uses this method and then returns to normal.

2. getparent

There is no problem with the use of this function, but it is a problem that we sometimes need to pin a window when developing a desktop application, there are several setforegroud methods to solve, but these problems are subject to the fact that if your parent window is not straight forward, you do not want to be at the top, so setparent (null) is usually used) function to remove the parent window. After the parent window is cleared, the getparent function is deprecated and the obtained value is null. In most cases, you need to obtain the handle of the parent class on the interface for operating the parent class in VC, and then obtain the values of some controls in the parent class, for example, the Child Window obtains the currently selected value of the parent class list. When the window is not worth the money, it is okay to use getparent. Later, in order to get setparent (null) before the window is made, it is difficult to compile it. During the runtime, the system will tell you that the handle is invalid, however, it is difficult to find bugs when they fail. We recommend that you set a variable containing the parent class handle in the base class by default.

The above problems are some bad programming habits or questions about function creation at will, after summing up the above problems, before I start a large C ++ project, we must do a good job of preliminary design, basic framework, and use several good design modes, centralized management of resources and interfaces is prone to leakage. After the project is completed, summarize the things that must be included in the basic framework of a large C ++ project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.