Re: Problem Sending VB6 Byte Array to C#...help?



Kevin:

Thanks again. I greatly appreciate the effort.

Please know that I understand the difference between pointers to arrays
and arrays and the like...I've been hacking around in all these things
for longer than I care to mention, from C to C++ and VB6 and .NET and
too many others, and have tried to mentor many a coworker on them over
the years :). This is just a very narrow problem tied to this
particular interop situation that made me wonder about typelib
generation for COM Interop DLL's.

I'm pretty sure that, based on your response, that I'm just doing a
lousy job of explaining my problem - and that's entirely my fault. I'm
more than a bit embarrassed to think how dense you must think I am to
have had to explain pointers...<gulp>....

Let me reset things a bit, along with some updated info. First, let me
offer that I fixed the original problem of VB6 complaining about the
"automation type unsupported by Visual Basic." In fixing *that*
problem, it ALSO fixed the way .NET generated the type library for the
Interop assembly. Those issues dovetail in a way that probably explains
the whole problem better than I have so far.

Here's how I got there. In thinking about the typelib for the COM
interop method, I realized what VB was complaining about; it
essentially saw a declaration requesting that a SafeArray be passed
ByVal, which almost certainly scrambled VB's brain. It can only pass
them by reference. That implies that the method signature needed to be
SAFEARRAY(unsigned char)*. I needed a "control" to verify the theory,
and that led me to building the throwaway VB dll that expected a byte
array parameter so I could inspect *its* type library...Sure enough,
the type library for a VB6 DLL expecting a byte array parameter ends up
looking like

HRESULT MyFunction([in, out] SAFEARRAY(unsigned char)* MyArray);

WIth that in hand, that led me to the fix on the .NET side.......

The fix was to change the method parameter signature in the C# Interop
DLL from "byte[]" to "ref byte[]". This had the downstream effect of
changing the method signature in the COM type library to
SAFEARRAY(unsigned char)*, which is *precisely* what I expected/needed.
When I call the function from VB, it now works perfectly.

A different way of seeing my problem can be illustrated in the
following C# example:

void SomeMethod(System.Byte[] lotsabytes, ref byte[] morebytes);

If marked for COM Interop, the type library for SomeMethod will look
something like:

HRESULT SomeMethod([in] SAFEARRAY(unsigned char) lotsabytes, [in]
SAFEARRAY(unsigned char)* morebytes)

VB can't pass an array by value, as implied by the first declaration,
but it can pass the array by reference. The latter declaration is
consistent with how .NET writes the COM Interop type library for a
function *returning* a byte array, but not for a *parameter*. That's
what had me puzzled; declarations for the same type were being written
two different ways. Changing the declaration of the array to "ref"
resolved the problem, and everything now works the way I
intended/expected.

I realize you're probably tired of my rambling explanations, but
hopefully THIS version of the explanation was an approach from a
different angle that clarified what I was trying to do. I appreciate
your patient and polite help nonetheless, and I hope you don't think
I'm a *complete* idiot. :)

David

.



Relevant Pages

  • Re: Array of value types
    ... > The structure directly represents a chunk of a binary file. ... Interop is far from a specialty of mine:( ... However, if you have an array of references, you *probably* won't be ... Ask in the interop group for more ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array of value types
    ... Regards ... Given that IFDENTRY is a structure, and ifd.entries is a field of type ... is this causing a copy as well for each item in the array? ... Interop is far from a specialty of mine:( ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array of value types
    ... Dennis JD Myrén ... >> to create the structure instance from a byte array. ... > able to pass that as an array of that type by interop, ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Array of value types
    ... Given that IFDENTRY is a structure, and ifd.entries is a field of type ... is this causing a copy as well for each item in the array? ... Interop is far from a specialty of mine:( ... increasingly marginal performance benefits of using indexing. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Need help on PHP for MPE/ix
    ... If one item is an array, it must be declared as such in the list ... So, please show us the declaration of the record buffer, and its ... Of Pavan Kumar Rati ... Need help on PHP for MPE/ix ...
    (comp.sys.hp.mpe)

Loading