4
votes

I am doing an application that comunicates vb6 with a cryptographic wrapper. The .net and interop part, up to now, is alright, fully working.
As my client is testing It, I just have a quick question:

[ComVisible(true)]
public SomeObjectComVisible GetThat(byte[] array){ ... }

I used, until now, either types that I exposed to com or int and string, and no problems until now.

Is it ok to use (.net) byte or chould I use *char?
When I mark the assembly to be visible and register to com interop, it creates a wrapper for it, or should I use some unmanaged type?

Ah, it is a vb6, not vbscript.

thanks a million

for those who seek the answer:

public SomeObjectComVisible GetThat([MarshalAs(UnmanagedType.SafeArray, SafeArraySubType = VarEnum.VT_UI1)]byte[] array)

the problem is with arrays. http://msdn.microsoft.com/en-us/library/z6cfh6e6.aspx and http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx

Any non bittable type can be a chore. You can specify your own types so they are used, you just have to make use of

[ComVisible(true), 
ClassInterface(ClassInterfaceType.None),
ProgId("SomeNamespace.SomeClass"),
Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]

on top of the class

Thank you very much you all.

Great help

3

3 Answers

8
votes

Marshaling of arrays is something I struggle with often when dealing with COM clients of my .Net code. This article I find very useful in helping me understand the process.

Blittable and Non-Blittable Types

Specifically you can look at this article which talks about arrays

Note: part of my original answer which we found to be incorrect

So from looking at that it looks like "byte" isn't blitable yet "Byte" is. If you switch to Byte[] it will likely work the way you expect it to. Note: char isn't blitable but Char is.

6
votes

Try this:-

[ComVisible(true)]
public SomeObjectComVisible GetThat([MarshalAs(UnmanagedType.AsAny)] byte[] array){ ... }

If that doesn't work, you can try different values of the UnmanagedType enum to see if you can find one which works.

Alternatively, you may have to mark the parameter as a ref, i.e.

[ComVisible(true)]
public SomeObjectComVisible GetThat(ref byte[] array){ ... }

(Or perhaps a combination of the above.)

NOTE - make sure you regenerate the .tlb file after each change.

0
votes

Very old thread, but for VB.NET to VB6, e.g. ByRef bytes as Byte() works a treat. If ByRef is omitted and bytes is passed ByVal (the default), VB6 throws a "Function marked as restricted or uses a type not supported in Visual Basic" error.