0

Good day, everybady,

I work on Windows7 (64 bits) and try use COM / OLE object "iTunesApp Class". This object has installed with iTunes application. My code is following

  HRESULT hr;
  CLSID clsid;
  IiTunes *pIiTunes = nullptr;
  //Apple.iTunes
  CLSIDFromProgID(OLESTR("iTunes.Application.1"), &clsid);
  hr = CoCreateInstance(clsid, nullptr, CLSCTX_LOCAL_SERVER, __uuidof(IiTunes), reinterpret_cast<LPVOID *>(&pIiTunes));
  if (pIiTunes != nullptr)
  {
      VARIANT data[16];
      OLECHAR ver[4096] = L"vaneustroev@gmail.com";
      pIiTunes->Authorize(1, data, (BSTR*)ver);
  }

Then (pIiTunes->Authorize(1, data, (BSTR*)ver); ) I've got exception '...exception from address 0x000007FEFF4E4FCA (oleaut32.dll) ...Violation of access rights at address 0x000007FEFF4E4FCA...'

I don't know which parameters for pIiTunes->Authorize() I must set

1 Answers1

0

I don't know what is the value of parameters that must be set, but I know the types of these parameters.

First one is a int32, second is a VARIANT reference, third is a array of BSTR. VARIANTs must be initialized and cleared after use, BSTRs must be allocated (a BSTR is not a OLECHAR *) and freed after use.

So, beyond the real semantics of the method, you can call it like this:

VARIANT data;
VariantInit(&data); // undercovers, this will just zero the whole 16-bytes structure

// ... do something with data here

BSTR ver = SysAllocString(L"vaneustroev@gmail.com"); // you should check for null -> out of memory
pIiTunes->Authorize(1, &data, &ver);

// always free BSTRs and clear VARIANTS
SysFreeString(ver);
VariantClear(&data);

If you use Visual Studio, there are cool Compiler COM Support Classes that ease VARIANT and BSTR programming considerably, as you could rewrite all this like this:

_variant_t data;
_bstr_t ver = L"vaneustroev@gmail.com";
BSTR b = ver;
pIiTunes->Authorize(1, &data, &b);

Visual Studio also provides a library called ATL that has other wrappers. Using them is similar:

CComVariant data;
CComBSTR ver = L"vaneustroev@gmail.com";
BSTR b = ver;
pIiTunes->Authorize(1, &data, &b);
Simon Mourier
  • 132,049
  • 21
  • 248
  • 298