ATL establishes ActiveX control (3)

Sample code http://download.csdn.net/source/1458175

Controls will encounter many problems when they are used, so be patient when programming controls. I will briefly describe the problems I encountered :

     1. The security description of the control

But when we run this control, do we want to pop up a dialog box called unsafe interaction? This is it

This is very annoying. Usually when we put this webpage on the website and run it, the first dialog box will not pop up, but the second one cannot be removed like this. This is mainly because there is no security description for the control. In MFC 's OCX , we can add some operations during registration (OCX will not be discussed here, please leave me a message if necessary !) , but this ATL project is different from OCX , I also started I used it like an OCX , but it took a lot of effort, and I couldn't find my tired nose. Later I found a very good way. Very simple, just two lines of code,

The first step is to let the control class implement a safe interface, just inherit it. Add the code as follows:

public IObjectSafetyImpl<CPlayerCtrl,INTERFACESAFE_FOR_UNTRUSTED_CALLER  | INTERFACESAFE_FOR_UNTRUSTED_DATA >

The meaning of the following two parameters:

INTERFACESAFE_FOR_UNTRUSTED_CALLER - safe for scripting (安全描述)

     INTERFACESAFE_FOR_UNTRUSTED_DATA   - safe for initialization from data (安全初始化)

The second step is to add the security initialization event entry mapping, the code is as follows:

COM_INTERFACE_ENTRY(IObjectSafety)

Everything is OK , if you run it again, is there no more trouble. Ha ha.

Second, the control crashes when running in IE

         We know that many programs now use hooks, especially those like Xunlei, and there are also Jiangmin antivirus. Originally, their constructs did not affect us, but somehow they were hooked in three clicks. We went inside our control, and as a result, the soul of our control was hooked away and collapsed. . .

But as long as I reproduce the problem, I can use the code to debug it, it turns out that it is! Our control is written in ATL . In order to be called by the web page, we have implemented the IDispatch interface, but this interface is public, so anyone can call it as long as they get it . If there are some interfaces at this time, we don’t go further. Realization, so if the original code is not thorough at this time, then our control lifetime will be declared Over . Therefore, it is easy to deal with these situations. You only need to see which interface function is the problem, and then complete the interface and change it. Below I am a solution to a Thunder hook I encountered

     //In order to avoid that Xunlei’s hook will crash the plug-in, overload the function implementation of IDispatchImpl interface

     STDMETHOD(GetIDsOfNames)(REFIID riid, LPOLESTR* rgszNames, UINT cNames,    LCID lcid, DISPID* rgdispid)

     {

         if ( rgszNames [0] == NULL )//Add a judgment to prevent the crash

              return S_OK;

         return IDispatchImpl<IPlayerCtrl, &IID_IPlayerCtrl, &LIBID_PlayerLib,1,0>::GetIDsOfNames( riid,  rgszNames,  cNames, lcid,  rgdispid);

     }

3.             Objects or variables of static global life cycle cannot be used

When our control is in use, it must be contained by other containers, so the lifetime of the control will be determined by the container, and we can create multiple different control instances. But at this time, we should pay attention that if we use a global static object in the control, there is only one static object for all controls, and only one for the external container, and it can even be accessed. Therefore, when multiple controls are running at the same time, they will mess around. This control will be used for a while, and that one will be used for a while, so the control will either not run or it will collapse.

This situation is common in IE , because there are usually multiple objects in IE . So how should we solve it, the most direct and simple way is not to use static variables of the global period.

If you really want to use it, there is another way, that is to add a reference count to your control, as long as there is a control running, you can increase the count by one, and reduce it by one if one is missing. It happens that we may use the characteristics of global static variables to add a static variable to the control class, so that one is added to it in the constructor, and one is decremented in the destructor, so that if a load of controls detects that the reference is greater than 1 , then the control Don't run the kernel, so you can prevent multiple kernels from running at the same time and crashing.

If you want multiple cores to run at the same time without crashing, then you should replace static objects with NEW pointers.

Let's extend this situation a bit. It is conceivable that if we use the same symbol as the name of the file to be operated in the control, the same problem will occur. So the solution depends on your own choice.

 

Four,             IE controls in runtime only a relatively low privileges

For the safety of the system, Windows has reduced the operation of IE to a very low level, because most viruses are transmitted over the network , and direct network interaction with IE is very likely to bring malicious scripts and executables to the local. Therefore, the control IE runs as a part of IE , so the control authority will be very low. However , the security level in VISTA is improved, even ordinary applications will be subject to the employment system, and they must be approved by the administrator if they want to run. At this time, the authority of IE is extremely low, and the control cannot operate on the hard disk, cannot create or rewrite the data on the hard disk, and it is not allowed to operate the registry. We are done with this one, and there are many things that cannot be completed. One word "finished", ha ha

What shall we do? :-<In fact, it is easy to handle, but it is a little troublesome.

1.   What if we want to write a configuration file for the control?

Haha, it's simple, VISTA didn't completely limit your death, but left you a little bit. That is under our user temporary files, we can go and see what kind of bird stuff is under this file, whether it is white, black, flowery, or green. . . There are any birds! Haha, because Windows allows any program to operate on it, the controls can show their power here. You can create folders or delete files. It's up to you, whatever you want.

How can I find this place in the program? Simple.

theReturn = ::GetTempPath(MAX_PATH, lpBuffer);

The path of the folder is in lpBuffer .

2.   What if I want to operate the registry and I want to put a file in the system directory?

There is also a good way to do this. You can write a simple program, the main thing is to be small, in this program you can do whatever you want. So how do you use this program? It's simple. You put this program together with the DLL and files you want to use in your CAB package, and then you start the program in the control, so you want to do it Registration is possible, and the file you want to put in the system directory can also be copied to the destination with this program. O out.

Another problem is that you will encounter a problem when you start another program in the control. A dialog box may pop up to ask whether to execute it as an administrator. Just click OK.

  So far, the controls are almost covered. If you understand all of these, then you can say that you can build controls with ATL. If you have any other questions, just leave me a message.

Guess you like

Origin blog.csdn.net/wanghaisheng/article/details/4323327