This is a discussion on Visual C++ Tips & Tricks within the C and C++ Programming forums, part of the Software Development category; About Microsoft Visual C++: An application development tool developed by Microsoft for C++ programmers. Visual C++ supports object-oriented programming ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| About Microsoft Visual C++: An application development tool developed by Microsoft for C++ programmers. Visual C++ supports object-oriented programming of 32-bit Windows applications with an integrated development environment (IDE), a C/C++ compiler, and a class library called the Microsoft Foundation Classes (MFC). The IDE includes an AppWizard, ClassWizard, and testing features to make programming easier. Visual C++ was introduced in 1993, and Release 4.0 became available in 1996. Iam a Visual C++ developer and i have gained vast experience by working with this tool right from its introduction. Now i would like to share all my knowledge and experience through this website and i will be giving lots of Visual C++ Tips. I hope this will be helpful for the Visual C++ developers around the world. I will be contributing to this thread on a regular basis so please check back this thread regularly. If you are a Visual C++ beginner, you can join this community and ask me any queries you have in Visual C++ and i will be happy to help you out. If you are an experienced Visual C++ developer i welcome you to join the list of experts in this community to help others and share your knowledge. Iam starting with a Visual C++ tip right now. When i tried to create a dll in c using visual c++ compiler i received the errors below. LNK2001: unresolved external symbol _main Debug/windll.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. Reason: The problem is in creating a executable file hence the compiler is looking for the main method.. Inorder to create a DLL file we have to some manipulation in the project properties. i.e open ur project properties window and select the link tab.Change the output file from exe to DLL and in the "Project Options" text box change the "subsytem console" to "dll" thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-22-2007 at 11:24 AM. |
| Sponsored Links |
| |||
| Hi all, Below I have given the trick to create borderless window in Visual C++. Hope it will be very useful for your vc++ project. BORDERLESS WINDOW DWORD dwStyle = GetWindowLong((ProgEntry->m_Prog).m_hWnd, GWL_STYLE); SetWindowLong((ProgEntry->m_Prog).m_hWnd, GWL_STYLE, dwStyle ^ WS_BORDER); SetWindowLong((ProgEntry->m_Prog).m_hWnd, GWL_EXSTYLE, GetWindowLong((ProgEntry->m_Prog).m_hWnd, GWL_EXSTYLE)^WS_EX_CLIENTEDGE); simple code but very useful... ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:37 AM. |
| |||
| Hi All, Here is the tip for TCHAR conversion in Visual C++. I have given the sample conversion program source code. TCHAR convertionthanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:37 AM. |
| |||
| The following example demonstrates the use of CStringT::FormatV. Its very simple to write a program for formatting the variables in Visual C++. Below is a sample #include <string.h> #include <atlsimpstr.h> #include <atlstr.h> void WriteString(LPCSTR pstrFormat, ...) { CString str; // format and write the data you were given va_list args; va_start(args, pstrFormat); str.FormatV(pstrFormat, args); va_end(args); printf(str); return; } int main( ) { WriteString("%d error(s) found in %d line(s)", 10, 1351); } thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:38 AM. |
| |||
| Hi, I have given the technique to use array in Visual C++ below. Hope it is useful for you. Array Declaration // example of 2 dimensional array typedef CArrayEx CIntArray; CArrayEx<CIntArray, CIntArray &> a2D; CIntArray aInt; a2D.Add (aInt); // auto_ptr example. CArray<AUTO_PTR,auto_ptr<CValue> > a; CArray<AUTO_PTR,auto_ptr<CValue> > b; TRACE(_T("Create\n")); a.Add(auto_ptr<CValue>(new CValue(1))); a.Add(auto_ptr<CValue>(new CMyValue(2))); b.Add(auto_ptr<CValue>(new CValue(3))); b.Add(auto_ptr<CValue>(new CMyValue(4))); // TRACE(_T("Copy\n")); // a.Copy(b); TRACE(_T("Append\n")); a.Append(b); TRACE(_T("Remove all\n")); b.RemoveAll(); thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:38 AM. |
| |||
| Some useful techniques and ways of using WTL features like: • dialog box is resizable The VC++ 6.0 project that comes with this article is an ATL in-process COM server, housing Browser Helper (BHO) and IE Extension objects. When the IE browser downloads HTML page, BHO will pop up a dialog box with a treeview which will display Document Object Model (DOM) nodes and attributes of that HTML document. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| How to get the URL from ActiveX Control This is a simple technique to determine the URL of the web page in which the ActiveX control is hosted. I had to develop ActiveX controls for web based applications. Some of these controls were manipulating the local resources. To disable malicious use of these control by others through scripting, I had to implement security check. I decided to implement a simple security scheme where I determine the url in which the control is hosted. If the url comes from our domain, I enabled its functionality. I used GetMoniker method of IOleClientSite Interface.The IMoniker interface has GetDisplayName() method, which returns a user-readable representation of the moniker. Sample Code HRESULT hrResult = S_FALSE; IOleClientSite *pClientSite = NULL; IMoniker* pMoniker = NULL; LPOLESTR sDisplayName; // If using ATL to develop, use the m_spClientSite data // member of CComControl class. // If using MFC, use the following code: // (member function of COleControl class // - don't forget to call release) // pClientSite = GetClientSite(); hrResult = m_spClientSite->GetMoniker(OLEGETMONIKER_TEMPFORUSER, OLEWHICHMK_CONTAINER, &pMoniker); if(SUCCEEDED(hrResult)) { hrResult = pMoniker->GetDisplayName(NULL, NULL, &sDisplayName); pMoniker->Release(); } //TODO : relevant processing with sDisplayName and //free sDisplayName using SysFreeString() thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| Common WinInet MFC Classes Here is MFC classes hierarchy. In MFC hierarchy you see CFtpConnection, CHttpConnection, and CGopherConnection classes are derived from CInternetConnection. WinInet MFC ClassesthanksCObjectCInternetSession ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 03-27-2007 at 03:33 PM. |
| |||
| Here is the sample FTP program in Visual C++ CFtpConnection* m_pFtpConnection; m_pFtpConnection = m_pInetSession->GetFtpConnection(strServerName, szftpUserID, szftpPassword, nPort, FALSE); // Do connection to the FTP server if ( m_pFtpConnection->PutFile(lpExePath + "default.asp", "default.asp", FTP_TRANSFER_TYPE_BINARY, 1) == 1 ) AddLog("File : default.asp uploaded successfully."); else AddLog("Error in file :default.asp upload."); thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:39 AM. |
| |||
| Differences between release and debug builds I used the phrase 'debug build' quite a few times in the preceding paragraphs. It's important to understand that asserts are a debug helper. If you're building a debug version of your program the compiler includes all the code inside the ASSERT(...). If you're building a release version the ASSERT itself and all the code inside the parentheses disappears. The assumption is that you've tested your debug builds and caught all likely errors. If you're unlucky and missed an error and release a buggy version of your program the hope is that it'll limp along even though it failed a test that an assert would have caught. Sometimes optimism is a good thing! It might happen that you want, in a debug build, to assert that something is true and the something you're asserting is code that must be compiled into your program whether it's a debug build or a release build. That's where the VERIFY(...) macro comes to the rescue. VERIFY in a debug build includes the extra plumbing MFC provides to let you jump into the debugger in the event that the condition isn't satisfied. In a release build the extra plumbing is omitted from your program but the code inside the VERIFY(...) statement is still included in your executable. For example. VERIFY(MoveFile(szOriginalFilename, szNewFileName)); will cause a debug assert if, for whatever reason, the MoveFile() function fails in debug builds. Regardless of what kind of build the MoveFile() call will be included in your program. But in a release build the failure of the call is simply ignored. Contrast this with ASSERT(MoveFile(szOriginalFilename, szNewFileName)); In debug builds the MoveFile() will be compiled and executed. In release builds the line completely disappears and no file move is attempted. This can lead to some puzzled head-scratching thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 03-27-2007 at 03:59 PM. |
| |||
| How to get the sub menu handle? Here is the tips for getting the Sub menu handle... CMenu *m_lMenu; // A pointer to the menu CPoint m_pPoint; // A copy of the mouse position // Copy the mouse position to a local variable m_pPoint = point; // Convert the position to a screen position ClientToScreen(&m_pPoint); // Get a pointer to the window menu m_lMenu - GetMenu(); // Get a pointer to the first submenu m_lMenu = m_lMenu->GetSubMenu(0); // Show the Popup Menu m_lMenu->TrackPopupMenu(TPM_CENTERALIGN + TPM_LEFTBUTTON, m_pPoint.x, m_pPoint.y, this, NULL); thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| When i tried to create a dll in c using visual c++ compiler i received these errors, LNK2001: unresolved external symbol _main Debug/windll.exe : fatal error LNK1120: 1 unresolved externals Error executing link.exe. The actual reason for the above problem is... The problem is creating a executable file hence the compiler is looking for the main method.. Inorder to create a DLL file u have to some manipulation in the project properties. i.e open project properties window and select the link tab. change the output file from exe to DLL and the "Project Options" text box change the "subsytem console" to "dll" After setting this in the project properties, the program was compiled successfully. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:39 AM. |
| |||
| hi, Do you know How to change the cursor in a form? Here is the simple tips for changing the cursor. Add the WM_SETCURSOR message handler . LRESULT OnSetCursor(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| The following code is used to convert the mouse pos to the client pos. Useful one. POINT point; GetCursorPos(&point); ScreenToClient(&point); If you find any tips and tricks, please post your idea here. ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| Compiler Problem My compiler(VC++ 6) never accepts cin >> str1 if str1 is declared as a string . the following program gives error. #include <iostream.h>Solutions : Change "#include <iostream.h>" to "#include <iostream>". Use the newer standard headers such as iostream, string and fstream instead of iostream.h, string.h and fstream.h. Mixing older and new style headers causes compilation problems such as I experienced. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| Here is one tip: You can view whitespace (tabs and spaces) in your source files by clicking advanced on the Edit menu and then clicking view Whitespace thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention |
| |||
| Hi, You can use Visual C++ debugger window in different situation. It is very useful one to trace your code errors. The Debugger windows support intelligent drag & Drop where the result depends on the drop location. For example, you can add a variable to the Watch Window by dragging it from the variables window or view the memory contents by dragging a variable to the memory window. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:40 AM. |
| |||
| About Macros When creating objects that derive directly or indirectly from CObject, such as CFrameWnd, you should let the compiler know that you want your objects to be dynamically created. To do this, use the DECLARE_DYNCREATE and the IMPLEMENT_DYNCREATE macros. The DECLARE_DYNCREATE macro is provided in the class' header file and takes as argument the name of your class. An example would be DECLARE_DYNCREATE(CTheFrame). Before implementing the class or in its source file, use the IMPLEMENT_DYNCREATE macro, passing it two arguments: the name of your class and the name of the class you derived it from.thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 03-29-2007 at 01:16 PM. |
| |||
| Marshalling Library and Vista Development with Visual C++ The latest Channel 9 video by the Visual C++ team has just been released. This video features Sarita Bafna, a PM on the libraries team, talking about a few of latest features which are previewed in the March CTP: MFC support for Vista IDE support for Vista Native-Managed Marshalling Library “MFC support for Vista” includes support for new Vista file dialogs, support for the new controls specifically for Vista and support for additional Windows messages (including some for XP) that had not previously been supported by MFC etc. The IDE Support for Vista includes support for the new controls and high quality graphics in the Resource Editor, support for enabling UAC for VC++ apps etc. The Native-Managed Marshalling Library supports marshalling strings between native-managed code and includes an extensibility model so you add support for your own types. This video includes a demonstration of the VC++ features to support Vista development. thanks ![]()
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Booom : 08-09-2007 at 04:40 AM. |
| |||
| Tips1: The Compiler options /Yc and /Yu (Create and Use specific procompiled header) provide the most efficient precompiled header management. Tips2: By pressing F2 and Shift F2 to go to Next and previous source file Bookmark thanks
__________________ Karpagarajan. R Necessity is the mother of invention Last edited by Karpagarajan : 04-06-2007 at 04:05 AM. |