Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to programmatically convert VB6 Formatting strings to .NET Formatting strings?

  1. Does anyone know of a good reference for VB6 format-strings?
  2. Does anyone know of a converter from VB6 formatting strings to .NET strings?

I'm working on porting a large VB6 code base to .NET. It is a database centric piece of software, and the database itself holds VB6 format-strings which are later loaded and used to display other data in the database.

My question, like this article, is how to port this. However the answer chosen for that question isn't adequate for my needs. I'm uncomfortable relying on libraries specifically designed for backwards compatibility with a language I've been specifically hired to port away.

like image 550
Chris Pfohl Avatar asked Nov 01 '10 19:11

Chris Pfohl


1 Answers

The formatting routine that VB6 uses is actually built into the operating system. Oleaut32.dll, the VarFormat() function. It's been around for 15 years and will be around for ever, considering how much code relies on it. Trying to translate the formatting strings to a .NET composite formatting string is a hopeless task. Just use the OS function.

Here's a sample program that does this, using the examples from the linked thread:

using System;
using System.Runtime.InteropServices;

class Program {
    static void Main(string[] args) {
        Console.WriteLine(Vb6Format("hi there", ">"));
        Console.WriteLine(Vb6Format("hI tHeRe", "<"));
        Console.WriteLine(Vb6Format("hi there", ">!@@@... not @@@@@"));
        Console.ReadLine();
    }

    public static string Vb6Format(object expr, string format) {
        string result;
        int hr = VarFormat(ref expr, format, 0, 0, 0, out result);
        if (hr != 0) throw new COMException("Format error", hr);
        return result;
    }
    [DllImport("oleaut32.dll", CharSet = CharSet.Unicode)]
    private static extern int VarFormat(ref object expr, string format, int firstDay, int firstWeek, int flags,
        [MarshalAs(UnmanagedType.BStr)] out string result);
}
like image 191
Hans Passant Avatar answered Sep 23 '22 06:09

Hans Passant