Question : OLE IDispatch exception code 0 from ?: ?.. from returned COM Object

I have a COM Server writte in VFP9 that returns various COM Objects. The problem I have is that the returned COM object is giving me the virtually no information if an error occurs. I always get the message "OLE IDispatch exception code 0 from ?: ?.." if an error occurs in the returned object. I wonder if this is something to do with VFP late binding and whether there is anything I can do to get around this? I have tried using an error method on both objects with COMReturnError but I still get the same message.

To demonstrate this problem you can replicate the issue by creating a test COM server and a test program as per the attached snippet.

Many thanks
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
*-- Create project called testcomerror and save this as a program within the project.
DEFINE CLASS testcom as Session olepublic
FUNCTION testmethod
  loassoc = CREATEOBJECT("assoc")
  RETURN loassoc
ENDFUNC
ENDDEFINE

DEFINE CLASS assoc as session olepublic
FUNCTION testhelper
  ERROR 1
  RETURN "Hello from the helper world"
ENDFUNC
ENDDEFINE
*-- End of server program

*-- Then build the project and run the following code from command prompt or a program
lotest = CREATEOBJECT("testcomerror.testcom")
loobject = lotest.testmethod
? loobject.testhelper()

*-- You will get the "OLE IDispatch code 0 from ? :?" message rather than an error 1 or anything meaningful.

Answer : OLE IDispatch exception code 0 from ?: ?.. from returned COM Object

Errors in COM objects must be handled differently.

Standard way of error handling in COM objects is described in attached code sample. Additional problem of your code is probably in the fact one COM object creates an instance of another COM object. If you would create an instance following way:

lotest = CREATEOBJECT("testcomerror.assoc")
? lotest.testhelper()

Then the error is reported correctly (by COMRETURNERROR, of course).
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
DEFINE CLASS sampleclass AS CUSTOM OLEPUBLIC
   NAME = "sampleclass"

   *!* GenError accepts one numeric parameter, which is the error number
   *!* of the Visual FoxPro (VFP) error to generate.
   PROCEDURE GenError
      LPARAMETERS nError
      
      *!* This raises the error.
      ERROR nError
   ENDPROC

   *!* When the error is generated, it triggers the Error event, which
   *!* creates a return statement to pass back to the client.
   PROCEDURE Error
      LPARAMETERS nError, cMethod, nLine
      LOCAL lcRetVal
      
      lcRetVal = ALLTRIM(STR(nError)) + ": " + MESSAGE() + ;
         " on Line " + ALLTRIM(STR(nLine)) + ;
         " in the sample DLL that was created for this article"
         
      COMRETURNERROR(cMethod, lcRetVal)
   ENDPROC
ENDDEFINE


*-- Calling sample:
oX = CreateObject('Mysample.sampleclass')
oX.generror(1)
Random Solutions  
 
programming4us programming4us