Visual C ++ Programming Troubleshooting (1)

xiaoxiao2021-03-06  15

[Foreword:] There are many problems encountered in the program. It will not have problems, but many solutions are flooded in the vast "water" of the forum. In order to facilitate the same way to save these essence, I will The problems in the actual programming and the excellent post-retusing collected in the forum are published in the form. All questions and answers in this article are from the forum.

Question 1: How to implement the CD-ROMB drive population population

The key is here:

Void ctrl_cdrom_door (LPCTSTR Drivename, // The name of the drive is like f:, etc.. Bool Fopen file: // When pop-up, use false {tchar devstr [128], ctrlstr [128]; WSPrintf (DevStr, _t) ("Open% S Type Cdaudio Alias ​​Mycd Wait"); WSPrintf (CtrlStr, _T ("SET mycd door% s wait"), fopen? _t ("open"): _ t ("closed)); mcISendstring (devstr, null, 0, null); mcIndstring (Ctrlstr, NULL, 0, NULL); McISendString (_T ("Close Mycd Wait"), NULL, 0, NULL);} File: // Test example code .void CMainFrame :: OnTestOpen () {// TODO: Add your command handler code here ctrl_cdrom_door ( "F:", true);} void CMainFrame :: OnTestClose () {// TODO: Add your command handler code here ctrl_cdrom_door ( "F ", false);

Question 2: How to achieve the interability of a smore?

The key is here:

// j2f.cpp: Simplified (GB) ==> Traditional process // reverse conversion is similar .// Note Direct from Simplified -> BIG5 can not do one or one. Do you have a lot? The appearance, //, this needs to be turned into a traditional. Re-turned into big5.// I feel that this method should be consistent with the interaction with the interaction provided by Winnt or Office. #Include "stdafx.h" # Include #include #include #include #include using namespace std;

void j2f (const string & s) {int n = s.length (); int r = LCMapString (MAKELCID (MAKELANGID (LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED), SORT_CHINESE_PRC), LCMAP_TRADITIONAL_CHINESE, s.c_str (), s.length (), NULL, 0); if (r) cout << "error:"! BIG5 FT [R1] = 0; cout

// From the standard input Simplified Guide -> BIG5 Traditional Standard Output, enter two empty lines exit int Main (int Argc, char * argv []) {

For (;;) {char line [1024]; cin.getline (line, sizeof (line); string s; if (! cin || s.Length () == 0) Break; J2F (S } _ getch (); return 0;}

Question 3: How to get a view pointer in multi-thread?

Keys here: There are two ways to achieve your requirements:

1) The first method:

If the multi-thread does not appear in app.cpp, then add extern cysurapp theApp in multi-threaded .CPP;

// get document template: POSITION curTemplatePos = theApp.GetFirstDocTemplatePosition (); CDocTemplate * m_doc = theApp.GetNextDocTemplate (curTemplatePos); file: // get the document: curTemplatePos = m_doc-> GetFirstDocPosition (); CYourDoc * m_pdoc = (CA8Doc *) m_doc-> getnextdoc (CURTEMPLATEPOS);

File: // Get the view: CURTEMPLATEPOS = m_pdoc-> getFirstViewPosition (); cyourview * m_pview = (cyourView *) m_pdoc-> getnextView (CURTEMPLATEPOS);

File: // Call the view function: m_pview-> Put ();

2) Second method:

// Get the form pointer: cmainframe * pframe = (cMAINFRAME *) AFXGetApp () -> m_pmainwnd; file: // Get a view with the form: cyorView * m_pview = (cyourView *) Pframe-> getActiveView ();

File: // Call the view function: m_pview-> Put ();

Question 4: How do I make the program do not create a new document when starting?

The key is here: Join before the ProcessShellcommand function in the program's initInstance:

CMDINFO.M_NSHELLCOMMAND = ccommandlineinfo :: Filenothing

Problem 5: How do I get all the views in the MDI program?

The key is here:

You must use the functions not recorded in some documents:

CDocument :: GetFirstViewPosition (); // DOCCORE.CPPCDocument :: GetNextView (); // DOCCORE.CPPCMultiDocTemplate :: GetFirstDocPosition (); // DOCMULTI.CPPCMultiDocTemplate :: GetNextDoc (); // DOCMULTI.CPP

At the same time, you also need to deal with CWINAPP members M_TemplateList.

Problem 6: How do I get all the tables in a database?

The key is here:

HRESULT hr = S_OK; _ConnectionPtr pConnection = NULL; _CatalogPtr pCatalog = NULL; _bstr_t strCnn ( "Provider = sqloledb; Data Source = MyServer;" "Initial Catalog = pubs; User Id = sa; Password =;"); try {file: // Define a command Object for a stored procedure.

pConnection.CreateInstance (__ uuidof (Connection)); hr = pCatalog.CreateInstance (__ uuidof (Catalog)); hr = pConnection-> Open (strCnn, "", "", adConnectUnspecified); pCatalog-> PutActiveConnection (_variant_t ((IDispatch * ); long ntbcount = pcatalog-> TABLES-> count; // This is the number of tables you want PConnection-> close (); pConnection = null;} catch (_COM_ERROR & E) {....

Question 7: What is the difference between blocking and non-blocking socket from the application perspective?

The key is here:

From system performance, it is more complicated with non-blocking socket efficiency, but programming is more complex, especially when you use events or messages, however, you can manage more than 100 Socket connectivity, efficiency through 4 working threads. Very high, do not need to manage only one socket connection every work thread. It is relatively simple to use blockage, but when more clients consume too much.

The so-called four threads are managed more than 100 Sockets, but this kind of conception: establish a thread pool, when there is a socket event, take a thread from the thread pool, execute, return the thread to the thread pool in. This approach works better under the following conditions: (1), each Socket connection time is longer, constantly interacting with the server.

(2), each connection Socket is not a specific implementation of the transceiver data in the time: (I am implemented in the Windows environment, now in Linux is now studying) Overlaped IO, or complete port (Compeleteio) )

Question 8: How do I set the size of the stack?

The key is here:

Method 1: Stacksize definition .def file

Syntax: stacksize reserve [, commit]

RESERVE: The size of the stack; the commit: option, related to the operating system, only assigning the physical memory only at NT

Method 2: Setting / Stack

Open the project, follow the operations the menu as follows: Project-> setting-> link, select Output in Category, and set the maximum value of the stack and commit in RESERVE.

Note: The Reserve default is 1MB, the minimum is 4Byte; commit is inside the page file that is retained in the virtual memory. It is set to make the stack open a large value, which may increase the overhead and startup time of memory.

Question Nine: How do I get the UDP port on this machine?

The key is here:

Can be implemented by port scanning technology.

Port Scanning Port Scan is to determine what services are running by connecting to TCP or UDP ports connected to the target system. Generally speaking, there are three uses:

* Identify TCP and UDP services running on the target system. * Identify the operating system type of the target system (Windows 9X, Windows NT, or UNIX, etc.). * Identify a version number of an application or a particular service. Port scan technology:

1. TCP Connect Scan: This method is the easiest, directly to the target port and completes a complete three-time handshake process (SYN, SYN / ACK, and ACK). The disadvantage is that it is easy to detect by the target system.

2. TCP SYN SCAN: This technology is also called "Half-Open Scanning) because it does not complete a complete TCP connection. This method sends an SYN packet to the target port. If the target port returns SYN / ACK, it can be sure that the port is in an audit state; otherwise, returning is RST / ACK. This method is more concealed than the first, and may not leave a scanning trace in the target system.

3. TCP FIN SCAN: This method sends a FIN group to the target port. Press RFC793 (http://www.ieetf.org/rfc/rfc0793.txt), for all closed ports, the target system should return the RST flag. This method is usually used in Unix-based TCP / IP stacks.

4. TCP Xmas Tree Scan: This method sends a packet containing Fin, URG, and PUSH flags to the target port. According to RFC793, for all closed ports, the target system should return the RST flag. 5. TCP NULL SCAN: This method sends a packet that does not contain any flags to the target port. According to RFC793, for all closed ports, the target system should return the RST flag.

6. UDP SCAN: This method sends a UDP packet to the target port. If the target port responds with the "ICMP port unreachable" message, then the port is closed; it is reversed, if the "ICMP port unreachable" response message is not received, the port can be opened. Since the UDP protocol is a connectionless protocol, the accuracy of this scanning technology is highly dependent on network performance and system resources. In addition, if the target system uses a large number of packet filtration techniques, the UDP scanning process will become very slow. If you want to scan the Internet, then you can't expect a reliable result. In addition, IP protocols in some system are implemented, for all scanned ports, whether they are in close or listening, returns the RST flag (we know, this does not conform to RFC793). Therefore, when scanning such a system, different scanning results may be different from different scanning techniques. Port Scanning Tools: * Strobe

Strobe is one of the old port scan tools, the fastest and most reliable TCP scan tool. The disadvantage is that there is no UDP scan function. URL: ftp.win.or.jp/pub/neetwork/misc/strobe-1.05.tar.gz * udp_scan

UDP_scan originally from Satan (Security Administrator Tool For Analyzing Networks, security administrator network analysis tool, Satan's new version renamed Saint, published by http://wwdsilx.wwdsi.com). UDP_scan is one of the most reliable UDP scanning tools, but the concealedness is not good, it is easy to detect the target system.

URL: ftp://ftp.technotronic.com/Unix/neetwork-sanners/udpscan.c * netcat

Netcat is one of the most useful network tools, a lot of functions, namely the Swiss army in the network security kit. Netcat provides basic TCP, UDP scanning.

URL: http://www.l0pht.com/netcat * PortPro and portscan

It is one of the fastest port scan tools on Windows NT. Portscan can specify a scan range, and PortPro can only increase the scan, they can not scan multiple IP addresses at a time.

URL: portpro: http://www.securityfocus.com * NetWork Mapper (NMAP)

NMAP is a high-end port scan tool that provides a variety of scanning methods. NMAP has some interesting functions, and the method with the fragment TCP header (which is transmitted into a plurality of TCP headers into multiple packets) bypass some firewalls with simple packet filtering. Another interesting feature of NMAP is to send a spoofing address scan. The specific implementation method is to send a large number of SYN packets to the target system with a forged IP address, and mix the SYN group in which the real address is mixed, which can cause a large amount of time to respond to those forged packets, resulting in denial services (Denial Of service, this is the so-called SYN FLOOD attack. URL: http://www.insecure.org/nmap

Question 10: How to use the DirectoryEntry component to view the network

The key is here:

The DirectoryEntry component provides a Path property. According to the documentation, this property specifies the object name used to access the object in the directory service, which is as follows:

Protocol: // Servername: Port Number / Distinguished Name

The first part of this statement defines the protocol that will be used, such as

LDAP: (LightWeight Directory Access Protocol)

IIS: (Provide IIS metadata to read and configure Internet Infomation Server)

WINNT: (Provides access to Windows NT "access in very limited performance)

NDS: (Provide access to Novell Directory Service)

Wait (detail, refer to MSDN).

According to this, we construct a DirectoryEntry instance, set its PATH to "Winnt:" to discover all domains (and working groups) on the network by enumerating the enumerations of it. In this way, the sub-items of the found domains (and the working group) can be enumerated, and all the computers on the network can be found. One of the console applets demonstrate this.

Using system; using system.directoryservices;

Class Tempclass {static void main () {enumComputers ();

static void EnumComputers () {using (DirectoryEntry root = new DirectoryEntry ( "WinNT:")) {foreach (DirectoryEntry domain in root.Children) {Console.WriteLine ( "Domain | WorkGroup: / t" domain.Name); foreach (DirectoryEntry Computer in Domain.children) {Console.Writeline ("Computer: / T" Computer.name);}}}}}

The two nesting Foreach loops in the above code does not look great, and the display effect of the console is not so beautiful. Below, I will change the code and transplant it to WinForm.

Create a Windows Application [C #], add a TreeView on the Form, named TreeView1.

Add the following functions:

// constructed a node with the specified text, add it as the sub-node of the parameter para, and return to the node of the just constructed PREENODE TREENODE (Treenode PARANT, STRING TEXT) {Treenode Node = New Treenode (TEX); parant.nodes. Add (Node); Return Node;} // Recipiently find all child nodes of the parameter entry, and display in TreeView1; the Entry is required to correspond to the EnTryNode (DirectoryEntry Entry, Treenode EntryNode) {if (entry. Children! = NULL) File: // If there is no child node, the {FORECH (DirectoryEntry I in entry.children) {file: // adds each child node to TreeView, and recursively enumchildren (I, AddNode, i. Name));}}}

// construct a root node with a given string, and list all the child node private void enumerate (string path) {use {using (directoryEntry Root = new DirectoryEntry (path) {Treenode node = new Treenode (root.name) ... treeview1.nodes.add (node); enumchildren (root, node);}} catch {}}

In this way, through the "Winnt:" to the function enumerate (string), you can see all the computers on the network in TreeView, and users, groups, services such as users, groups, services on each computer.

转载请注明原文地址:https://www.9cbs.com/read-49144.html

New Post(0)