IT Community - Software Programming, Web Development and Technical Support

Visual C++ Tips & Tricks

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


Go Back   IT Community - Software Programming, Web Development and Technical Support > Software Development > C and C++ Programming

Register FAQ Members List Calendar Mark Forums Read

Reply
 
Thread Tools Display Modes
  #1  
Old 03-12-2007, 05:50 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Visual C++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2  
Old 03-19-2007, 02:07 AM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3  
Old 03-27-2007, 02:59 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

Hi All,

Here is the tip for TCHAR conversion in Visual C++. I have given the sample conversion program source code.
TCHAR convertion
TCHAR szCurDir[MAX_PATH];
wsprintf(szCurDir,"%s",dFile);

szCurDir[dFile.GetLength()+1]='\0';

USHORT m_Length = strlen(szCurDir);

WCHAR *m_Buffer = (LPWSTR)malloc((m_Length + 1) * sizeof(WCHAR));
MultiByteToWideChar(CP_ACP, 0, szCurDir, m_Length, m_Buffer, m_Length);
m_Buffer[m_Length] = L'\0';
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Booom : 08-09-2007 at 04:37 AM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4  
Old 03-27-2007, 03:02 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5  
Old 03-27-2007, 03:10 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6  
Old 03-27-2007, 03:14 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

Some useful techniques and ways of using WTL features like:
• dialog box is resizable
• WTL treeview doesn't store text strings for each tree item. Display of each item is done 'on demand', when TVN_GETDISPINFO notification message arrives
• tree items contain COM interface pointers as item data. In display phase these interfaces are queried to find out what text and bitmap to display for the DOM node or attribute in question
• tree is constructed dynamically. No child nodes are added until the node is expanded. Children nodes are deleted when parent is collapsed
• there is context menu that allows you to expand or collapse a node in its entirety, with all of its children, its children's children, and so on
• BHO and IE Extension objects may be loaded in different COM apartments. BHO registers its instances in Global Interface Table (GIT) so that Extension can call methods on it
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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7  
Old 03-27-2007, 03:19 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8  
Old 03-27-2007, 03:29 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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 Classes
CObject
CInternetSession
CInternetConnection
CFtpConnnection
CHttpConnection
CGopherConnection
CFindFile
CFtpFindFile
CGopherFindFile
CGopherLocator
CException
CInternetException
Cfile
CStdioFile
CInternetFile
CHttpFile
CGopherFile
thanks
__________________
Karpagarajan. R
Necessity is the mother of invention

Last edited by Karpagarajan : 03-27-2007 at 03:33 PM.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #9  
Old 03-27-2007, 03:36 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #10  
Old 03-27-2007, 03:57 PM
Karpagarajan Karpagarajan is offline
D-Web Analyst
 
Join Date: Mar 2007
Posts: 301
Karpagarajan is on a distinguished road
Thumbs up Re: VC++ Tips & Tricks

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.
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
C# .Net Tips & Tricks oxygen C# Programming 85 01-08-2009 12:25 AM
SAP Tips & Tricks leoraja8 Operating Systems 0 03-29-2008 12:11 AM
Visual Studio Tips & Tricks SaravananJ C# Programming 197 12-20-2007 03:25 AM
PHP Tips and Tricks Sabari PHP Programming 20 12-18-2007 05:26 AM
.NET tricks & Tips Karpagarajan VB.NET Programming 1 04-23-2007 08:17 AM


All times are GMT -7. The time now is 05:05 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.
Our Partners
One Way Moving Companies | Stamford Dentist | Euro Millions Lottery | Home Loans| Furniture

SEO by vBSEO 3.0.0