Accepting a Byte array, vb6 to C# interop

瘦欲@ 提交于 2019-12-07 11:27:31

问题


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


回答1:


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.




回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/8356418/accepting-a-byte-array-vb6-to-c-sharp-interop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!