July 23, 2010

Register an ActiveX DLL from within FoxPro

Filed under: FoxPro — Tags: — admin @ 5:30 am

A Chilkat customer provided this bit of Foxpro code:

DECLARE LONG DllRegisterServer IN <yourfile.dll>
IF DllRegisterServer() = 0
   * OK
ELSE
  * Not OK
ENDIF

It provides an alternative way to distribute an ActiveX DLL with a FoxPro application. Typically, one would create an installer, such as a .msi or something from an install software package (InstallShield perhaps), such that the installer registers any ActiveX DLL’s included in the setup/deployment project when installing. However, using the idea above, it should be possible to simply include the ActiveX DLL in the same directory as the EXE and potentially distribute as a simple .zip. When the app is first run, it could automatically register the ActiveX DLL.

January 7, 2009

ActiveX Events in FoxPro

Filed under: FoxPro — Tags: , — admin @ 10:55 am

ActiveX components and controls are used in many programming languages, each of which has it’s own way of handling event callbacks.  This blog post provides helpful hints about how to receive event callbacks from an ActiveX component.

To receive the event, you must bind the ActiveX (also referred to as the COM server) event to the implemented interface methods on a Visual FoxPro object.   This is done using FoxPro’s EVENTHANDLER function.  For example:

* oCtrl will be the object in Visual FoxPro to receive the event notifications.
* It will implement one or more of the ActiveX's event callback methods
oCtrl = CREATEOBJECT("Ctrl")

* Create an instance of an ActiveX component:
loMailman = CreateObject('Chilkat.MailMan2')
...

*The EVENTHANDLER tells the COM object to send events to the VFP object:
EVENTHANDLER(loMailman,oCtrl)

Let’s look at the VFP object’s class (i.e. oCtrl):

*Definition of control class:
DEFINE CLASS Ctrl as Session OLEPUBLIC

*The IMPLEMENTS... line below is what makes Foxpro take control over the SendPercentDone,
ReadPercentDone and AbortCheck
*NOTE - This path part of the IMPLEMENTS must point to the dll, so locate and correct it
before you are using the program.
IMPLEMENTS _IChilkatMailEvents IN "c:\mydlls\chilkatmail_v7_9.dll"

PROCEDURE Init
ENDPROC

PROCEDURE _IChilkatMailEvents_SendPercentDone(percentDone AS Number, abort AS Number) AS VOID;
    HELPSTRING "method SendPercentDone"
    * add user code here

ENDPROC

PROCEDURE _IChilkatMailEvents_ReadPercentDone(percentDone AS Number, abort AS Number) AS VOID;
    HELPSTRING "method ReadPercentDone"
    * add user code here

ENDPROC

PROCEDURE _IChilkatMailEvents_AbortCheck(abort AS Number) AS VOID;
    HELPSTRING "method AbortCheck"
    * add user code here

ENDPROC

PROCEDURE _IChilkatMailEvents_EmailReceived(subject AS String, fromAddr AS String
    fromName AS String, returnPath AS String, date AS String, uidl AS String,
    sizeInBytes AS Number) AS VOID;
    HELPSTRING "method EmailReceived"
    * add user code here

ENDPROC

ENDDEFINE

This is not enough for events to fire.  You’ll need to take one additional step:  Set the AutoYield property = .T. to allow ActiveX events to fire.

Finally, how do you know what events are available for a given ActiveX?   Unfortunately, Chilkat does not have programming-language specific help for the events.  We can look at the IDL for the events and then understand how to write the FoxPro event interfaces.  For example, here’s the IDL for Chilkat MailMan:

dispinterface _IChilkatMailEvents
{
properties:
methods:
[id(1), helpstring("method SendPercentDone")] HRESULT SendPercentDone([in] long percentDone, [out] long *abort);
[id(2), helpstring("method ReadPercentDone")] HRESULT ReadPercentDone([in] long percentDone, [out] long *abort);
[id(3), helpstring("method AbortCheck")] HRESULT AbortCheck([out] long *abort);
[id(4), helpstring("method EmailReceived")] HRESULT EmailReceived([in] BSTR subject, [in] BSTR fromAddr,
    [in] BSTR fromName, [in] BSTR returnPath, [in] BSTR date, [in] BSTR uidl, [in] long sizeInBytes);
};

A few notes:

  • Notice the “_IChilkatMailEvents” corresponds to the “_IChilkatMailEvents” in the FoxPro code.
  • All event methods will be declared as a PROCEDURE returning VOID (i.e. “AS VOID”)
  • Ignore the HRESULT in the IDL.  It doesn’t apply in FoxPro.
  • In the IDL, BSTR means “string”.
  • In the IDL, input arguments are specified by “[in]“, output arguments are specified by “[out]“.  A common output argument is “abort”.  Your event handler may set it equal to 1 to abort the current Chilkat method that is running, or leave it at 0 to allow it to continue.

That’s about it.   You should be able to look at the IDL and know how to create your event handling code in FoxPro.  Here’s a link to the IDL for Zip, MailMan, Ftp2, HTTP, and IMAP: http://www.cknotes.com/?p=119

May 20, 2008

FTP2 Events for FoxPro

Filed under: FTP, FoxPro, events — Tags: , , — admin @ 9:22 am

The Chilkat FTP2 ActiveX has events for progress monitoring. The events may also be used to abort an FTP upload/download, or to skip files and/or directories.

These are the events in Visual FoxPro terms:

PROCEDURE _IChilkatFtp2Events_PutProgress(percentDone AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_GetProgress(percentDone AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_AbortCheck(abort AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_BeginDownloadFile(filePath AS String, skip AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_EndDownloadFile(filePath AS String, numBytes AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_VerifyDownloadDir(dirPath AS String, skip AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_BeginUploadFile(filePath AS String, skip AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_EndUploadFile(filePath AS String, numBytes AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_VerifyUploadDir(dirPath AS String, skip AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_VerifyDeleteDir(dirPath AS String, skip AS Number) AS VOID;
PROCEDURE _IChilkatFtp2Events_VerifyDeleteFile(filePath AS String, skip AS Number) AS VOID;

The “skip” and “abort” args are output-only.

An example of using ActiveX events in FoxPro may be found here:
Email Events in FoxPro
Although the example is for sending email, the same programming techniques apply with the FTP2 component.