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.
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);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With