Develop ActiveX controls using ATL

Source: Internet
Author: User
Tags visual studio 2010

 

This article describes the complete process of developing an ActiveX control using ATL.

1. Create a project

Click "new project…" on the start page ...", Select the "ATL Project" project under the "ATL" category. The project name is "Calculator ". In the Project Wizard that appears later, use the default configuration.

2. Add controls

Right-click the project in solution manager, select "add" and "class", and select the ATL control type under the ATL category in the add class dialog box. Click "add". The Add ATL control wizard is displayed.

In step 2 of the wizard, select "dual" as the interface type to prepare the control support events. In the support options, select the "connection points" check box.

The interface for selecting the interface to be implemented by the control appears. In addition to the implementation added by default by Vs, the IObjectSafety interface is added, this interface can be used to prevent insecure scripts that run in IE when the control is used.

 

 

3. Add and implement controls

In the Class View window, right-click the ic1c interface and select "add" and "add method ...", Assume that we implement an addition operation, name the method "add", and then add the parameter:

You must note the processing of the returned values. Set the parameter type to double * and select the "retval" check box.

After the Wizard is complete, vs automatically adds an empty implementation of this method to calc. cpp. The modified method code is:

STDMETHODIMP CCalc::Add(DOUBLE a, DOUBLE b, DOUBLE* result)
{
*result = a + b;

 

return S_OK;
}

 

Test this method:

Because you only call this control for addition operations and do not need to display the interface of the control, you can delete other code in the ondraw method automatically generated by vs before testing the control, return s_ OK directly.

Modify the HTM automatically generated by vs for testing to test the addition method. The modified HTM code is as follows:

<HTML>
<HEAD>
<TITLE>ATL 8.0 test page for object Calc</TITLE>
</HEAD>
<BODY>

 

<OBJECT ID="Calc" CLASSID="CLSID:59443E6F-7B99-4F75-A7AF-6FEE5B8208CD"></OBJECT>

<input type="button" value="Add" onclick="add();" />

<script type="text/javascript">
function add() {
var calc = document.getElementById('Calc');
var result = calc.Add(2, 3);
alert(result);
}
</script>

</BODY>
</HTML>

 

Click "add:

4. Add events for controls

Assuming that the Control performs a very complex operation, in order to call the operation without blocking the caller thread, the operation can be completed asynchronously. When the Control completes the operation, it must notify the caller. In this case, an event is required.

First, add an Asynchronous Method addasync to call the addition operation according to the method in step 3, and then add the operation completed event addcompleted to the control.

In the Class View window, right-click the _ icalcevents interface and select "add" and "add method ...", Add the addcompleted method in the add method wizard, as shown in:

In the Class View window, right-click the ccalb class and select "add" and "add connection point ...", In the pop-up implementation connection point window, implement the _ icalcevents interface.

 

After the Wizard is complete, Vs will automatically generate the basic framework for us, including the method fire_addcompleted that triggers the event. We only need to add an operation in the addasync method and call the fire_addcompleted code at the end of the operation:

STDMETHODIMP CCalc::AddAsync(DOUBLE a, DOUBLE b)
{
double result;
result = a + b;
Fire_AddCompleted(&result);

 

return S_OK;
}

 

Add the code for asynchronous computing to the webpage for testing (the added JavaScript code is as follows), and we should be able to get the desired effect.

<script type="text/javascript">
function addAsync() {
var calc = document.getElementById('Calc');
calc.attachEvent("AddCompleted", OnAddCompleted);
calc.AddAsync(3, 4);
}

 

function OnAddCompleted(result) {
alert(result);
}
</script>

 

V. ActiveX control events and Multithreading

Careful readers will surely find that the so-called "Asynchronous" in Step 4 is false: although the caller is notified by an event after the computation, because the computation is performed on the main thread, therefore, the call process is still synchronized. Step 4 only demonstrates the simple usage of events. If Asynchronization is implemented, multithreading must be used in the control.

When using multithreading in ActiveX controls, note that the event (that is, the Fire_XXXX call) must be performed in the window thread. Otherwise, the event cannot be processed by the ActiveX control container. If the thread that executes the code when an event occurs is not a window thread. You should use PostMessage or SendMessage to notify the window thread and execute Fire_XXXX in the message processing function. To use the message mechanism of the control, set the m_bWindowOnly field to TRUE in the construcc.

The following is some sample code after multithreading:

STDMETHODIMP CCalc::AddAsync(DOUBLE a, DOUBLE b)
{
m_a = a;
m_b = b;

 

_beginthreadex(NULL, NULL, AddMethod, this, NULL, NULL);

return S_OK;
}

unsigned __stdcall CCalc::AddMethod(LPVOID arg)
{
CCalc* pThis = (CCalc*)arg;
pThis->m_result = pThis->m_a + pThis->m_b;

pThis->SendMessage(WM_ADDCOMPLETED);

return 0;
}

LRESULT CCalc::OnAddCompleted(UINT /*uMsg*/, WPARAM /*wParam*/, LPARAM /*lParam*/, BOOL& /*bHandled*/)
{
Fire_AddCompleted(&m_result);

return 0;
}

 

So far, a simple ActiveX control has been developed. For more information about ActiveX Control packaging and deployment, see develop ActiveX Control-control packaging and deployment using ATL.

The Development Environment used in this example is Visual Studio 2010.

 

Source: http://www.cnblogs.com/chinadhf/archive/2010/09/03/1817336.html

 

[Recommended read] http://msdn.microsoft.com/en-us/library/599w5e7x (VS.80). aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.