This document will help with the making of a text input-output interface

TYX Corporation

Productivity Enhancement Systems

Reference

TYX_0051_6

Revision

1.1b

Document

PawsIOHowTo3.doc

Date

October 9, 2003

 

 

 

Binary I\O exe resource with MFC tutorial


 

 

 

 

 

This document will help with the making of a Binary input-output interface with MFC.

 

Studio version used: 1.20.0

 

Requirements: Studio 1.10.x or above. Backward and forward compatibility between the COM build with one version of the Studio library and the Studio version is not guarantied.

 

 

Introduction: First we will address the issue of what to do with the exe that we wish to link to the Atlas with a Binary IO and MFC. Other documents will address the issue of using the support of Text IO and Binary IO resources without the usage of GUI within the COM environment.

Then we will see the Atlas and what it takes with a 416 environment to make use of it.

 

 

Dll versus Exe:

1.       Advantage of Dlls versus Exe:

·         The Dll uses fewer resources than the Exe.

·         The Dll can easily remain on top of the Wrts.

·         The accelerators are directly passed on to the Wrts without additional code. When the  Exe has the focus, it will need some code in order to pass on the accelerator destined for the Wrts.

·         The message loop for painting… is taken care of by the Wrts. For the Exe, the code needs to be placed into the Exe which may be done by the wizard to some extent.

·         The Dll will be slightly faster than the Exe. This will however only make a difference for repeated transfer of a large amount of data.

2.       Advantage of the Exe versus Dll:

·         The Exe is more isolated than the Dll and if it crashes, it will not affect the Wrts. This may be an issue if the Exe links to code that is not stable.

·         The Exe can be moved in front or behind the Wrts at will.

·         The Exe can be run remotely.

 

 

 


1          When generating an exe with MSVC 6++

 

Combining ATL and MFC is not totally straight forward with the wizards. It is easier to deal with fixing the ATL problems on top of a MFC project than the other way around so we are going to do just that:

First start an MFC project and then try to add the ATL  to it.

1.1        Starting a simple MFC project, dialog based with one toggle button:

·         From File/New…, select an MFC  AppWizard (exe) and select a project name as seen below:

 

 

·         Click on OK

·         Then select a Dialog Based project as seen below and click on Next:


 

 


·         We will here uncheck the About box check box because we will not be using it. It is here that you will decided whether you want to include ActiveX Controls. We will not be using it in our example. Uncheck ActiveX Controls and then on Next:


 


·        
In the window below, we will want to link the MFC library statically for greater independence of our exe and click on Next:

 


·        

Click on Finish on the window below after accepting the default class name and OK on the window that will appear after that.:


 


·         If we look at the image below, we can see what the project should look like.


 


·         We will go into the dialog box and delete the OK button and the Cancel button. All you need to do is to left click on the buttons and press the delete button on your keyboard. We then want to click on the TODO caption and delete it as well. We will want to resize the dialog window to something sufficient for one button as seen below:


 


·         Now we will add a check-box. Click on the check box and drag it onto the dialog box. You will see the following below:

 

 

·         Right click on the checkbox and select the Properties. You will see the windows below and change the caption to Button.

 

 

·         In the Style tab, select Push-like check box.

 

 

·         In the Extended Styles, select the options that you see fit.

·         Then right click on the dialog box and select Properties. In Style, uncheck the title bar and close that window:

 

 

 

 


·         You will see now have the following project:

 


 


·         Build your project by going into Build/Build Proj6.exe or press F7. Your project should build without errors.

·         Now, you can write click on the dialog box window and select ClassWizard. You will see the following window:


 


·         This is where you may add any functionality related to clicking on the window. We will in our case only be interested in what happens when we click on the button. No implementation of that functionality is necessary in our case.

In the event that you wished to implement it, you will have to select IDC_CHECK1 on the Object Ids window.

Then you will double-click on BN_CLICKED in the Messages. This will have the same effect as adding a function to the message event BN_CLICKED.

 

·         You will see the window below:

 

 

·         Click on OK after allowing the default name to be chosen.

Now you have allowed the wizard to add the code to your project. Click on OK on the MFC ClassWizard window.

 


 

1.2        Now, we will add the ATL environment:

 

Note: Due to a known problem with MFC, it is possible that this step may be giving you some problems. In order to avoid those problems, we will need to clean up the files a little bit and proceed from there. If the process of adding the ATL object crashes, by cleaning it up once more, the second attempt is usually successful.

·         We will want to:

o        Close the MSVC Studio after saving the project workspace.

o        Delete the files that are in your project subfolder with the extensions: ncb, opt, plg, aps and clw and the Debug folder.

o        Open the project by double clicking on the file with the dsw extension.

o        Go to Insert and select New ATL Object.

 

 

o        Click on Yes

o        You may see the following window:

 

 

o        Click on OK. If you’re lucky, you’ll see the ATL Object Wizard window displayed below. If you do, proceed from there, otherwise, follow the next instruction.

o        Go into Build and select Clean.

o        Go into File and select Save Workspace.

o        Close the MSVC Studio.

o        Delete the files with the extension ncb, aps and opt and the Debug folder.

o        Double click on the file with the dsw extension.

o        Go to Insert and select New ATL Object.

o        Now you should see the following window:

 

·         From the Studio, select insert/New ATL Object… Select Yes on the following window:

 

·         Select Simple Object as seen below and click on Next.

 

 

·         You will then see the following window. In the Short name: box, enter the name of the ATL object that you wish to create. The other boxes will fill in by themselves. In this case, we chose the name ButtonRes. Don’t chose the name IOResource because it’s already used by the system and it will prevent you from building the COM exe.

Also, take note of the name in Prog ID: In this case Proj6.ButtonRes (exe name followed by the short name). This will be used as an entry in the Wrts options.

 


 


·         In the list of attributes, as shown below, all defaults can be acceptable. You may want to add the option of Support IsupportErrorInfo.

These options make more sense for those that are familiar with COM.

 


 


·         Click on OK.

 

·         Now we are back to the Studio.

 

·         Now you need to link the project to an interface, which in this case relates to the Wrts capabilities. In the workspace, you should right-click on the Class that starts with C<short name>, where short name is from the ATL Object Wizard Properties. In our case CButtonRes as seen below.

 


 


·         After right-clicking on CbuttonRes, Select Implement interface…

 

·         You will get the following message and then click on OK.

 

 

·         In the event that you see the following image instead of the previous one, press Add Typelib…

 


 


·         Select the RtsIO for Wrts input-output library and then click on OK. If you do not see RtsIO, it’s because you are using a version of the TYX Studio that is older than 1.10.x.

 

 

·         Make sure that the rtsio.dll is located in the location described at the bottom of the window above.

·         In the following window, you need to check IIOResouce and then click on OK.

 

 

·         Repeat the interface implementation for IBinaryResource (for text IO resources). For this, go back to right-clicking on the CButtonRes from ClassView and rather than selecting IIOResource in the Implement Interface window, select IBinaryResource as seen below.

 

·         In the event that you see this window below, clean the project as we’ve seen above, reopen the dsw project and make follow the instruction below. You will want to make sure that the code is the same as the one listed below.

 

 

·        
Now you will need to modify the source of what has been made available to you. In our case, double click on CbuttonRes, or C<short name> as shown below:

 

 

 

 

 


// ButtonRes.h : Declaration of the CButtonRes

 

#ifndef __BUTTONRES_H_

#define __BUTTONRES_H_

 

#include "resource.h"       // main symbols

#import "c:\usr\tyx\com\rtsio.dll" raw_interfaces_only, raw_native_types, no_namespace, named_guids

 

/////////////////////////////////////////////////////////////////////////////

// CButtonRes

class ATL_NO_VTABLE CButtonRes :

       public CComObjectRootEx<CComSingleThreadModel>,

       public CComCoClass<CButtonRes, &CLSID_ButtonRes>,

       public ISupportErrorInfo,

       public IDispatchImpl<IButtonRes, &IID_IButtonRes, &LIBID_Proj6Lib>,

       public IBinaryResource,

       public IIOResource

{

public:

       CButtonRes()

       {

       }

 

DECLARE_REGISTRY_RESOURCEID(IDR_BUTTONRES)

 

DECLARE_PROTECT_FINAL_CONSTRUCT()

 

BEGIN_COM_MAP(CButtonRes)

       COM_INTERFACE_ENTRY(IButtonRes)

       COM_INTERFACE_ENTRY(IDispatch)

       COM_INTERFACE_ENTRY(ISupportErrorInfo)

       COM_INTERFACE_ENTRY(IBinaryResource)

       COM_INTERFACE_ENTRY(IIOResource)

END_COM_MAP()

 

// ISupportsErrorInfo

       STDMETHOD(InterfaceSupportsErrorInfo)(REFIID riid);

 

// IButtonRes

public:

// IBinaryResource

       STDMETHOD(Read)(LONG lType, VARIANT * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(Write)(LONG lType, VARIANT val)

       {

              return E_NOTIMPL;

       }

// IIOResource

       STDMETHOD(Open)(BSTR bstrName, LONG lMode)

       {

              return E_NOTIMPL;

       }

       STDMETHOD(Close)()

       {

              return E_NOTIMPL;

       }

       STDMETHOD(Flush)()

       {

              return E_NOTIMPL;

       }

       STDMETHOD(Abort)()

       {

              return E_NOTIMPL;

       }

       STDMETHOD(Seek)(LONG lOffset, SHORT sOrigin)

       {

              return E_NOTIMPL;

       }

       STDMETHOD(get_name)(BSTR * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(get_Mode)(LONG * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(get_Size)(LONG * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(get_Position)(LONG * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(get_Eof)(VARIANT_BOOL * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

       STDMETHOD(get_State)(LONG * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

                    

              return E_NOTIMPL;

       }

};

 

#endif //__BUTTONRES_H_

 

For complete information on each one of those functions, please refer to the TYX website or the updated online help.  You can also look at the rtsio.dll with the OleView tool that is provided with your compiler.

 

·         What you will want to do first, is to change the exit code for all of those functions, from E_NOTIMPL which is the return for “Error, not implemented”, to S_OK for an ok return value.

  • For example: For the Open and Close functions, you will get:

 

   STDMETHOD(Open)(BSTR bstrName, LONG lMode)

   {

          return S_OK;  // Changed

   }

   STDMETHOD(Close)()

   {

          return S_OK;  // Changed

   }

 

Those particular functions are called by the Wrts upon loading and unloading the program with the Wrts. This is where you would put the code that you wish to see executed when the Wrts loads and unloads an Atlas program.

 

·         You will also want to delete the following line on line 17:

 

public IIOResource

 

in order to avoid compilation problems. Make sure that you also delete the comma at the end of line 16.

 

·         You will also want to return an end of file true statement in the get_Eof  method:

 

       STDMETHOD(get_Eof)(VARIANT_BOOL * pVal)

       {

              if (pVal == NULL)

                     return E_POINTER;

              *pVal = VARIANT_TRUE;                    // Added

              return S_OK;                      // Changed

       }

 

·         The code below is where the binary input and output are being handled. Some code has been added in order to achieve the goal that we have set ourselves in this example, which is to pass on and retrieve an integer. Typically, you will implement your own code as a function of your own requirements:

 

#include "Proj6Dlg.h" // Included at the top of file in order to allow the code

                      // beneath to build

 

// IBinaryResource

   STDMETHOD(Read)(LONG lType, VARIANT * pVal)

   {

          CProj6Dlg* pDlg = (CProj6Dlg*)AfxGetMainWnd();         // Added

 

          if (pVal == NULL)

                 return E_POINTER;

                

          pVal->vt = VT_I4;                                      // Added

          pVal->lVal = (int)pDlg->IsDlgButtonChecked(IDC_CHECK1);       // Added

 

          return S_OK;                                           // Changed

   }

   STDMETHOD(Write)(LONG lType, VARIANT val)

   {