mahdi Lotfi
mahdi Lotfi

Reputation: 320

C++ component object model

i want set a pointer to the dispatch for software automation with c++ my code is :

::CLSIDFromProgID(OLESTR("SGNSAutomation.SGNSApplication"), &clsid);
IID iid;

  HRESULT hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, 
  IID_IDispatch, (LPVOID*)&pWMPDispatch);
  IDispatch * pdisp = (IDispatch *)NULL;
  DISPID dispid;
  OLECHAR * Name = OLESTR("openCase");
 HRESULT hresult =pdisp->GetIDsOfNames(IID_NULL, &Name,1,LOCALE_SYSTEM_DEFAULT,&dispid);

but an error occurs in last line : Unhandled exception at 0x008B4AD7 in Win32Project1.exe: 0xC0000005: Access violation reading location 0x00000000.

how i should solve this problem? i think it is because i don't set pdisp pointer and i don't know how set it. please help me thank you

Upvotes: 1

Views: 225

Answers (1)

Michael Gunter
Michael Gunter

Reputation: 12811

You didn't show the declaration of pWMPDispatch, but I bet it's an IDispatch*. It should be, because you're using IID_IDispatch in your CoCreateInstance call. So you don't need the other variable pdisp. Just use pWMPDispatch. Also, make sure you check every HRESULT return code. I think your code should look something like this

HRESULT hr = ::CLSIDFromProgID(OLESTR("SGNSAutomation.SGNSApplication"), &clsid);
if (FAILED(hr)) {
    // handle error here by returning or throwing
}

hr = CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IDispatch, (LPVOID*) &pWMPDispatch);
if (FAILED(hr)) {
    // handle error here by returning or throwing
}

DISPID dispid;
LPOLESTR Name = OLESTR("openCase");
hr = pWMPDispatch->GetIDsOfNames(IID_NULL, &Name, 1, LOCALE_SYSTEM_DEFAULT, &dispid);
if (FAILED(hr)) {
    // handle error here by returning or throwing
}

Upvotes: 2

Related Questions