Microsoft KB Archive/262038

= PRB: COMRETURNERROR Does Not Work if Errors Occur During Server Creation =

Article ID: 262038

Article Last Modified on 8/27/2002

-

APPLIES TO


 * Microsoft Visual FoxPro 5.0 Standard Edition
 * Microsoft Visual FoxPro 5.0a
 * Microsoft Visual FoxPro 6.0 Professional Edition

-



This article was previously published under Q262038



SYMPTOMS
When you use Microsoft Visual FoxPro, you may make use of the COMRETURNERROR function in your Visual FoxPro Component Object Model (COM) servers. However, COMRETURNERROR only works from method calls. There is no way that COM can return an error during object creation. As a result, you do not get a detailed error message if a problem occurs in the INIT of your object and you are using COMRETURNERROR only.



CAUSE
This problem exists because there is no way that COM can return an error during object creation.



RESOLUTION
As a possible workaround for this issue, use an approach similar to the following when you develop your COM server:   Build this program code into an .exe file named GOOD_SERVER: ********* START CODE ***************** DEFINE CLASS SERVER AS CUSTOM OLEPUBLIC

xError = .F.

PROCEDURE INIT ERROR 21 ERROR 22 ERROR 23 ERROR 24 ENDPROC

PROCEDURE ERROR LPARAMETERS nError, cMethod, nLine #DEFINE CRLF CHR(13) + CHR(10) LOCAL cErrStr, cErrTxtFile cErrTxtFile = ; JUSTPATH(SUBSTR(SYS(16),AT(' ',SYS(16),2))) + &quot;\COMERR.TXT&quot; cErrStr = &quot;Error #: &quot; + TRANS(nError) + CRLF + ; &quot;Method Name: &quot; + cMethod + CRLF + ; &quot;On Line: &quot; + TRANS(nLine) + CRLF + ; &quot;Message: &quot; + MESSAGE + CRLF + ; &quot;DateTime: &quot; + TTOC(DATETIME) + CRLF + CRLF

IF &quot;INIT&quot; $ UPPER(cMethod) STRTOFILE(cErrStr, cErrTxtFile, .T.) THIS.xError = cErrStr ELSE COMRETURNERROR(cMethod, cErrStr) ENDIF ENDPROC

ENDDEFINE   Call the server by using the following code: ********* START CODE *****************
 * END CODE ******************
 * 1) DEFINE MB_ICONQUESTION  32   && Warning query
 * 2) DEFINE MB_YESNO          4   && Yes and No buttons
 * 3) DEFINE IDYES             6   && Yes button pressed
 * 4) DEFINE CRLF CHR(13) + CHR(10)
 * 5) DEFINE MB_OK             6   && OK button pressed

PUBLIC goMyServer LOCAL lcComInitError, lnReleaseCom lcComInitError = &quot;&quot; lnReleaseCom = 0

goMyServer = CREATEOBJECT(&quot;GOOD_SERVER.SERVER&quot;) IF VARTYPE(goMyServer) # &quot;O&quot; MESSAGEBOX(&quot;Create Object Failed!&quot;) RETURN .F. ENDIF

IF VARTYPE(goMyServer.xError) = &quot;C&quot; lcComInitError = goMyServer.xError lnReleaseCom = MESSAGEBOX(&quot;The following error occurred:&quot; + ;       + CRLF + CRLF + lcComInitError + CRLF + CRLF + ;        &quot;Additional errors may have occurred. Check COMERR.TXT&quot; + CRLF + ;        &quot;in the directory with the object for details. Release the object?&quot;, ;        MB_ICONQUESTION  + MB_YESNO, ;        &quot;Error Creating Object - Release It?&quot;)
 * !* Text in this property means we had an error (default is logical).

IF lnReleaseCom = IDYES RELEASE goMyServer MESSAGEBOX(&quot;Object Released&quot;,MB_OK,&quot;COM ERROR&quot;) ELSE MESSAGEBOX(&quot;Object Remains&quot;,MB_OK,&quot;COM ERROR&quot;) ENDIF RETURN .F. ENDIF MESSAGEBOX(&quot;Object Created Successfully&quot;) RELEASE goMyServer The main issue with this approach is that the error message you get from the message box is only the LAST error that occurred in the INIT of the object (error 24). To work around this, the object writes any errors out to a file named COMERR.TXT that resides in the same directory as the server. 
 * !* If goMyServer.xError is still .F., no INIT error occurred,
 * !* so do whatever...
 * END CODE ******************



STATUS
This behavior is by design.



Steps to Reproduce Behavior
 Build the following program into an executable file called BAD_SERVER.  Call the server from the Visual FoxPro Command window by running the oX = CREATEOBJECT('BAD_SERVER.SERVER') function. When you run the CREATEOBJECT function line, note that you see an error message such as this:

OLE error code 0x800004002: No such interface supported

********** START CODE ************* DEFINE CLASS SERVER AS CUSTOM OLEPUBLIC

PROCEDURE INIT *!* Cause an error ERROR 22 ENDPROC

PROCEDURE ERROR LPARAMETERS nError, cMethod, nLine #DEFINE CR CHR(13) LOCAL cErrStr cErrStr = &quot;Error #: &quot; + TRANS(nError) + CR + ; &quot;Method Name: &quot; + cMethod + CR + ; &quot;On Line: &quot; + TRANS(nLine) + CR + ; &quot;Message: &quot; + MESSAGE

COMRETURNERROR(cMethod, cErrStr) ENDPROC

ENDDEFINE </li></ol>
 * END CODE *************

<div class="references_section">