An ATL Sample using VS2005<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

 

1.        Create an ATL COM

1). Start VS2005, new a project, select “Visual C++”->”ATL”->”ATL Project”, specify the name(“SimonATLSample”) and the directory, use the default setting and click “Finish”.

2 projects(SimonATLSample & SimonATLSamplePS) are created by VS. Here, SimonATLSample is the main project while SimonATLSamplePS is proxy/stub project, which may be useful when out-of-process marshalling (I have not understood them well yet). Build the project, it should be clean build, and during the build, VS will write some info into the registry, such as:

Mainly, they’re AppID and TypeLib Info

[HKEY_CLASSES_ROOT\AppID\{BBA913E6-424A-4610-870E-64E0DAE31B3C}]

@="SimonATLSample”

                   [HKEY_CLASSES_ROOT\AppID\SimonATLSample.DLL]

"AppID"="{BBA913E6-424A-4610-870E-64E0DAE31B3C}""

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}]

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}\1.0]

@=" SimonATLSample 1.0 Type Library"

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}\1.0\0]

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}\1.0\0\win32]

@=" [SimonATLSample.dll’s path]"

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}\1.0\FLAGS]

@="0"

[HKEY_CLASSES_ROOT\TypeLib\{A6551417-E0D4-4761-9D84-D984BB5A13AB}\1.0\HELPDIR]

     @=""

2). We need to add COM interface and class to the project. Open “Class View”, right click on the “SimonATLSample” project, “Add”->”Class”, and select “Visual C++”->”ATL”->”ATL Simple Object”, click “Add”. On the dialog, we specify the “ShortName”: SimonMathUtil, other fields will be filled automatically, “Finish”

<?xml:namespace prefix = v ns = "urn:schemas-microsoft-com:vml" />

 
It will generate new files(SimonMathUtil.h&cpp, SimonMathUtil.rgs), the SimonATLSample.idl are also modified accordingly. Save all the changes.

 
3). The “SimonMathUtil” is empty, we need to add useful method to the interface and class. “Class View”->Right click on “ISimonMathUtil”->”Add”->”Method”, on the dialog, specify the method signature, “Finish”

It will modify the SimonMathUtil.h&cpp, and the idl file, save all the changes. Go to SimonMathUtil.cpp, add the code like below:

 
STDMETHODIMP CSimonMathUtil::Add(LONG op1, LONG op2, LONG* result)

{

     // TODO: Add your implementation code here

     *result = op1 + op2;

     return S_OK;

}

Build the solution. During the build, it will write ISimonMathUtil and CSimonMathUtil info into the registry. Every interface and class has its own guild, IID and CLSID. One class may have several interfaces.

[HKEY_CLASSES_ROOT\Interface\{77103636-D4AA-42B9-BD12-80F057F432E0}]

@="ISimonMathUtil"

 

[HKEY_CLASSES_ROOT\Interface\{77103636-D4AA-42B9-BD12-80F057F432E0}\ProxyStubClsid]

@="{00020424-0000-0000-C000-000000000046}"

 

[HKEY_CLASSES_ROOT\Interface\{77103636-D4AA-42B9-BD12-80F057F432E0}\ProxyStubClsid32]

@="{00020424-0000-0000-C000-000000000046}"

 

[HKEY_CLASSES_ROOT\Interface\{77103636-D4AA-42B9-BD12-80F057F432E0}\TypeLib]

@="{587A0882-5469-4D50-BF8D-F6F4384426A2}"

           "Version"="1.0"

 

                               [HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}]

@="SimonMathUtil Class"

"AppID"="{C1AA92E7-482B-4D6F-A5D8-E42AB8839026}"

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}\InprocServer32]

@="D:\\Code\\SimonATLSample\\SimonATLSample\\Debug\\SimonATLSample.dll"

"ThreadingModel"="Apartment"

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}\ProgID]

@="SimonATLSample.SimonMathUtil.1"

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}\Programmable]

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}\TypeLib]

@="{587A0882-5469-4D50-BF8D-F6F4384426A2}"

 

[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\CLSID\{75AD0D34-2D47-4B5A-B21E-49BBADC98E69}\VersionIndependentProgID]

@="SimonATLSample.SimonMathUtil"

 
2.        Create a testing program

We just need the tlb file to create the testing program. Create a C++ Win32 console program, and write the testing code like below:

 

#include "stdafx.h"

 

#import "..\..\SimonATLSample\SimonATLSample\Debug\SimonATLSample.tlb" named_guids

 

#include "Debug\SimonATLSample.tlh" //This is the generated tlh file by the imported tlb above

 

using namespace SimonATLSampleLib;

 

int _tmain(int argc, _TCHAR* argv[])

{

     HRESULT hr;

     ISimonMathUtil *pISimonMathUtil;

 

     hr = CoInitialize(NULL);

 

     if(SUCCEEDED(hr))

     {

        hr = CoCreateInstance(__uuidof(SimonMathUtil), NULL, CLSCTX_INPROC_SERVER, /*IID_IKegleTest*/__uuidof(ISimonMathUtil), (void**)&pISimonMathUtil);

         if(SUCCEEDED(hr))

         {

              long retVal;

              hr = pISimonMathUtil->Add(1, 2, &retVal);

 

              hr = pISimonMathUtil->Release();

         }

     }

 

     CoUninitialize();

 

     return 0;

}

 

Here, I still have some questions, the tlh header file is created after importing the tlb, can I specify the location of the this tlh file? How to do that?