Call COM-DLL dynamically

I have an application in VC ++ that needs to execute the function provided to it (the name of the function entered in the text field) from the DLL COM-library (the name of the file provided in another text field).

I saw the code to load the Win32 library using LoadLibraryand GetProcAddress.

How can this be done for a COM-DLL file (created in Visual Basic 6.0)? Is there a link where I can get more information?

+3
source share
2 answers

If the component you are calling supports IDispatch(which is likely if it was created in VB), you can use late binding to dynamically call COM interface methods.

For instance:

IDispatch *pDispatch;
// Assumes pUnknown is IUnknown pointer to component that you want to call.
HRESULT hr = pUnknown->QueryInterface(IID_IDispatch, reinterpret_cast<void **>(&pDispatch));
if(SUCCEEDED(hr))
{
    DISPID dispid;
    // Assumes sMethodName is BSTR containing name of method that you want to call.
    hr = pDispatch->GetIDsOfNames(IID_NULL, &sMethodName, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
    if(SUCCEEDED(hr))
    {
        // Assumes that the method takes zero arguments.
        VARIANT vEmpty;
        vEmpty.vt = VT_EMPTY;
        DISPPARAMS dp = { &vt, 0, 0, 0 };
        hr = pDispatch->Invoke(dispid, IID_INULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &dp, 0, 0, 0);
    }

    pDispatch->Release();
}

This example retrieves the DISPIDspecified method from IDispatch::GetIDsOfNames(), then calls this method, passing DISPIDin IDispatch::Invoke().

For clarity, I suggested that there are no arguments for the method you want to call, but you can change the DISPPARAMSstructure that is passed Invoke(), if any.

+2
source

Here is an example of C ++ LoadLibraryAnd GetProcAddressfor COM COM library How to use COM-DLL with LoadLibrary in C ++

0
source

Source: https://habr.com/ru/post/1735393/


All Articles