Today is the second time I published an article. It's rare. No way, my eyes hurt, but I still stick to the spirit of "Working Hard For Me, serving thousands of families, I will continue to write my article. Lecture ing...
//--------------------------------------------------------------------//
The last time I wrote a standard C ++ program to display the current path of the program, but what if it is a Windows application? There is no need to worry about this because many functions are available in windows.
First, we use the getcommandline () function. This function is selected because it is very simple and does not require parameters. The declaration of this function is:
# Define getcommandline getcommandlinea
Winbaseapi _ out lpstr winapi getcommandlinea (void );
Edit a small program here. We can see that you only need to call this function to see the path of the current program.
Code:
- # Include <windows. h>
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- MessageBox (null, getcommandline (), "program message", mb_ OK );
- Return 0;
- }
- Example 1
Running result
But there is another problem. There are some problems with calling this function, that is, in addition to including the name of the current program, this function also has a pair of double quotes. What should I do? In the first lecture, we have written a path that can be intercepted to remove the name. How can we remove double quotation marks? Very simple. We know that the following double quotation marks are no longer available when they are intercepted. We only need to process the preceding double quotation marks. If you only display the path, you can write it like this.
Code:
- # Include <windows. h>
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- Char * P1, * P2;
- Int I;
- P1 = P2 = (char *) getcommandline () + 1; // starts when double quotation marks are removed.
- For (; * P2! = 0; P2 ++)
- If (* P2 = '//')
- I = P2-P1; // mark the last "/"
- P1 [I] = 0; // set the original "/" to 0
- MessageBox (null, P1, "program message", mb_ OK );
- Return 0;
- }
- Example 2
Running result
It's easy. In fact, this method is the most efficient, and can be solved without any strlen (), strcpy (), strcat.
Next I will introduce another function getcurrentdirectory (). This function has two parameters and its prototype is:
# Define getcurrentdirectory getcurrentdirectoryw
Winbaseapi DWORD winapi getcurrentdirectoryw (_ in DWORD nbufferlength, _ out_ecount_part_opt (nbufferlength, return + 1) lpwstr lpbuffer );
This function has two parameters. The first parameter is the buffer length, and the second parameter is the string installed in the current path. Such a function needs to be stored with a string, so it is a little more complicated than the previous one. However, it is easy to use without double quotation marks. See the following program:
Code:
- # Include <windows. h>
- # Define max_buffer_size 255
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- Char STR [max_buffer_size];
- Getcurrentdirectory (max_buffer_size, STR );
- MessageBox (null, STR, "program message", mb_ OK );
- Return 0;
- }
- Example 3
Its running result
To remove the program name, you can intercept the STR that has been assigned a value. The modified program is as follows:
Code:
- # Include <windows. h>
- # Define max_buffer_size 255
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- Char STR [max_buffer_size];
- Char * P = STR;
- Int I;
- Getcurrentdirectory (max_buffer_size, STR );
- For (; * P! = 0; P ++)
- If (* P = '//')
- I = p-STR;
- STR [I] = 0;
- MessageBox (null, STR, "program message", mb_ OK );
- Return 0;
- }
- Example 4
Its running result is as follows:
It's easy. Note that max_buffer_size must be defined to be large enough. If max_buffer_size is too small, the path display is incomplete and other unexpected problems will occur.
The function we will introduce is getmodulefilename (), which has three parameters. Its prototype is as follows:
# Define getmodulefilename getmodulefilenamew
Winbaseapi DWORD winapi getmodulefilenamew (_ in_opt hmodule, _ out_ecount_part (nsize, return + 1) lpwch lpfilename, _ in DWORD nsize );
The first parameter indicates the handle that loads a program instance. If this parameter is null, the function returns the full path of the current application. The second parameter represents the pointer to the memory block that stores the returned name, which is an output parameter, and the third parameter is the maximum value of the buffer, which is the same as the getcurrentdirectory () function. If the buffer is too small, the display is incomplete and other problems also occur.
See this program below:
Code:
- # Include <windows. h>
- # Define max_buffer_size 255
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- Char STR [max_buffer_size];
- Getmodulefilename (null, STR, max_buffer_size );
- MessageBox (null, STR, "program message", mb_ OK );
- Return 0;
- }
- Example 5
Program Results
Similarly, the current path can also be displayed by using the screenshot function. See the following program:
Code:
- # Include <windows. h>
- # Define max_buffer_size 255
- Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
- {
- Char STR [max_buffer_size];
- Getmodulefilename (null, STR, max_buffer_size );
- Char * P = STR;
- Int I;
- Getcurrentdirectory (max_buffer_size, STR );
- For (; * P! = 0; P ++)
- If (* P = '//')
- I = p-STR;
- STR [I] = 0;
- MessageBox (null, STR, "program message", mb_ OK );
- Return 0;
- }
- Example 6
Program Results
Here I also want to say that if the getmodulefilename (null, STR, max_buffer_size) of the fifth line of the above program is changed to getmodulefilename (hinst, STR, max_buffer_size ); in this way, the same result will be obtained. Does that mean that the two are the same? No. If you perform multi-threaded operations in the winmain () function, each thread will have an instance handle. In this way, if you use the getmodulefilename () function, it will get the instance handle path. In this way, it is helpful to know the thread source and delete some Trojans.
Reminder: If "error c2664:" messageboxw "appears ": parameters 2 cannot be converted from "char [255]" to "lpcwstr" (usually vs series ), click the menu> Project> <Project Name> Properties (PIn the dialog box that appears, select Configuration Properties> general in the tree on the left, and then select "use multi-Byte Character Set" in the character set on the right ", in this way, the type conversion can be smoothly performed, so no error is reported.
//---------------------/
Now, the current path is coming to an end. You are welcome to comment on it. Let's share the knowledge of Windows programming! (Conclusion 2)