Windows C ++ programming to get the current path to the hacker !!!

Source: Internet
Author: User

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:
  1. # Include <windows. h>
  2. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  3. {
  4. MessageBox (null, getcommandline (), "program message", mb_ OK );
  5. Return 0;
  6. }
  7. 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:
  1. # Include <windows. h>
  2. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  3. {
  4. Char * P1, * P2;
  5. Int I;
  6. P1 = P2 = (char *) getcommandline () + 1; // starts when double quotation marks are removed.
  7. For (; * P2! = 0; P2 ++)
  8. If (* P2 = '//')
  9. I = P2-P1; // mark the last "/"
  10. P1 [I] = 0; // set the original "/" to 0
  11. MessageBox (null, P1, "program message", mb_ OK );
  12. Return 0;
  13. }
  14. 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:
  1. # Include <windows. h>
  2. # Define max_buffer_size 255
  3. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  4. {
  5. Char STR [max_buffer_size];
  6. Getcurrentdirectory (max_buffer_size, STR );
  7. MessageBox (null, STR, "program message", mb_ OK );
  8. Return 0;
  9. }
  10. 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:
  1. # Include <windows. h>
  2. # Define max_buffer_size 255
  3. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  4. {
  5. Char STR [max_buffer_size];
  6. Char * P = STR;
  7. Int I;
  8. Getcurrentdirectory (max_buffer_size, STR );
  9. For (; * P! = 0; P ++)
  10. If (* P = '//')
  11. I = p-STR;
  12. STR [I] = 0;
  13. MessageBox (null, STR, "program message", mb_ OK );
  14. Return 0;
  15. }
  16. 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:
  1. # Include <windows. h>
  2. # Define max_buffer_size 255
  3. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  4. {
  5. Char STR [max_buffer_size];
  6. Getmodulefilename (null, STR, max_buffer_size );
  7. MessageBox (null, STR, "program message", mb_ OK );
  8. Return 0;
  9. }
  10. Example 5

Program Results


Similarly, the current path can also be displayed by using the screenshot function. See the following program:
 

Code:
  1. # Include <windows. h>
  2. # Define max_buffer_size 255
  3. Int winapi winmain (hinstance hinst, hinstance prevhinst, lpstr cmd, int show)
  4. {
  5. Char STR [max_buffer_size];
  6. Getmodulefilename (null, STR, max_buffer_size );
  7. Char * P = STR;
  8. Int I;
  9. Getcurrentdirectory (max_buffer_size, STR );
  10. For (; * P! = 0; P ++)
  11. If (* P = '//')
  12. I = p-STR;
  13. STR [I] = 0;
  14. MessageBox (null, STR, "program message", mb_ OK );
  15. Return 0;
  16. }
  17. 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)

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.