1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

CoCreateInstance in DLL

Discussion in 'C' started by peteonthenet, Sep 20, 2012.

  1. peteonthenet

    peteonthenet New Member

    Joined:
    Sep 20, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    i have created one dll to connect to the com object of one application and call a method of the com. Its working fine. but with one issue. Every time dll is access it keeps creating new instance, so i to avoid this i created global variables, but seems its now working.

    connectApi - this function to be called only once to create com instance
    sendOrder - this function will be called repeatably as need be which shud use the com object pointer created by the connectApi function.


    here is the code:
    Code:
    IPlusapi *pNestApp;
    HRESULT hr;
    
    
    BOOL APIENTRY DllMain( HANDLE hModule, 
                           DWORD  ul_reason_for_call, 
                           LPVOID lpReserved
                         )
    {
    
        return TRUE;
    }
    
     int __stdcall connectApi(char* apiname)
    {
        CoInitialize(NULL);
    
        const CLSID clsid = {0x91741D4F, 0x948B, 0x46A0, {0xA2,0xCC,0x0B,0x92,0x5c,0x4f,0x5e,0x17}};
        hr = CoCreateInstance(clsid,NULL, CLSCTX_LOCAL_SERVER, IID_IPlusapi, (void **)&pNestApp);
    
        if(FAILED(hr)) {
            MessageBox(NULL,"Nest Plus API Not registered properly","ERROR",NULL);
            return(-1);
        }
        else {
             pNestApp->SetObjectName(apiname);
             
             return(1);
        }
         
    }
    
    
    
    void __stdcall sendOrder(char* type,char* refno,char*exch,char* sym,char* valid, char* ordertype,
                                         long qty,double price,double triggerprice,long disqty,char* prodtype,char* clientid)
    {
        
        if(SUCCEEDED(hr))
        {
            pNestApp->PlaceOrder(type,refno,exch,sym,valid,ordertype,qty,price,triggerprice,disqty,prodtype,clientid);
          
        }
    }
     
    Last edited by a moderator: Sep 20, 2012
  2. peteonthenet

    peteonthenet New Member

    Joined:
    Sep 20, 2012
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Can anyone please help ??
     
  3. sirama

    sirama New Member

    Joined:
    Jun 18, 2012
    Messages:
    7
    Likes Received:
    2
    Trophy Points:
    0
    Occupation:
    Systems Er.
    Home Page:
    Call ConnectApi from DllMain
     

Share This Page