Microsoft KB Archive/259211

= PRB: COMRETURNERROR Only Works Properly if Error Has Occurred =

Article ID: 259211

Article Last Modified on 4/12/2000

-

APPLIES TO


 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q259211



SYMPTOMS
The COMRETURNERROR function populates the COM exception structure with information that Automation clients can use to determine the source of Visual FoxPro Automation errors. However, if this function is called without an error having occurred, it does not properly populate the structure. Visual FoxPro clients display the information that was sent, but other clients may display only a generic &quot;Automation Error&quot; message.



RESOLUTION
One resolution to this problem is to always call the COMRETURNERROR function from the Error method of your Automation server. The ERROR cErrorMessage command throws Error 1098 (User-defined error) when used. You can test for this in the Error method and call the COMRETURNERROR function with the cErrorMessage command.



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Create a new project and save it as ThrowErr.pjx . On the the Code tab, click New.  Save the following code as ThrowErr.prg : DEFINE CLASS RetErr AS custom OLEPUBLIC

Name = &quot;RetErr&quot; PROCEDURE ThrowErrBad COMRETURNERROR(&quot;ThrowErr&quot;, &quot;109&quot;) ENDproc PROCEDURE ThrowErrGood ERROR &quot;109&quot; ENDproc PROCEDURE Error LPARAMETERS nError, cMethod, nLine

IF nError = 1098 COMRETURNERROR(&quot;ThrowErr&quot;, MESSAGE) ENDif ENDproc ENDdefine  On the Build menu, select Multi-threaded COM server, and save it as ThrowErr.dll .  To test this in FoxPro, run the following code in the Command window. You should see the same error box pop up both times. loServer = CREATEOBJECT(&quot;ThrowErr.RetErr&quot;) loServer.ThrowErrBad loServer.ThrowErrGood  To test this in Microsoft Access, create a new database and a new form. Put two CommandButtons named Command1 and Command2 and three Labels named Label1, Label2, and Label3 on the form.</li>  In Command1, put the following code: Private Sub Command1_Click On Error Resume Next Set x = CreateObject(&quot;ThrowErr.RetErr&quot;) Call x.ThrowErrBad Label1.Caption = Str(Err.Number) Label2.Caption = Err.Source Label3.Caption = Err.Description End Sub </li>  In Command2, put the following code: Private Sub Command2_Click On Error Resume Next Set x = CreateObject(&quot;ThrowErr.RetErr&quot;) Call x.ThrowErrGood Label1.Caption = Str(Err.Number) Label2.Caption = Err.Source Label3.Caption = Err.Description End Sub </li> Run the form, click Command1, and then click Command2. Command1 displays 440 for the Error Number, the name of the database for the Error Source, and Automation Error for the Error Description.

Command2 displays 98 for the Error Number, ThrowErr for the Error Source, and 109 for the Error Description.

Further clicks on Command1 display the correct error information.</li></ol>

<div class="references_section">