Calling a COM component from ASP
-
Hi, Here is the question : I develop a COM component using ATL . In this component I have a method called "BeepMe", from where I want to return a BSTR message , as an out parameter : BeepMe ([out] BSTR* pbstrMessage) when I call it from an ASP page (written in Jscript): var strRet; Component.BeepMe(strRet); the strRet is empty . but, if I declare the pbstrMessage as retval ( BeeepMe([out,retval] BSTR* pbstrMessage) ) , and write : var strRet = Component.BeepMe(); everithing works fine . The pbstrMessage is propertly initialized in the method ( *pbstrMessage = ::SysAllocString(L"Beep") ), so that isn't the problem. I know that the VB script interpreter handle the deallocation of memory , and I suppose that is also the ASP case . That is the cause of this behavior ? ( the ASP interpreter free the memory of the returned value ? )
-
Hi, Here is the question : I develop a COM component using ATL . In this component I have a method called "BeepMe", from where I want to return a BSTR message , as an out parameter : BeepMe ([out] BSTR* pbstrMessage) when I call it from an ASP page (written in Jscript): var strRet; Component.BeepMe(strRet); the strRet is empty . but, if I declare the pbstrMessage as retval ( BeeepMe([out,retval] BSTR* pbstrMessage) ) , and write : var strRet = Component.BeepMe(); everithing works fine . The pbstrMessage is propertly initialized in the method ( *pbstrMessage = ::SysAllocString(L"Beep") ), so that isn't the problem. I know that the VB script interpreter handle the deallocation of memory , and I suppose that is also the ASP case . That is the cause of this behavior ? ( the ASP interpreter free the memory of the returned value ? )
Hi, First of all you should use the SysReAllocString instead of SysAllocString. It's not the source of the problem but it's a correct way of doing things. Before assigning a new value to the pointer you must free the data it's pointing to. STDMETHODIMP CTest::get_Path(BSTR *pVal) { if(pVal == NULL) return E_POINTER; if(!SysReAllocString(pVal, m_bstrPath)) return E_OUTOFMEMORY; return S_OK; } Your problem is that in VBScript and JScript cannot pass parameters by reference to automation properties or methods. Use return values of properties and methods to return a value instead of using byref parameters for this purpose. This approach limits you to one return value. Regards, Alex Gorev, Dundas Software.