Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / COM / February 2005



Tip: Looking for answers? Try searching our database.

How to get HRESULT softerrors(e.g. S_FALSE) value in VB

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
ishekara - 18 Feb 2005 11:17 GMT
Hi all,

I have a C++ COM dll that is used in C++ as well as VB. one of the function
in this dll returns soft error i.e. S_FALSE, In C++ i can check the return
value whereas in VB only errors i.e. E_FAIL,E_NOINTERFACE etc can be caught
as error and S_FALSE cannot be caught. Is there any way we can have VB know
the return value?

e.g.
C++ COM dll function:

HRESULT SomeFunction(BSTR pName)
{
   if (wcslen(pName) == 0)
       return S_FALSE;
}

In C++ client I can check the return as follows.

CComBSTR str;
HRESULT hr=SomeFunction(str);
if (hr==S_FALSE)
{
   // do some processing.
}

In VB client the function doesn't have the return value

Dim str as String
call SomeFunction(str)
' now i dont know the return value.

Can someone help me find a way to find out the return value from VB client?

Thanks for help.
shekar.
Ralph - 19 Feb 2005 00:26 GMT
> Hi all,
>
[quoted text clipped - 32 lines]
> Thanks for help.
> shekar.

shekar,

Forgive me if your example was a typo or something, but first of all if you
use 'Call' the return value is swallowed up by VB. So in order to get a
return you have to provide one...
    Dim lRtn As Long
    lRtn = SomeFunction( str )

But don't get too excited yet! <g> This return will *NOT* be the HRESULT, it
will be whatever you defined in the IDL as [in, retval], should be the last
parameter in the argument list, and should be of type pointer-to the 'real'
value you want to return. eg, long*, BSTR*, &etc.

    HRESULT SomeFunction( [in, out] BSTR* str, [out, retval] long* lRtn );

According to the rules of COM, or Automation in this case (you did define
your interface in the IDL as 'automation' or 'dual', right?), the HRESULT is
always made available to the transport layer  (not the user) so that it can
play around with it (ignore, add its own, etc.). In the case of VB it ends
up the VB Runtime, and is often 'converted' to a VB Runtime error.

For example, an E_INVALIDARG ends up as a VB Runtime Error 5 "Invalid
procedure call or argument". I think a E_NOINTERFACE  would end up as a 425
or 426 "Method not supported by object" or something like that. (don't hold
me to this I am just winging it, with no reference material, and my memory
isn't that good. <g>).

So to catch the "HResult" (kind of) you can use VB built in structured error
hander. The ErrorObject in VB is essentially just a wrapper for the
EXCEPINFO struct implementing an interface to the members...

On Error Goto ErrorHandler
    Dim lRtn As Long
    lRtn = SomeFunction( str )
    Exit
ErrorHandler:
       ' right out of the struct...
   Debug.Print Err.Source   ' bstrSource
   Debug.Print Err.Description  ' bstrDescription
   Debug.Print Err.HelpFile       ' bstrHelpFile
   Debug.Print Err.HelpContext  ' bstrHelpContext

' ignore this... <g>
'Debug.Print Err.LastDLLError ' returned from Windows system API calls

   Debug.Print Err.Number       ' usually the runtime error
      ' you can catch the error from the OLE transport (usually the
HRESULT)
      ' like this...
   Dim lHR As Long
   If Err.Number And vbObjectError Then
        lHR = Err.Number XOr vbObjectError.
   End If

Note I said 'usually'! The HResult, according to the rules of com, might
actually get changed. (based on the wCode, or something else.)

I realize it looks a bit contrieved, but VB is actually only following the
rules. It is only because it is so easy to violate the rules in C/C++ that
makes makes VB's handling look so strange.

hth
-ralph
ishekara - 21 Feb 2005 06:04 GMT
ralph,

Thanks for your reply, but i was expecting a way to see S_FALSE returned
from C++ in VB.

shekar.

> > Hi all,
> >
[quoted text clipped - 100 lines]
> hth
> -ralph
anonymous@discussions.microsoft.com - 21 Feb 2005 21:42 GMT
>-----Original Message-----
>ralph,
[quoted text clipped - 3 lines]
>
>shekar.

Sorry, I should haved gleamed that from your post.

I could have saved a lot of typing and just said - no.
<g>

Of course, they say nothing is impossible in
programming... but AFAIK the runtime swallows everything
that isn't an out-right error.

-ralph
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.