Binary data from VB6 to C#

Author: Dave
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

Comments: (0)

Leave Comment:
Email: (not shown)
Message: (Required)
Math Question: 52 + 68 = ? followed by the letter: V 

About Me
More Blogs
Main Site
Console tricks
FireFox temp dir
OCX License
Extract substring
VB6 Console Apps
VB6 Debugger View As Hex tooltips
VB6 - C Share registry data
VB6 Addin Missing Menus
VB6 Class Init Params
VB6 isIn function
Python and VB6
Python pros and cons
download web Dir
vc rand in python
VB6 Language Enhancement
Register .NET as COM
UDT Tricks pt2
Remote Data Extraction
Collection Extender
VB6 FindResource
DirList Single Click
Reset CheckPoint VPN Policy
VB6 BSTR Oddities Explained
SafeArrays in C
BSTR and Variant in C++
Property let optional args
Misc Libs
Enum Named Pipes
Vb6 Collection in C++
VB6 Overloaded Methods
VB6 Syncronous Socket
Simple IPC
VB6 Auto Resize Form Elements
Mach3 Automation
Exit For in While
C# self register ocx
VB6 Class Method Pointers
JS Debugger
Duktape Debug Protocol
QtScript 4 VB
Vb6 Named Args
vb6 Addin Part 2
VB6 Addin vrs Toolbars
OpenFile Dialog MultiSelect
Duktape Example
DukTape JS
VB6 Unsigned
.Net version
TitleBar Height
.NET again
VB6 Self Register OCXs
Query Last 12 Mos
Progid from Interface ID
VB6 to C Array Examples
Human Readable Variant Type
ScriptBasic COM Integration
CodeView Addin
ScriptBasic - Part 2
Script Env
printf override
History Combo
Disable IE
API Hooking in VB6
Addin Hook Events
FastBuild Addin
VB6 MemoryWindow
Link C Obj Files into VB6
Vb6 Standard Dlls
CStr for Pascal
Lazarus Review
asprintf for VS
VB6 GlobalMultiUse
Scintilla in VB6
Dynamic Highlight
WinVerifyTrust, CryptMsgGetParam VB6
MS GLEE Graphing
printf for VB6
C# App Config
Tero DES C# Test
VC 2008 Bit Fields
Speed trap
C# Db Class Generator
VB6 vrs .NET (again)
FireFox Whois Extension
git and vb6
Code Additions
Compiled date to string
C# ListView Sorter
VB6 Wish List
C# Process Injection
C# PE Offset Calculator
VB6 Async Download
Show Desktop
coding philosophy
Code release
Dll Not Found in IDE
Advanced MSScript Control
random tip
Clipart / Vector Art
VB6 Callback from C#
Binary data from VB6 to C#
CSharp and MsScriptControl
HexDumper functions
Js Beautify From VB6 or C#
vb6 FormPos
Inline Asm w VB6
The .NET Fiasco
One rub on computers
Universal extractor