Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   CoCreateInstance in DLL (http://www.go4expert.com/forums/cocreateinstance-in-dll-t29122/)

peteonthenet 20Sep2012 09:52

CoCreateInstance in DLL
 
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);
     
    }
}


peteonthenet 21Sep2012 18:41

Re: CoCreateInstance in DLL
 
Can anyone please help ??

sirama 4Oct2012 16:22

Re: CoCreateInstance in DLL
 
Call ConnectApi from DllMain


All times are GMT +5.5. The time now is 03:24.