From: Thomas Bendrich on
Hello, I wrote an ActiveX control. This control have a mothod to load a
picture. This picture was loaded with OleLoadPicture that returns a IPicture
implemented object. I call the
QueryInterface(IID_IpictureDisp,(LPVOID*)&pPict) to receive a pointer to the
IPictureDisp object and return it. But when I call this function in
VisualBasic 6 (I drop it on a standard form) and call this method in VB
(e.g. Set
myPic=PicturListCtrl1.LoadPictureFromFile("C:/MyPictures/sample.jpg") I
became an error number 13 (type mismatch). What can I do? Who can help me?

Best regards and thanks

Thomas Bendrich

Here is the method I wrote:

LPPICTUREDISP CPicturListCtrl::LoadPictureFromFile(BSTR FAR* FileName)
{
CString strPath((char*)(FileName+sizeof(DWORD))); //BSTR have a DWORD on
begin.

LPSTREAM pstm = NULL;
LONG lSize;
HRESULT hr;

if (FAILED(hr = GetFileStream(strPath, &pstm, &lSize))) // GetFileStream is
a global method in the module
return NULL;

if(!pstm)
return NULL;

LPPICTURE pPict=NULL;
LPPICTUREDISP pPictDisp=NULL;

if(SUCCEEDED(::OleLoadPicture(pstm,lSize,FALSE,IID_IPicture,(LPVOID
*)&pPict)))
{
pstm->Release();
if(!pPict)
return NULL;

pPict->QueryInterface(IID_IPictureDisp,(LPVOID*)&pPictDisp);

pPict->Release();
}
else
return NULL;

return pPictDisp;
}


From: Thomas Bendrich on
Hi Asko,

thanks for your answer. I'm so happy, it works fine.

Best regards and thanks

Thomas Bendrich

"Asko Telinen" <asko.NOSPAM.telinen(a)jyu.REMOVETHIS.fi> schrieb im
Newsbeitrag news:%23qjDU9gqFHA.904(a)TK2MSFTNGP10.phx.gbl...
> Thomas Bendrich wrote:
>> Hello, I wrote an ActiveX control. This control have a mothod to load a
>> picture. This picture was loaded with OleLoadPicture that returns a
>> IPicture
>> implemented object. I call the
>> QueryInterface(IID_IpictureDisp,(LPVOID*)&pPict) to receive a pointer to
>> the
>> IPictureDisp object and return it. But when I call this function in
>> VisualBasic 6 (I drop it on a standard form) and call this method in VB
>> (e.g.
>> Set myPic=PicturListCtrl1.LoadPictureFromFile("C:/MyPictures/sample.jpg")
>> I became an error number 13 (type mismatch). What can I do? Who can help
>> me?
>>
>> Best regards and thanks
>>
>> Thomas Bendrich
>>
>> Here is the method I wrote:
>>
>> LPPICTUREDISP CPicturListCtrl::LoadPictureFromFile(BSTR FAR* FileName)
>> { CString strPath((char*)(FileName+sizeof(DWORD))); //BSTR have a DWORD
>> on begin.
>>
>> LPSTREAM pstm = NULL; LONG lSize; HRESULT hr;
>>
>> if (FAILED(hr = GetFileStream(strPath, &pstm, &lSize))) // GetFileStream
>> is a
>> global method in the module return NULL;
>>
>> if(!pstm) return NULL;
>>
>> LPPICTURE pPict=NULL; LPPICTUREDISP pPictDisp=NULL;
>>
>> if(SUCCEEDED(::OleLoadPicture(pstm,lSize,FALSE,IID_IPicture,(LPVOID
>> *)&pPict))) { pstm->Release(); if(!pPict) return NULL;
>>
>> pPict->QueryInterface(IID_IPictureDisp,(LPVOID*)&pPictDisp);
>>
>> pPict->Release(); } else return NULL;
>>
>> return pPictDisp; }
>>
>>
>
> If this is the method that implements your interface method directly it
> should
> return HRESULT, not a pointer to interface. All interface methods always
> should
> return HRESULT.
>
> in ILD/OLD file you define:
>
> [id(1), helpstring("method LoadPictureFromFile")] HRESULT
> LoadPictureFromFile([in] BSTR FileName, [out, retval] IPicture** pVal);
>
>
> And these methods implements the loading of picture (tested)
>
> HRESULT CPictureListCtrl::LoadFromBuffer(BYTE* pBuff, int nSize, IPicture
> **pPicture)
> {
> HRESULT hResult = S_OK;
>
> HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, nSize);
> void* pData = GlobalLock(hGlobal);
> memcpy(pData, pBuff, nSize);
> GlobalUnlock(hGlobal);
>
> IStream* pStream = NULL;
>
> hResult = CreateStreamOnHGlobal(hGlobal, TRUE, &pStream);
>
> if ( hResult == S_OK )
> {
> hResult = OleLoadPicture(pStream, nSize, FALSE, IID_IPicture, (LPVOID
> *)pPicture);
> pStream->Release();
> }
>
> return hResult;
> }
>
> HRESULT __stdcall CPictureListCtrl::LoadPictureFromFile(BSTR FileName,
> IPicture **pVal)
> {
> if ( !pVal ) {
> return E_POINTER;
> }
>
> HRESULT retval = E_FAIL;
>
> CString path(FileName);
>
> HANDLE hFile = ::CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
> OPEN_EXISTING, 0, NULL);
>
> if ( hFile != INVALID_HANDLE_VALUE ) {
>
> DWORD dwLoSize = 0, dwHiSize = 0;
> dwLoSize = ::GetFileSize(hFile,&dwHiSize);
>
> if ( dwLoSize != 0xFFFFFFFF ) {
>
> BYTE* pBuff = new BYTE[dwLoSize];
>
> if ( ::ReadFile(hFile,pBuff,dwLoSize,&dwHiSize,NULL) == TRUE ) {
>
> retval = LoadFromBuffer(pBuff, dwLoSize, pVal);
> }
>
> delete [] pBuff;
> }
>
> ::CloseHandle(hFile);
> }
>
> return retval;
> }
>
>
> Asko.