Hottest Forum Q&A on CodeGuru for the week of March 29th, 2004

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


Why does the CFtpConnection::Command function not work for me? (top)  

Thread:

lspecht is working with the CFTPConnection class. He wants to use the Command method of that class. Unfortunately, the VC++ compiler complains that the Command function does not exist. Take a look:

I need some help . The CFtpConnection object in the code below
seems to work well if I comment out the line:

"if(pFTPSite->Command..." function

However, when the command function is not commented out, a
compilation error occurs.

What is causing this error? The CFtpConnection object is obviously
working for the other functions. I have included afxinet.h in the
file. I've also installed the latest SDK and added wininet.lib to
the project settings for the linker (something I saw in anotherthread).

The error is:
c:\Visual C++ Applications\UnixScriptGenerator\RunScriptWizard.cpp(929) :
error C2039: 'Command' : is not a member of 'CFtpConnection'
c:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\atlmfc\include\
afxinet.h(371) : see declaration of 'CFtpConnection'
c:\Visual C++ Applications\UnixScriptGenerator\RunScriptWizard.cpp(929) 
: error C2065: 'CmdRespNone' : undeclared identifier
Here is the code:
void CRunScriptSTDIODialog::RetrieveInputList()
{
   CInternetSession sess;
   CFtpConnection* pFTPSite = NULL;
   CUnixScriptGeneratorDoc* pDoc =
      CUnixScriptGeneratorDoc::GetDocument();
   ASSERT_VALID(pDoc);

   UpdateData(TRUE);

   try{
      pFTPSite = sess.GetFtpConnection(pDoc->pDefaultCProfile->
                 m_strFTPServerID,
      pDoc->pDefaultCProfile->m_strUserID,pDoc->pDefaultCProfile->
            m_strUserPassword,
      (INTERNET_PORT)pDoc->pDefaultCProfile->m_nPortNumber);
      if(pFTPSite->SetCurrentDirectory(pDoc->
         newRunScript.m_strRemotePathToInputFiles)==0){
         AfxMessageBox("Could not set current directory on ftp site",
                       MB_ICONEXCLAMATION);
      }

      if(pFTPSite->Command("ls *.inp > UnixScriptGenTemp",
         CmdRespNone, FTP_TRANSFER_TYPE_ASCII, 1)==0){
         AfxMessageBox("Could not perform ls command on ftp site",
                       MB_ICONEXCLAMATION);
      }

   }
    catch (CInternetException* pEx){
      AfxGetApp()->LoadStandardCursor(IDC_ARROW);
      pEx->ReportError(MB_ICONEXCLAMATION);
      pFTPSite = NULL;
      pEx->Delete();
   }
   delete pFTPSite;
}
According to the error:
'Command' : is not a member of 'CFtpConnection'

Command is not a function of the CFTPConnection class. But, according to the MSDN, the function does exist. If you take a look at the afxinet.h header file and search for the command function, you will see that it is declared like this:

#if _WIN32_IE >= 0x0500
CInternetFile* Command(LPCTSTR pszCommand,
                       CmdResponseType eResponse = CmdRespNone,
DWORD dwFlags = FTP_TRANSFER_TYPE_BINARY,
DWORD_PTR dwContext = 1);
#endif

And what does that mean? Well, it means that the command function is only available if you have installed IE5.0 or later. So far, so good. But, lspecht has installed IE6.0, so somewhere it is not picking up the correct version of IE. If you take a look at the stdafx.h, you will see that #define _WIN32_IE is defined as 0x0400. Just change it to 0x0600 and it will work.


How do I change the title of CMDIFrameWnd when the child is maximized? (top)  

Thread:

jeffchen wants to change the title of a CMDIFrameWnd when the child window is maximized. Do you know how to do that?

As all you know, usually the title bar will show
"AppTitle - [DocTitle]". In my project, I want to change it to
"[DocTitle] - AppTitle".

I know the FWS_ADDTOTITLE and FWS_PREFIXTITLE settings. However, It
only works when the MDI child is not maximized. When the MDI child
is maximized, the title will restore to the "AppTitle - [DocTitle]"
format.

I also Googled for the answer but did not find one.
I hope someone here knows how to solve it.

You can change your title by SetWindowText or override CMDIChildWnd::OnUpdateFrameTitle. The title is changed in the fuction. Your function should look like this:

GetMDIFrame()->OnUpdateFrameTitle(bAddToTitle);

if ((GetStyle() & FWS_ADDTOTITLE) == 0)
   return;    // leave child window alone!

CDocument* pDocument = GetActiveDocument();
if (bAddToTitle)
{
   TCHAR szText[256+_MAX_PATH];
   CString strTitle;

   strTitle.LoadString(IDR_MAINFRAME);

   if (pDocument == NULL)
      lstrcpy(szText, m_strTitle);
   else
      lstrcpy(szText, pDocument->GetTitle());

   if (m_nWindow > 0)
      wsprintf(szText + lstrlen(szText), _T(":%d"), m_nWindow);

   // set title if changed, but don't remove completely
   lstrcat(szText, " - ");
   lstrcat(szText, (char *)((LPCTSTR) strTitle));
   AfxGetMainWnd()->SetWindowText(szText);
}

Besides that, you should get rid of the main frame's OnUpdateFrameTitle function.


How can I make sure whether a pointer is still good? (top)  

Thread:

NittiLin is working with many dialogs. For this purpose, he is using an array to save the pointers of that dialogs. After usage, he loops through the array of dialogs. Here, he needs to check whether the pointers to the dialog are still valid or not. Can this be done?

I have 'A' dialog, which creates multiple modeless 'B' dialogs. 
I use GetDesktopWindow() as 'B' dialogs' parent for some reason. 

'A' dialog saves pointers of 'B' dialogs to a CArray in order to do 
something when user closes 'A' dialog.

The user can delete 'B' dialogs at any time before closing 'A' dialog. 
Since 'A' is not B's parent, I don't know how to notify 'A' dialog 
to remove the pointer from CArray if user deletes any of the 'B'
dialogs.

However, so far, program works fine without any problems. 

My question is:

1) If the user deleted a 'B' dialog, but its pointer still exists
in 'A' dialogs' CArray. When 'A' dialog loops though CArray and got
that pointer and use that to call some functions on B dialogs, will
it cause any problems?

2) How do I make sure if the pointers on CArray are still good to use?

The answer to the first question is: Yes, it will cause problems. The answer to your second question is that you should use a std::map instead of a CArray or a linked list. This way, dialog A generate keys when modeless dialogs are created. The modeless dialog then can use this key and the pointer to dialog A to be removed from the map when it is closed.


How do I recognize a STOP in Windows Services? (top)  

Thread:

mattl is working with services. He needs to recognize that a STOP has been requested, so that he can shut down the service in a clean manner.

I am writing a Service in 'C' that has lots of database activity.
When the user requests to STOP the Service (within the Service
Control Manager) the program simply halts. It does not matter what
it is in the middle of doing.

How do I recognize that a STOP has been requested? I need this so I
can shut down the service gracefully.

There is a nice example of a Service posted on this Web site and it
shows how to pause or stop a servicem but both of these happen
instantly without any warning.

A STOP request for service looks like getting a SERVICE_CONTROL_STOP control message via its control handler routine. The service that has been requested to stop itself has to set its state to SERVICE_STOP_PENDING to initiate a cleanup procedure and, after cleanup, ends it has to set its state to SERVICE_STOP, notifying the system it's done. It has about 30 seconds (because SERVICE_CONTROL_STOP has been emitted) to do it before the system will decide to kill it as a hanging process. If the service needs more than 30 seconds, it has to use a checkpoint mechanism.




About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 10, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Modern mobile applications connect systems-of-engagement (mobile apps) with systems-of-record (traditional IT) to deliver new and innovative business value. But the lifecycle for development of mobile apps is also new and different. Emerging trends in mobile development call for faster delivery of incremental features, coupled with feedback from the users of the app "in the wild". This loop of continuous delivery and continuous feedback is …

  • The explosion in mobile devices and applications has generated a great deal of interest in APIs. Today's businesses are under increased pressure to make it easy to build apps, supply tools to help developers work more quickly, and deploy operational analytics so they can track users, developers, application performance, and more. Apigee Edge provides comprehensive API delivery tools and both operational and business-level analytics in an integrated platform. It is available as on-premise software or through …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds