Click here to Skip to main content
15,860,861 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i'm developing ole automation application. while converting VBA code to C++.

Dim back As HYSYS.BackDoor
above "BackDoor" is OLE Automation's class type.

variable back is simply declared in VBA , but c++ is not.

how to declare class type variable in ole automation ?

i don't know how to solve this problem....

CoCreateInstance, CoGetObject, Cogetclassobject...

i used CoCreateInstance when application initializing. and when i got application object, i used CoGetObject.

when i declare external(ole automation server's class) class variable, what use method, function, variable ?

in VB

VB
Dim hyapp As HYSYS.Application
Dim hycase As HYSYS.SimulationCase
Dim hyfs As HYSYS.Flowsheet

Dim back As HYSYS.BackDoor
Sub main()
    Set hyapp = CreateObject("HYSYS.application")
    Set hycase = GetObject("C\\test.hsc")
    Set hyfs = hycase.Flowsheet

    Call GetHyObj(hyfs)
End Sub
Function GetHyObj(hyfs As Flowsheet)
    For Each subOperation In hyfs.Operations("digitalop")
        Set back = subOperation    // autocasting data type 
        Set hyrv = back.BackDoorVariable(":selection.501").Variable
    Next
End function


conver to C++

C++
class CMSHysys //header
{
    public:
    IDispatch*  m_hyApp;
    IDispatch*  m_hyCase;
    IDispatch*  m_back;
    IDispatch*  m_hyfs;
}
void CMSHysys::Initialize(bool bVisible) 
// == Set hyapp = CreateObject("HYSYS.application")
{
    CoInitialize(NULL);
    CLSID clsid;

    m_hr = CLSIDFromProgID(L"Hysys.Application", &clsid);
    if(SUCCEEDED(m_hr))
    {
        m_hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&m_hyApp);
        if(FAILED(m_hr)) m_hyApp=NULL;
    }
}

C++
HRESULT CMSHysys::Connect(LPCTSTR szFilename) //.cpp
//==Set hycase = GetObject("C\\test.hsc")
{  
    m_hr = CoGetObject(szFilename, NULL,IID_IDispatch,(void **)&m_hyCase);

    if(FAILED(m_hr))  
    {
        printf("[-] connect fail\n"); 
    }
    else
    {
        printf("[+] connect ok\n"); 
    }

    VARIANT result;
    VariantInit(&result);  
    m_hr=OLEMethod(DISPATCH_PROPERTYGET, &result, m_hyCase, L"Flowsheet", 0);
    m_hyfs= result.pdispVal;

    if(FAILED(m_hr)) 
    {
       printf("[-] flowsheet fail \n");
    }
    else
    {
       printf("[+] flowsheet ok\n"); 
    }
}

in above code, hyapp == m_hyapp , hycase == m_hycase and back == m_back.

hycase(m_hycase)and hyfs(m_hyfs) depend on hyapp(m_hyapp). back(m_back) is independent.

so, i do not know how to declare and use m_back... like VB

just i want to know how to declare variable (ole automation server's inner data type )
Posted

1 solution

I would go over an interface declaration like extending iDispatch. OLE works with known interfaces, so it must be declared. Or you write some mysterious code like:

C++
LONG doAction( char *action, void* data);


where action is some command and data is some memory which get proceeded. :-O


Bonus: Some inoffical tool which I found.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900