West Wind Internet Protocols

This class allows accessing of .NET classes without requiring COM registration for the .NET classes. It works by hosting the .NET runtime in Visual FoxPro and using a small proxy class to act as a Class Factory for instantiating .NET objects and passing them back for use in Visual FoxPro.

The class works through a couple of DLLs that provide a proxy service for instantiating types and passing them back to VFP. The wwDotnetBridge FoxPro class is simply a front end for the .NET class.

This class supports:

  • No COM Registration required for accessing .NET Assemblies
  • Explicit loading of .NET assemblies from disk or the GAC
  • Support for all .NET 4.0 Runtimes (incl. 4.5.x, 4.6.x, 4.7.x)
  • Access to most .NET components from FoxPro
  • Access to Value Types
  • Access to static values and methods
  • Access to enumerated values
  • Access to Generic .NET types
  • Call .NET methods asynchronously
  • Many helpers for accessing .NET Arrays, Lists and Dictionaries
  • Auto-Conversion of many problem .NET types in FoxPro
  • DataSet conversions to and from XmlAdapter (and from XmlAdapter to cursors)
  • Support for Task based Async methods with Callback support
  • Support for calling any .NET method asynchronously
  • Auto-Conversion of many general FoxPro types to specific .NET types
  • ToJson() and ToXml() for .NET objects

Getting Started

Somewhere in the startup of your application call InitializeDotnetVersion():

*** Load dependencies and add to Procedure stack
*** Make sure wwDotnetBridge.prg wwDotnetBridge.dll wwIPStuff.dll 
*** are in your FoxPro path
DO wwDotnetBridge
InitializeDotnetVersion("V4") 

This ensures that wwDotnetBridge loads with the specified single version of the .NET Runtime that your FoxPro application can load.

Unable to load CLR Instance Errors

If you get an Unable to CLR Instance error when creating an instance of wwDotnetBridge, you probably need to unblock the wwdotnetbridge.dll or need to ensure that the wwdotnetbridge.dll and wwipstuff.dll are in your FoxPro path. Please see Unable to load CLR Instance for more info.

Then when you need to utilize wwDotnetBridge call GetwwDotnetBridge() to get a cached instance and use it to access .NET components:

*** Create or get cached instance of wwdotnetbridge
LOCAL loBridge as wwDotnetBridge, loHttp
loBridge = GetwwDotnetBridge()

*** Create a built-in .NET class and run a method
loHttp = loBridge.CreateInstance("System.Net.WebClient")
loHttp.DownloadFile("http://west-wind.com/files/MarkdownMonsterSetup.exe",
                    "MarkdownMonsterSetup.exe")
DO wwUtils
GoUrl(FULLPATH("MarkdownMonsterSetup.exe"))  && run it

*** Load a custom .NET assembly
loBridge.LoadAssembly("CustomDotnet.dll")

*** Access a .NET component from the new assembly
loItem = loBridge.CreateInstance("Custom.Item")

*** Access properties directly
? loItem.Sku
loItem.Sku = "NewSku"
lnTotal = loItem.CalculateTotal()

*** Access non-accessible properties and methods indirectly
lnFlagValue = loBridge.GetProperty(loItem,"Flag")
lnFlagValue = loBridge.SetProperty(loItem,"Flag",5) 
loBridge.InvokeMethod(loItem,"PassFlagValue",lnFlagValue)

Note that not all properties and methods can be accessed directly as shown on the first example, but some properties and methods require implicit activation as in the 'Flag' example requiring GetProperty(), SetProperty() or InvokeMethod() to indirectly access object members.

If direct access fails, always try the indirect methods.

For much more detailed wwDotnetBridge and .NET Interop information you can also check out the white paper:


© West Wind Technologies, 2023 • Updated: 04/18/22
Comment or report problem with topic