oleinitialize

I am writing a simple web editor like dreamweaver, I used the TWebBrowser as the editing component. When I run the program, the webbrowser component's design mode has already been set to on, and I can also edit the html elements in my webbrowser window now.

While one problem I found was that I could not move the element by drag and drop. When I select an element in the window and then hold the left mouse button down, then try to move the element to another place by drag the frag of this element, but fail. I found the mouse cursor changed as a crNo shap.

Since I used to use this function in Delphi, I check my codes carefully and found nothing wrong. Late I found out a program I wrote before, and make comparation between these two progrmas many times. Finally I found, I forget to call the OleInitialize function which I remember I already wrote in the form create event.

So, to add these 2 functions, I make my program works on drag and drop editing mode.
OleInitialize(nil);
OleUninitialize;

You could also see carefully specification to these 2 functions in article TCppWebBrowser/TWebBrowser has been set to the edit mode, but the copy and paste do not work .

 

This is because you do not call the OleInitialize function. As the OLE operation is not thread-safe, so operation to the memory will cause failure or error. OleInitialize ensure that the concurrent modules run in an STA mode (that is, a single-threaded mode)  to avoid the sharing of resources error. Therefore, in a program's starting function WinMain, we should make some changes as following.
WINAPI WinMain (HINSTANCE, HINSTANCE, LPSTR, int)
(
    OleInitialize (NULL);
    try
    (
       ... ... //Normal statements
    ) catch (...)
    (
       ... ... //Normal statements
    )
    OleUninitialize ();
    return 0;
)

In a C-Builder program, you could also call the OleInitialize function in the main form's OnCreate event, like below mode. Last, call the OleUninitialize function when the form destroy.

void __fastcall TForm1::FormCreate(TObject *Sender)
{
    OleInitialize(NULL);   
}

void __fastcall TForm1::FormDestroy(TObject *Sender)
{
    OleUninitialize();   
}

In a Delphi program, is in a same way.