10 Cool Examples of Using Wwdotnetbridge to Extend Your Foxpro Code

10 Cool Examples of Using Wwdotnetbridge to Extend Your Foxpro Code

10 Cool Examples of using wwDotnetBridge to extend your FoxPro code Prepared for: Southwest Fox 2016 By Rick Strahl weblog.west-wind.com [email protected] wwDotnetBridge is a small library for Visual FoxPro, that allows you to call just about any .NET component from Visual FoxPro. .NET has now been around for 15+ years and there’s a huge amount of library content available that you can plug into your own applications. Whether it’s accessing native functionality that comes with the .NET framework or whether it’s third party or free open source components, or whether it’s components that you’d rather offload to .NET for processing rather than doing it in FoxPro. Here’s what we’re going to cover: 1. Create a powerful string formatter 2. Create an encryption library 3. Add Markdown parsing to your applications 4. Upload and Download files using SFTP securely 5. Add spell checking to your applications 6. Call a SOAP Web Service 7. Humanizer 8. Inter-application messaging with Named Pipes 9. Hosting a Web Server in Visual FoxPro 10. Make Asynchronous .NET method calls 11. Bonus: Broadcast messages with SignalR You can find the examples related to this articles in a BitBucket repository: https://bitbucket.org/RickStrahl/swfox16_wwdotnetbridge_10uses wwDotnetBridge on GitHub Create a Number and Date String Formatter This example demonstrates: Simple use case of calling native .NET BCL methods and wrapping with FoxPro function wrappers. For this first example lets look at something very simple, that takes advantage of what’s built into the .NET framework natively and doesn’t require any external .NET dependencies. .NET has a few built-in powerful language string formatters. Specifically, it has a nice way of converting numbers and dates into strings using a variety of formatting options that are not available in FoxPro. The formatter can also deal with logical values and convert to string any value or object that supports a custom .ToString() method. If you’ve used C style languages before you probably know about printf based formatting and .NET has a much more powerful version of this type of string formatter. Formatting Dates and Numbers The first function is FormatValue(), which is a super simple way of passing through a passed value and using the .NET ToString() basically just a way to call the .NET string function. This is especially useful for formatting dates and times with custom and UI display formats which is much richer than what you get from FoxPro natively. ************************************************************************ * FormatValue **************************************** FUNCTION FormatValue(lvValue,lcFormatString) LOCAL loBridge IF ISNULL(lvValue) RETURN "null" ENDIF loBridge = GetwwDotnetBridge() IF EMPTY(lcFormatString) RETURN loBridge.InvokeMethod(lvValue,"ToString") ENDIF RETURN loBridge.InvokeMethod(lvValue,"ToString",lcFormatString) The idea of this function is that you pass in a FoxPro value and that value is passed to .NET and mapped to a .NET type. A number becomes a System.Double, System.Decimal or System.Int32 a date turns in System.DateTime. The function then calls the ToString(string format) version of this method which supports many string formatting options. Note the use of GetwwDotnetBridge() to retrieve a cached instance of wwDotnetBridge – this ensures that you don’t recreate the .NET environment everytime you call this function. So what can you do with this? Let’s take a look: DO wwDotnetBridge ? FormatValue(DATETIME(),"") * 6/6/2016 7:49:26 PM ? FormatValue(DATETIME(),"MMM dd, yyyy") * Jun 10, 2016 ?FormatValue(DATETIME(),"MMMM dd, yyyy") * August 1, 2016 ? FormatValue(DATETIME(),"HH:mm:ss") * 20:15:10 ? FormatValue(DATETIME(),"h:m:s tt") * 8:5:10 PM ? FormatValue(DATETIME(),"MMM d @ HH:mm") * Aug 1 @ 20:44 ? lcFormat ? FormatValue(DATETIME(),"r") * Mon, 06 Jun 2016 22:41:33 GMT lcFormat = "u" && ISO time format Json/xml ? lcFormat ? FormatValue(DATETIME(),lcFormat) * 8:5:10 PM *** Number formats ? FormatValue(2,"00") * 02 ? FormatValue(12,"00") * 12 ? FormatValue(1233.22, "c") * $1,233.22 ? FormatValue(1233.2255, "n2") * $1,233.23 As you can see here you get a plethora of options for formatting numbers and dates, much more so than what’s available in FoxPro natively. For a reference of what’s available for dates and numbers you can check these links: Date Formats: https://msdn.microsoft.com/en-us/library/8kb3ddd4(v=vs.110).aspx Number formats: https://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx It’s also nice that these values are automatically formatted using the current culture – if you’re running in German the formatting will automatically pick up German names for months and the number format automatically picks up the proper separators for 1000s and decimal points. Another useful function is FormatString() which allows you to use a format string, and embed values into it. This is similar to TextMerge() in FoxPro except that you get the same formatting features shown above. Here’s the FormatString() function: ************************************************************************ * FormatString **************************************** *** Function: Uses a string template to embed formatted values *** into a string. *** Assume: *** Pass: lcFormat - Format string use {0} - {10} for parameters *** lv1..lv10 - Up to 10 parameters *** Return: ************************************************************************ FUNCTION FormatString(lcFormat, lv1,lv2,lv3,lv4,lv5,lv6,lv7,lv8,lv9,lv10) LOCAL lnParms, loBridge lnParms = PCOUNT() loBridge = GetwwDotnetBridge() DO CASE CASE lnParms = 2 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1) CASE lnParms = 3 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2) CASE lnParms = 4 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3) CASE lnParms = 5 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4) CASE lnParms = 6 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5) CASE lnParms = 7 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5,lv6) CASE lnParms = 8 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5,lv6,lv7) CASE lnParms = 9 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5,lv6,lv7,lv8) CASE lnParms = 10 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5,lv6,lv7,lv8,lv9) CASE lnParms = 11 RETURN loBridge.InvokeStaticMethod("System.String","Format",lcFormat,lv1,lv2,lv3,lv4,lv5,lv6,lv7,lv8,lv10 ) OTHERWISE THROW "Too many parameters for FormatString" ENDCASE ENDFUNC The implementation of this function is a little more involved in that you have to pass each of the parameter explicitly to the .NET function. Since each overload is effectively a different method call there are all these overloads. There’s actually an easier way to just pass an array as a parameter, but while less code it’s actually slower from the FoxPro end, so this approach is most effective. To call the function is very simple though: ? FormatString("Hey {0}, the date and time is: {1:MMM dd, yyyy - h:mm}","Rick",DATETIME()) Which produces: Hey Rick, the date and time is: Jul 10, 2016 – 9:20 Any values that you pass are automatically converted to string using the .NET ToString() function, so if you pass a custom object that has a custom ToString() function you can render that value. This function is not a replacement for TextMerge() in FoxPro as you can’t eval FoxPro expressions inline, but it’s very useful if you need to format dates and numbers as part of a string. Create an Encryption Library This example demonstrates: Creating a wrapper library of static functions and calling them from FoxPro and creating a wrapper FoxPro class to simplify calling the library. Encryption is a thorny topic in development and the tools available for FoxPro natively via COM are getting a bit long in the tooth. While there are libraries that can address a number of use cases these libraries are fixed in functionality. If there’s something you need that works slightly different, you generally can’t enhance the functionality yourself. By using .NET you can take advantage of the large set of encryption libraries that are relatively easy to use, and can easily be customized to create custom encryption solutions or expose common encryption formats. Creating a .NET Wrapper Class Here’s a small .NET encryption library implementation that provides some common Encryption features. If there are other requirements or providers you might need it’s easy to extend and add those as needed. The class is easily callable from FoxPro and supports: Two Encryption/Decryption using TripleDES with Salted Hashes You typically use two-way encryption for encrypted content you want to store on disk. Encrypted text or binary files or encrypted data in configuration settings are examples where you store an encrypted value and then retrieve the decrypted value to present to the user. Hashing using SHAXXX and MD5 Unlike Encryption/Decryption, Hashing is a one-way, meaning you only encrypt data using a hash value that is stored. You then compare the computed hashvalue, but never retrieve the original unencrypted data. Hashing is typically used for storing things like passwords or other security values that are only validated but never require getting the original value. CheckSum Computation SHA256 and MD5 Provides computed checksums on files or binary values, which is quite common for validation of content sizes. public static class EncryptionUtils { /// <summary> /// Replace this value with some unique key of your own /// Best set this in your App start up in a Static constructor /// </summary> public static string EncryptionKey = "4a3f131c"; /// <summary> /// Replace this Salt value with a unique value for your application /// by assigning it at application startup to a fixed value /// </summary> public static byte[] SaltBytes = new byte[] { 3, 20, 129, 11, 223, 1, 55, 53, 143, 112, 51, 39, 22, 189 }; /// <summary> /// Encodes a stream of bytes using DES encryption with a pass key.

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    80 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us