AI Code

visual-foxpro VFP Interop with .NET


This topic will cover interop between VFP and .NET.

Using wwDotNetBridge to Run .NET Code

With the help of West Wind's wwDotNetBridge, you can easily have access .NET code within a VFP program.

The white paper has all the details, but this concise example will help illustrate the basic steps to running a method in a .NET assembly.

Note that wwDotNetBridge can directly access simple properties like strings, ints, etc. In order to access more complicated structures like lists, you first need to use the wwDotNetBridge function CreateArray to convert the .NET structure to a VFP COM array (as shown at the bottom of this example).

*!* Load WestWind .NET wrapper library (wwdotnetbridge.prg assumed to be in the search path)
IF (!wwDotNetBridge())
    RETURN .F.

lowwDotNetBridge = CREATEOBJECT("wwDotNetBridge","V4")

*!* Load .NET Assembly (include full or relative path if necessary)
IF !lowwDotNetBridge.LoadAssembly("SomeDotNetAssembly.dll")
    lcAssemblyLoadError = "LoadAssembly error: " + lowwDotNetBridge.cErrorMsg
    =MESSAGEBOX(lcAssemblyLoadError, MB_ICONSTOP, "Error")
    RETURN .F.

*!* Parameters to pass to class constructor
*!* You can pass up to 5 paramenters to the constructor
lcParameter1 = "StringParameter1"
lcParameter2 = "StringParameter2"
lnParameter3 = 3
lcParameter4 = .NULL.

*!* Get an instance of the assembly class
loAssemblyReference = lowwDotNetBridge.CreateInstance("MyDotNetProject.MyDotNetClass", ;
    lcParameter1, lcParameter2, lnParameter3, lcParameter4)
IF lowwDotNetBridge.lError
    lcAssemblyLoadError = "An error occurred loading the class: " + lowwDotNetBridge.cErrorMsg
    RETURN .F.

*!* Usage Example

*!* This example runs a method that return a boolean 
*!* and populates a List<string> (SomeStringList).
*!* The assembly has a public property named "LastErrorMessage" 
*!* with details about any handled exceptions/problems.

IF (!loAssemblyReference.SomePublicMethod())
    msg = "There was a problem executing the method:" + CRLF + ;
    =MESSAGEBOX(msg, MB_ICONSTOP, "Error")
    RETURN .F.

*!* At this point the string list (SomeStringList) should be populated
*!* wwDotNetBridge can convert that list to a VFP COM array (0-based)

laVFPArrayOfStrings = lowwDotNetBridge.CreateArray()

FOR x = 0 TO laVFPArrayOfStrings.Count-1
    ? laVFPArrayOfStrings.Item(x)

Got any visual-foxpro Question?