|
TYX Corporation
Binary I\O dll resource with MFC tutorial This document
will help with the making of a Binary input-output DLL ATL/COM 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 other Studio versions is not
guarantied. Introduction: First we will address the issue of
what to do with the dll 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 with an exe 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
less 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 the GUI 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 a dll with MSVC 6++ Combining ATL and MFC is not totally straightforward 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 ATL resource with MFC support, dialog based with one toggle button: · From File/New…, select an ATL COM Application and select a project name ComDll2 as seen below:
· Click on OK ·
Select Dynamic Link Library (DLL) and Support MFC.
· Click on Finish. · Click OK on the following window:
· If we look at the image below, we can see what the project should look like.
· Go into the Workspace and select the ResourceView Tab.
· Right click on ComDll2 resources and select insert:
· Click on Dialog and select New. · You will now have a the following window:
· Left click on the the OK and press delete. Left click on the Cancel button and press delete. · In the Controls window, select the check box (square item with an x in it). Left click on it and drag it onto the Dialog box. You may resize the Dialog box at will. You should have the following:
· 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:
· Go into Project/Settings… and you’ll see the following window
· Select All configurations for Settings For: and Use MFC in a Static Library for Microsoft Foundation Classes: as seen below.
· Click on OK. · Build your project by going into Build/Build ComDll2.dll or press F7. Your project should build without errors. · Now, you can write click on the dialog box window and select Class Wizard. You will see the following window and click OK by accepting the default:
· You will now be asked to chose a name for the new Dialog class and type in CComDllDlg:
· Press on OK · You will now 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 click on BN_CLICKED in the Messages: Window.
· Click on the Add Function button. A Add Member Function will open.
· 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. 1.2 Now, we will add the ATL environment: · Go to Insert and select New ATL Object. · Now you should see 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 ComDllRes. Don’t chose “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 ComDll2.ComDllRes (dll 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 as seen below. These options make more sense for those that are familiar with COM.
· Click on OK. · If you get the following window:
· Click on Yes. If not, just proceed. · 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 CComDllRes as seen below.
· After right clicking on CComDllRes, 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 AddTypelib…
· 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 Stutio that is older than 1.10.x.
· In the following window, you need to check IIOResouce and then click on OK. You may chose to select at this time both IIOResource and IBinaryResource, but it may cause problems.
· If you get an error message:
· Close the project. Delete the Debug folder, and any of the files that may be present with the following extensions: ncb, opt, plg, aps and clw and try again. · You need to repeat this for IBinaryResource (for text IO resources) if you haven’t done so already, by repeating the steps above starting at right-clicking on CComDllRes. · If you are still having problems trying to add the interfaces, you can make sure that the ComDllRes.h contains the content of the file below. · Now you will need to modify the source of what has been made available to you. In our case, double click on CComDllRes, or C<short name> as shown below:
//
ComDllRes.h : Declaration of the CComDllRes #ifndef
__COMDLLRES_H_ #define
__COMDLLRES_H_ #include
"resource.h" // main
symbols #import
"c:\usr\tyx\com\rtsio.dll" raw_interfaces_only, raw_native_types,
no_namespace, named_guids ///////////////////////////////////////////////////////////////////////////// // CComDllRes class
ATL_NO_VTABLE CComDllRes : public
CComObjectRootEx<CComSingleThreadModel>, public CComCoClass<CComDllRes,
&CLSID_ComDllRes>, public ISupportErrorInfo, public IDispatchImpl<IComDllRes,
&IID_IComDllRes, &LIBID_COMDLL2Lib>, public IBinaryResource, public IIOResource { public: CComDllRes() { } DECLARE_REGISTRY_RESOURCEID(IDR_COMDLLRES) DECLARE_PROTECT_FINAL_CONSTRUCT() BEGIN_COM_MAP(CComDllRes) COM_INTERFACE_ENTRY(IComDllRes) COM_INTERFACE_ENTRY(IDispatch) COM_INTERFACE_ENTRY(ISupportErrorInfo) COM_INTERFACE_ENTRY(IBinaryResource) COM_INTERFACE_ENTRY(IIOResource) END_COM_MAP() //
ISupportsErrorInfo STDMETHOD(InterfaceSupportsErrorInfo)(REFIID
riid); // IComDllRes 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
//__COMDLLRES_H_ For complete information on each one of those functions, please refer to the TYX website or the updated online help. · 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.
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 dll 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 16 or 17: public
IIOResource in order to avoid compilation problems. Make sure that you also delete the comma at the end of line 16 if public IIOResource is on line 17.
STDMETHOD(get_Eof)(VARIANT_BOOL * pVal) { if (pVal == NULL) return E_POINTER; *pVal = VARIANT_TRUE; //
Added return S_OK; //
Changed } ·
At the end of the CComDllRes
class definition, you need to make the dialog a member variable so that it can
be accessible throughout the resource. One place to put it would be on line
116. CComDllDlg theDlg; For the
above to build properly, you then need to include the header file that has the
definition for the class above. On line 7, you should now add this include: #include
"ComDllDlg.h" · 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: //
IBinaryResource STDMETHOD(Read)(LONG lType, VARIANT *
pVal) { if (pVal == NULL) return E_POINTER; pVal->vt = VT_I4; // Added pVal->lVal =
(int)theDlg.IsDlgButtonChecked(IDC_CHECK1); //
Added return S_OK; //
Changed } STDMETHOD(Write)(LONG lType, VARIANT val) { if(val.lVal == 1) // Added theDlg.CheckDlgButton(IDC_CHECK1,
BST_CHECKED); // Added else if (val.lVal == 0) // Added theDlg.CheckDlgButton(IDC_CHECK1,
BST_UNCHECKED); // Added else // Added return E_FAIL; //
Added |