VC skills 4

Source: Internet
Author: User

Directly read the specified sector of the hard disk:
(1) Use assembly;
(2) directly use createfile ("//. /physicaldrive0 ",...) open the physical disk device (the physical disk device is numbered from 0, 0 indicates the first hard disk, and 1 indicates the second hard disk ....), in this way, you can use setfilepointer to read anything on the hard disk at will! As for the sector, it is generally 512 bytes for one sector, and you can convert it yourself ~

More here:

Obtain the starting address of the local port:
You can read the BIOS data area. Of course, you can only read the data in the OS that is not in protection mode or in DOS.
Segment address: 00 h

408 H: The base address of the No. 0 parallel printer adapter on the computer,
The baseline address of the first parallel printer adapter on the 40ah computer.
The base address of the parallel printer adapter on the 40ch computer.
The base address of the parallel printer adapter on the 40eh computer. (PS2 type. This value is the extended BIOS data segment address)
Setupapi can obtain information about all devices, including I/O Ports, memory resources, and interrupt numbers.

Change desktop background:
Method 1:
Here, refer;
Method 2:
Systemparametersinfo (spi_set1_wallpaper, 0, "C: // aa.bmp", 1); // The jpgimage cannot be displayed.

How can I enable the SDI program to automatically hide it when it starts:Http:// Temp =. 1871607.
Http:// // This is to maximize
Put pmainframe-> showwindow (sw_hide );

This sentence is added after parsecommandline (partition info);, if (! Before processshellcommand (cmdinfo)

Efficient method to obtain the number of lines in a text file:

# Include <afx. h>
# Include <iostream. h>

Void main ()
Cstring STR = "";
Cfile file ("F: // test.txt", cfile: moderead );
Char * temp = new char;
Memset (temp, 0, file. getlength () + 1 );
File. Read (temp, file. getlength ());
STR = temp;
Int linenumber = Str. Replace ("/R/N ","");
File. Close ();
Delete temp;
Temp = NULL;
Cout <"file total" <linenumber + 1 <"line" <Endl; // do not forget to add 1

Accurate timing:

_ Int64 lfreq;
Bool Bret = queryperformancefrequency (large_integer *) & lfreq );
_ Int64 lstart;
Queryperformancecounter (large_integer *) & lstart );
//... Call your function
_ Int64 lstop;
Queryperformancecounter (large_integer *) & lstop );
_ Int64 lruntime = lstop-lstart;
Double DSEC = lruntime/lfreq;

The dialog box for deleting USB devices is displayed.: Winexec ("C: // windows // system32 // rundll32.exe shell32.dll, control_rundll hotplug. dll", sw_show );
The "Data Link Properties" dialog box is displayed.: Idatasourcelocatorptr: promptnew ();

Cbitmap ====> hbitmap:
Bytes -------------------------------------------------------------------------------------
Cbitmap bitmap;
Hbitmap = (hbitmap) bitmap; // The cbitmap reloads the hbitmap () operator, which can be easily used for forced conversion.

Hbitmap ====> cbitmap:
Cbitmap bitmap;
Bitmap. fromhandle (hbitmap );

Extract the. ICO icon from its cmd.exe file as its own program icon:

Hinstance hinst = loadlibrary ("B .exe ");
Hrsrc HRC = findresource (hinst, (lpcstr) 1, (lpcstr) rt_icon );
Lpvoid lpresource = lockresource (loadresource (hinst, HRC ));

Handle hupdate = beginupdateresource ("a.exe", false );
Updateresource (hupdate, (lpcstr) rt_icon, (lpcstr) 1, 0, lpresource, sizeofresource (hinst, HRC ));
Endupdateresource (hupdate, false );

Closehandle (HRC );
Freelibrary (hinst );

Obtain the parameters of the current screen.It is best to use a functionEnumdisplaysettings(Other methods such as getsystemmetrics (sm_cyfullscreen); this function is powerful and can be used based on the specified device name (the first parameter) because the taskbar is hidden or not) return its display information:

Cstring strcurrentdifferency, strcurrentcolormode; // string used to store the current screen resolution and color digits, respectively.
Lpdevmode gpcurrentmode = new devmode; // used to store the structure pointer currently set on the screen
Enumdisplaysettings(Null, enum_current_settings, gpcurrentmode); // obtain the current resolution size and other information.
Strcurrentdifferency. Format ("Resolution: % d * % d", gpcurrentmode-> dmpelswidth, gpcurrentmode-> dmpelsheight );
Strcurrentcolormode. Format ("current color bits: % d", gpcurrentmode-> dmdisplayfrequency );
Afxmessagebox (strcurrentdifferency );
Afxmessagebox (strcurrentcolormode );
Delete gpcurrentmode;
Gpcurrentmode = NULL;
You can also use the CDC: getdevicecaps function:
Cstring strcurrentdifferency;
CDC * PDC = CDC: fromhandle (: getdc (0); // you can obtain a DC pointer. Here, you can obtain the DC of the desktop.
Strcurrentdifferency. Format ("Resolution: % d * % d", PDC-> getdevicecaps (horzres), PDC-> getdevicecaps (vertres ));
Afxmessagebox (strcurrentdifferency );

Solve the Problem of repeated compilation in this project:Http:// Temp =. 9251825.

Check whether the computer is connected to the network:Internetgetconnectedstateex

Enumeration of software or hardware devices:Imdserviceprovider: enumdevices

Print the control content in the dialog box:Http:// Temp =. 1613275.
Make your dialog support print preview:
There is no printer to try now. Remember it first, and then try again later.

End other processes: Getwindowthreadprocessid and terminateprocess functions, but terminateprocess can be used as little as possible. It can directly end the target process. The target process does not know how it died, and it is too late to save its data results, it is too late to notify you of the calls. DLL file.
There is also a post: Temp =. 9869654.

Use the lockfile function to lock a region where the file is opened.To prevent other processes from accessing the region.

Enable the item label of the clistctrl control to be modified.: Modifystyle (0, lvs_editlabels );

To modify the resources of .exe or other modules, refer to the following functions:


The difference between bool size and bool size:
Bool occupies 1 byte in C ++, while bool is of the int type. The size of the int type depends on the specific environment. So: false/true only occupies 1 byte, while True/false depends on the specific environment.

Cfile: getstatusYou can also obtain the file attributes.

Retrieve all top-level windows on the desktop: Enumdesktopwindows, enumwindowsproc

Display the video captured with DirectShow:
DirectShow ivideowindow can be set
Ivideowindow * pvidwin = NULL;
Pgraph-> QueryInterface (iid_ivideowindow, (void **) & g_pvidwin );
Pvidwin-> put_windowstyle (ws_child | ws_clipsiblings );
Rect GRC;
Getclientrect (hwnd, & GRC );
Pvidwin-> setwindowposition (0, 0, GRC. Right, GRC. Bottom );

Change the cursor chart in the edit box control:
Cbitmap * pbitmap = new cbitmap;
Pbitmap-> loadbitmap (idb_happy_bitmap); // idb_happy_bitmapis A .bmp resource in the project, which is of any size. If the color exceeds 16 colors, the color will be distorted.
M_editctrl.createcaret (pbitmap); // m_editctrl is the control variable of the editing box.
Point point;
Point. x = 0;
Point. Y = 0;
M_editctrl.setcaretpos (point); // Changes the cursor position, in pixels.
M_editctrl.showcaret ();

Use theme functions to change the visual style of the dialog box window (more functions are available on msdn ):
# Include
# Pragma comment (Lib, "uxtheme. lib ")
Void cmydlg: ontest ()
Enablethemedialogtexture (m_hwnd, etdt_enable | etdt_usetabtexture );
Invalidate ();

String ====> Date and Time:
Coledatetime DT;
DT. parsedatetime (_ T ("8:00:00 "));
Systemtime ST = {0 };
DT. getassystemtime (ST );
// Setlocaltime (& St); // set the time in the computer

Date and Time ===> string:
Systemtime ST = {0 };
St. wyear = 2006;
St. wmonth = 3;
St. wday = 23;
St. whour = 8;
St. wminute = 30;
St. wsecond = 59;
Coledatetime DT (ST );
Afxmessagebox (Dt. Format ());

The typeid function can determine the type of an object., See msdn, the above sample code, but a little small problem: Url =/library/en-US/vclang/html/_ pluslang_typeid_operator.asp

System sleep, From: Temp =. 8914301.
Bytes ------------------------------------------------------------------------------------------------
If (MessageBox ("do you want to sleep? "," Shutdown program ", mb_yesno | mb_defbutton2 | mb_iconquestion) = idyes)
Static handle htoken;
Static token_privileges TP;
Static luid;
If (: openprocesstoken (getcurrentprocess (), token_adjust_privileges | token_query, & htoken ))
: Lookupprivilegevalue (null, se_shutdown_name, & luid );
TP. privilegecount = 1;
TP. Privileges [0]. luid = luid;
TP. Privileges [0]. Attributes = se_privilege_enabled;
: Adjusttokenprivileges (htoken, false, & TP, sizeof (token_privileges), null, null );
: Setsystempowerstate (false, true );

How to control the volume of microphone or line input in the recording attribute during recording
1. mixergetnumdevs obtains the number of mixing devices.
2. cyclically retrieve the device information mixeropen/mixergetlineinfo
3. Determine the type based on the dwcomponenttype in the obtained mixerline.
4. mixersetcontroldetails
Use the resource manager to open the specified folder and set the target folder to the selected status.: Winexec ("Explorer/N,/select, F: // Win98", sw_shownormal );
Enable or disable the Input Method: Iactiveime: setactivecontext

Extracts icons from the. .exe or. dll file.: Extracticon
Capture the maximum and minimum messages of other programs:Http:// Temp =. 8477289.

The wm_devicechangle message works with the deviceiocontrol function.Disable USB flash drive

Determine whether the edit box control is read-only:

Cedit * temp = (cedit *) getdlgitem (idc_edit1 );
Dword a = temp-> getstyle ();
If (A & es_readonly) // es_readonly = 0x800
Afxmessagebox ("Read Only ");
Afxmessagebox ("not read only ");

Method for printing percentage % in sprintf, From:
Int percent = 50;
Char Buf [100];
Sprintf (BUF, "% d % complete/n! ", Percent );
Printf (BUF); // print only one %
Printf ("% s", Buf); // after this is changed, two % are printed, as we expected.

Precise latency: The kestallexecutionprocessor function helps you, From:
But the problem is that kestallexecutionprocessor's document says that his minimum cycle cannot be greater than 50 microseconds! Otherwise, the system will pause ..
Test results show that kestallexecutionprocessor (416) may cause mouse pauses on some machines-_-But kedelayexecutionthread (kernelmode, false, & waittime); no matter how to set a small timeout, the results are as follows: 10 ms ??? Too much difference

How to get the folder size, From: Temp =. 2216761.
# Include
# Import "scrrun. dll" raw_interfaces_only
Int main ()
Coinitialize (null );
Scripting: ifilesystem3ptr FS;
FS. createinstance (_ uuidof (scripting: FileSystemObject ));
Scripting: ifolderptr folder;
FS-> getfolder (_ bstr_t ("F: // Win98"), & folder );
_ Variant_t vsize; Folder-> get_size (& vsize );
Cout <"F: // Win98 size:" <(long) vsize <"bytes" <}
Catch (_ com_error & E)
_ Bstr_t bstrsource (E. Source ());
_ Bstr_t bstrdescription (E. Description ());
Cout <"get directory size via FileSystemObject, by masterz" <cout <"com error occurred, source:" <(lpctstr) bstrsource <cout <"Description: "<(lpctstr) bstrdescription <}
Couninitialize ();
Return 0;

Open the console interface and write some text on it.:

Allocconsole (); // open the console window
Handle houtput = getstdhandle (std_output_handle); // obtain the console handle
Unsigned long lgsize;
Char * strbuf = "display this line of information. ";
Writefile (houtput, strbuf, strlen (strbuf), & lgsize, 0 );
Freeconsole (); // close the console window for releasing new information

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: 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.