C complex of. Net programmers

Source: Internet
Author: User

About two years of. Net experience, one year of BMC experience, as an electronic professional, is still in mind for C and C ++.

Recently, when the project is idle, check windows programming and windows via C/C ++, and use C ++ to create and install the class library we developed.ProgramDespite being a simple Windows C program, the sense of accomplishment arises. This article records the experiences of developing this applet:

1. Dialog Box-based Windows program

Although standard Windows programs always start with user-registered window classes, programmers need to use while to receive and process messages in the application message queue. However, if you want to quickly develop small programs, you can start the program based on the dialog box as follows. The dialog box automatically creates a message loop to receive messages. All we need to do is define a dialog box processing function.

 
Int winapi _ twinmain (hinstance, hinstance, ptstr szcmdline, int icmdshow) {dialogbox (hinstance..., dlgproc); Return 0 ;}

 

2. Check for Memory leakage

Although it is a small program, an error occurs in the Windows pop-up application after the program returns during the final execution. I guess the memory is leaking. It's really embarrassing that such a small program can leak memory. Later I found a way to check memory leakage on the Internet: http://www.cppblog.com/Lyt/archive/2009/03/22/77517.html

 
# DEFINE _ crtdbg_map_alloc # include <windows. h> # include <tchar. h> # include <stdio. h> # include <stdlib. h> # include <crtdbg. h> int winapi _ twinmain (hinstance, hinstance, ptstr szcmdline, int icmdshow) {hinstance = hinstance ;... _ crtdumpmemoryleaks (); Return 0 ;}

Here, the _ crtdumpmemoryleaks () will report the memory leakage in the output window of.

 

3. The resource ID of the dialog box cannot be obtained based on the dialog box handle.

Although getwindowlong (hwndchild, gwl_id); obtains the window resource ID through the handle, it is invalid when the handle is a dialog box handle. The related functions are as follows:

Id = getdlgctrlid (hwndchild); hwndchild = getdlgitem (hwndparent, ID );

 

4. getwindowrect (hdlg, & rect); returns the size of the window. The right and bottom in the returned rect are actually the width and height of the window. I really don't know why the name is so ambiguous .. Getsystemmetrics (sm_cxfullscreen) and getsystemmetrics (sm_cyfullscreen) obtain the pixel information of the display.

Movewindow (hdlg, inewleft, inewtop, width, height, false); you can set the position of the window.

 

5. Set the checkbox:

Sendmessage (hwnd, bm_setcheck, bst_checked, 0); sendmessage (hwnd, bm_setcheck, bst_unchecked, 0); sendmessage (hwnd, bm_getcheck, 0, 0 ));

 

6. the target file of findfirstfile can have wildcards, but must be a full path. It can also be used to determine whether a path exists (file_attribute_directory)

 
Win32_find_data findfiledata; handle hfind = findfirstfile (TMP, & findfiledata); If (invalid_handle_value = hfind) {} else {findclose (hfind );}

 

7. string processing

_ Tcscpy, _ tcscat, _ tcslen, _ tcscmp, and _ tcsstr

 

8. registry operation. To set environment variables, you must directly set them in the registry.

Regopenkeyex (HKEY_LOCAL_MACHINE, text ("SYSTEM \ CurrentControlSet \ Control \ Session Manager \ environment \"), 0, key_all_access, & henvkey) // you can read the key value once to obtain the required buffer length and Data Type regqueryvalueex (henvkey, text ("path"), null, & dwtype, null, & cbdata) regqueryvalueex (henvkey, text ("path"), null, & dwtype, lpborignalenvpath, & cbdata); // The returned lpbyte is already a character array, you can use tchar * szorignalenvpath = (tchar *) lpborignalenvpath; # ifdef _ Unicode , Unicodelstatus status = regsetvalueex (henvkey, text ("path"), null, dwtype, (lpbyte) sztargetenvpath, _ tcslen (sztargetenvpath) * 2 + 2 ); # elselstatus status = regsetvalueex (henvkey, text ("path"), null, dwtype, (lpbyte) sztargetenvpath, _ tcslen (sztargetenvpath) + 1 ); // After setting, you can use the following method to notify the system process that the environment variable has changed. This also prompts that we sometimes need to handle the meaning of wm_settingchange when writing an application sendmessage (hwnd_broadcast, wm_settingchange, 0, (lparam) text ("environment "));

9. Get system variables and Environment Variables

// You can also call it twice to obtain the buffer length dwsize = getenvironmentvariable (text ("homedrive"), null, 0); szhomedrive = new tchar [dwsize + 1]; getenvironmentvariable (text ("homedrive"), szhomedrive, dwsize + 1 );

10. c multi-file compilation, scope, and storage cycle

Http://www.cnblogs.com/P_Chou/archive/2012/05/25/2518097.html

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.