Binary data from VB6 to C#
Date: 09.18.10 - 9:39pm
First let me say wow...
I either really suck, or transfering binary data between VB6 and CSharp is a real PITA.
VB6 developers are use to an awesome string type which can hold binary data reliably (well ok it can screw up if you code page is set to a mid eastern language and you do some sloppy conversions)
Anyway..So i wanted to transfer some binary data between my VB6 exe and a c sharp dll. I first tried to use COM and transfering string types. To a VB6 developer this is expected to be OK. ummm not so much.
I screwed around with various types of encodings and manual conversions literally all day. I ended up scrapping the dll and making the .NET app a console application that worked off of files I was so frustrated. (talk about sloppy)
So I could have made the dll use in and out files too..but i figured whats the point, if I am going to write files to disk..i might as well make it an exe so at least non programmers could use its functionality. Maybe not a bad choice..except for all the console windows flashing up when you use it. (I know i could get cute and hide those too but not if I am using the Windows Script Shell to read all of its output. (yes you could shell and hide and redirect output..but aghh yes then you arent waiting until its done executing without yet more code..see my delight..)
Ok rant is almost over i promise.
So a ended up fighting late into the night to come up with a better solution that files on disk and shelling external applications. And i wasnt about to fight with encodings anymore.
I ended up finding a good solution that is actually pretty clean even though CSharp isnt really supposed to work with raw memory.
Memory Mapped Files turned out to be the ticket i was looking for.
Demo is crude but operational.
Update: Ok..I am kinda stupid..after just finishing a rather large portion of code using this technique while on vacation no less...I have finally realized...I just spent all that time working on an interprocess communication technque when in fact all of the code I am dealing with in my project is exchanging data within the same process. Which means I could have just passed a varptr() address of a byte buffer and a size to C Sharp which would then load a byte with Mashal.Copy, and then passed back an HAlloc() address and a size from csharp using CopyMemory to load it into into a new byte buffer for vb. Probably would have been half the code and no real API to go wrong..oghh well.
Example of exchanging binary data with csharp from vb