Class wwDotnetBridge

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:

Custom
  wwDotnetBridge

Remarks

wwDotnetBridge requirements:

  • Windows 10, 7, 2008 R2, 2012, 2016 or later
  • .NET 4.5 Runtime or later (4.5.x,4.6.x,4.7.x etc.)

Older versions of Windows and .NET 4.0 are optionally supported by way of wwdotnetbrdige_xp.dll which uses .NET Runtime 4.0 to replace wwdotnetbridge.dll`. Using this DLL you can run on XP, Vista and Server 2003, but only with .NET 4.0 Runtimes. This means you cannot load .NET 4.5 or later components that are common today.

It's also possible to load .NET 2.0 Runtimes (using the V2 version parameter), but we highly recommend you do not use .NET 2.0 as there are security implications and V2 is no longer deployed with Windows 8.1 and Windows 10. 2.0 components can easily be loaded by .NET 4.5+ and that is the recommended path.

Single .NET Runtime Version per FoxPro Process

You can only load a single version of the .NET Framework into a FoxPro app with wwDotnetBridge. To ensure you control the version that you want your application to use, explicitly specify the version when your application starts up, in the main program or main form.

 InitializeDotnetVersion("V4")
  • Relies on wwipstuff.dll and wwDotNetBridge.dll (.NET 4.0 binary)

Class Members

MemberDescription

ConvertToDotNetValue

o.ConvertToDotNetValue(lvValue, lcType)

CreateArray

o.CreateArray(lvArrayInstanceOrTypeName)

CreateComValue

o.CreateComValue(lvValue)

CreateInstance

Creates an instance of a .NET class and passes it back to Visual FoxPro as a COM reference.

Types need to be loaded as fully qualified class names (ie. Namespace.Class).

Before you load the class specified make sure any required assemblies are loaded first with LoadAssembly().

o.CreateInstance(lcClass, lvParm1, lvParm2, lvParm3, lvParm4, lvParm5)

CreateInstanceFromFile

Allows creation of an type directly from a file without having to first call LoadAssembly().

Generally this is not recommended except for special occasions, it's almost always bet

o.CreateInstanceFromFile(lcAssemblyFilename, lcType,lvParm1..lvParm3)

CreateInstanceOnType

Allows creation of a new .NET type and assigning that type to a .NET object property without ever going through Visual FoxPro. This allows for creation on non-COM visible types in .NET and indirect manipulation of them.

o.CreateInstanceOnType(loInstance,lcProperty, lcClass,lvParm1,lvParm2, lvParm3, lvParm4, lvParm5)

CursorToDataSet

o.CursorToDataSet(lcAliasList)

DataSetToCursors

Converts a .NET DataSet to cursors that match the tables in the DataSet's Tables collection.

o.DataSetToCursors(loDs)

DataSetToXmlAdapter

Converts a .NET DataSet object instance to a Visual FoxPro XmlAdapter object that can be turned easily into cursors.

o.DataSetToXmlAdapter(loDs)

FromJson

Deserializes JSON into a .NET object or simple value.

o.FromJson(lcJson,lvDotnetType)

FromXml

Parses an XML string into a .NET object.

This method requires that you provide either .NET type object, or a fully qualified type name (namespace.classname or fully qualified GAC name).

o.FromXml(lcJson,lvDotnetType)

GetEnumString

Retrieves the string enum field name from a .NET Enum value.

Enums are stored as numeric values internally. This function

o.GetEnumString(lcEnumType, lvEnumValue)

GetEnumValue

o.GetEnumValue(lcType, lcValue)

GetIndexedProperty

Returns an array value by its index.

o.GetIndexedProperty(loListInstance, lnIndex)

GetProperty

o.GetProperty(loInstance, lcProperty)

GetStaticProperty

Retrieves a static property from a .NET type.

o.GetStaticProperty(lcType, lcProperty)

GetType

Returns a .NET Type object from the value passed to this function.

o.GetType(lvValue)

GetTypeFromName

Returns a .NET type reference for a type by its fully qualified .NET type name (ie. namespace.classname).

o.GetTypeFromName(lcTypeName)

GetwwDotNetBridge

GetwwDotNetBridge(lcVersion)

Init

o.wwDotNetBridge.Init(lcDotNetVersion, llUseComInterop)

InitializeDotnetVersion

InitializeDotnetVersion(lcVersion)

InvokeMethod

o.InvokeMethod(loInstance,lcMethod,lvParm1,...lvParm10)

InvokeMethodAsync

o.InvokeMethodAsync(loCallbackEvents,     loInstance,     lcMethod,     lvParm1-10)

InvokeMethod_ParameterArray

o.InvokeMethod_ParameterArray(loInst,lcMethod,laParams)

InvokeStaticMethod

This method allows you to call a static .NET method such as System.Windows.Forms.MessageBox.Show() for example. Because the methods are static you can't create an instance and pass it back to FoxPro, instead this method has to be used to invoke a method by using a fully qualified type name instead of an instance.

o.InvokeStaticMethod(lcTypeName, lcMethod, lvParm1, ... lvParm10)

InvokeStaticMethodAsync

Invokes a .NET static method asynchronously on a seperate thread and fires OnCompleted() and OnError() events into the passed in Callback object.

Uses the same mechanism as wwDotNetBridge::InvokeMethodAsync.

o.InvokeStaticMethodAsync(loCallback,lcTypeName,lcMethod,lvParm1-10)

InvokeTaskMethodAsync

This method allows you to call any .NET async method - any method that returns a Task object basically - asynchronously by passing in a callback object that is called back when the async method completes or fails.

o.wwDotNetBridge.InvokeTaskMethodAsync(loCallback, loInstance, loMethod)

Load

Internal function responsible for bootstrapping the .NET Runtime and grabbing a wwdotnetBridge instance.

o.Load()

LoadAssembly

o.LoadAssembly(lcAssembly)

RunThread

o.RunThread(lcPrgFilename,loEvents)

SetProperty

Sets a .NET object property via Reflection.

o.SetProperty(loInstance,lcProperty,lvValue)

SetStaticProperty

Sets a static property or Enum value. Same behavior as SetProperty() but with static properties.

o.SetStaticProperty(lcType,lcProperty,lcValue)

SubscribeToEvents

o.SubscribeToEvents(loSource, loHandler, lcPrefix)

ToJson

Creates a JSON string from a .NET object or value passed in.

o.ToJson(loDotnetObject)

ToString

Returns the result from the .ToString() method reliably. ToString() often is overridden and so not directly accessible via direct COM access. This method internally calls InvokeMethod() to call the ToString().

o.ToString(loDotnetObject)

ToXml

Serializes a .NET object to an XML string.

o.ToXml(loDotnetObject)

Unload

o.Unload()

XmlAdapterGetCursor

Retrieves an indivdual cursor from a loaded XmlAdapter object.

o.XmlAdapterGetCursor(loAdapter,lvIndex)

XmlAdapterToCursors

Helper method that generates one cursor for each of the XmlAdapter's Tables. Cursors are created with the names defined in the XML document's item nodes (ie. the DataSet Table names).

o.XmlAdapterToCursors(loAdapter)

XmlStringToDataSet

Creates a .NET DataSet from an XML String that is exported from XmlAdapter or CursorToXml.

o.XmlStringToDataSet(lcXml)

cErrorMsg

Contains an error message if a method call fails.

lError

Error flag that can be checked after certain method calls.

oLastException

Returns the last .NET exception that was caused by a failed operation.

Requirements

Assembly: wwdotnetbridge.prg

See also:

wwDotNetBridge Examples | What is wwDotnetBridge? | wwDotnetBridge Features

© West Wind Technologies, 2004-2020 • Updated: 06/01/20
Comment or report problem with topic