473,461 Members | 1,806 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Passing SAFEARRAY of Bytes from VBScript to Managed COM object (C#

Attempting to pass a VBScript Array of Bytes to a C# Managed COM object has
resulted in some very undesirable and strange behavior. When definining a
method in my C# COM class as "void Write(byte[] bBytes)" or "void
Write(byte[] bBytes, int nLen)" or "int Write(byte[] bBytes, int
nLen)"VBScript returns a "Invalid procedure call or argument" exception.
However when I define the method as "int Write(byte[] bBytes)" it works just
fine. Unfortunately the method definition that works is not the one I need.
Regardless of whether one works or one doesn't work, they should all work.
Does anyone know why the CCW can't handle the method definition, and why it
is behaving in such a flakey manner?

Things I have tried to make it work:
- I have used the appropriate MarshalAs attribute in front of the byte[]
array.
- I have used the [ClassInterface(ClassInterfaceType.None)] attribute with
an explicit interface to make the tlbexp tool explicitly define the interface
methods.
Jul 21 '05 #1
2 13402
Complex types as arrays are expected to be VARIANT's when calling into COM
through COM interop.
So you have to declare them as Object (or ref Object for [in,out]) in
CSharp.

Here's a sample that illustrates this...

// CS file
using System.Runtime.InteropServices;
using System;

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("1ab6f6ea-83b5-4b16-934b-fb82d9af8e0a")]
public interface IDotNetInterface
{
int Write( Object aParam1); //[in]VARIANT
}

// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetIf")]
public class DotNetInterface : IDotNetInterface
{
public DotNetInterface() {}
public int Write( Object aParam1)
{
foreach(byte b in aParam1 as Array)
Console.WriteLine(b);
return (aParam1 as Array).Length; // Return Array.Length (will be
marshalled as a VARIANT containing an int type)
}
}

'VBS file
On Error Resume Next
Dim o
Dim ar 'Variant
Dim arr(2) 'Array 3 elements
arr(0) = CByte(1) 'add 1 as byte value in element 0
arr(1) = CByte(2)
arr(2) = CByte(3)
ar = arr 'set ar to array
set o = CreateObject("Test.DotNetIf")
r = o.Write (ar) 'pass Variant containing an array of bytes to callee
if Err.Number <> 0 then
Wscript.Echo "Failure :" & Err.Description
else
Wscript.Echo r
end if

Willy.
"Ken Layton" <Ke*******@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object
has
resulted in some very undesirable and strange behavior. When definining a
method in my C# COM class as "void Write(byte[] bBytes)" or "void
Write(byte[] bBytes, int nLen)" or "int Write(byte[] bBytes, int
nLen)"VBScript returns a "Invalid procedure call or argument" exception.
However when I define the method as "int Write(byte[] bBytes)" it works
just
fine. Unfortunately the method definition that works is not the one I
need.
Regardless of whether one works or one doesn't work, they should all work.
Does anyone know why the CCW can't handle the method definition, and why
it
is behaving in such a flakey manner?

Things I have tried to make it work:
- I have used the appropriate MarshalAs attribute in front of the byte[]
array.
- I have used the [ClassInterface(ClassInterfaceType.None)] attribute with
an explicit interface to make the tlbexp tool explicitly define the
interface
methods.

Jul 21 '05 #2
Note that you can also pass them byval as SAFEARRAY(VARIANT) (Variant array)

//CS file
public interface IDotNetInterface
{
int Write(object[] aParam1);
}
....

public int Write( object[] aParam1) // Object [] == SAFEARRAY(Variant)
{
foreach(byte b in aParam1) // Variant encapsulates a byte
Console.WriteLine(b);
return aParam1.Length;
}

'SCRIPT
Dim ar
Dim arr(2)
arr(0) = CByte(1)
arr(1) = CByte(2)
arr(2) = CByte(3)
ar = arr
WScript.Echo TypeName(arr)
set o = CreateObject("Test.DotNetIf")
r = o.Write ((ar)) 'Pass ar byval
'OR
r = o.Write ((arr)) 'Pass arr byval

Willy.

"Ken Layton" <Ke*******@discussions.microsoft.com> wrote in message
news:E9**********************************@microsof t.com...
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object
has
resulted in some very undesirable and strange behavior. When definining a
method in my C# COM class as "void Write(byte[] bBytes)" or "void
Write(byte[] bBytes, int nLen)" or "int Write(byte[] bBytes, int
nLen)"VBScript returns a "Invalid procedure call or argument" exception.
However when I define the method as "int Write(byte[] bBytes)" it works
just
fine. Unfortunately the method definition that works is not the one I
need.
Regardless of whether one works or one doesn't work, they should all work.
Does anyone know why the CCW can't handle the method definition, and why
it
is behaving in such a flakey manner?

Things I have tried to make it work:
- I have used the appropriate MarshalAs attribute in front of the byte[]
array.
- I have used the [ClassInterface(ClassInterfaceType.None)] attribute with
an explicit interface to make the tlbexp tool explicitly define the
interface
methods.

Jul 21 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
4
by: Roland Moschel | last post by:
Hi there ! I have some Problems to get a SafeArray out of a COM Server written in (Unmanaged) C++. From (unmanaged) Visual Basic , everything is ok , but unfortunately in C# I get an...
2
by: lolomgwtf | last post by:
I have a managed C++ method that wraps unmanaged code and creates a managed object holding data retrieved form an unmanged one. I want create an instance of this managed class in C#, pass it to...
2
by: Ken Layton | last post by:
Attempting to pass a VBScript Array of Bytes to a C# Managed COM object has resulted in some very undesirable and strange behavior. When definining a method in my C# COM class as "void Write(byte...
7
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The...
5
by: Maxim | last post by:
Hi all, I'm calling a COM Interface method that returnes SafeArray wrapped into variant. Is it possible to convert it to managed array? Because working with SAFEARRAY directly is a bit...
17
by: =?Utf-8?B?U2hhcm9u?= | last post by:
Hi Gurus, I need to transfer a jagged array of byte by reference to unmanaged function, The unmanaged code should changed the values of the array, and when the unmanaged function returns I need...
0
by: eitanyan | last post by:
I am trying to create a Java to .Net interop and the way I am doing it is by creating a C# com object and a native unmanaged c++ dll that uses JNIEnv of java. the java is loading the native c++ dll...
0
skeptics
by: skeptics | last post by:
Trying to pass a SafeArray that i get form Request.BinaryRead Method in classic asp to a c# com interop component. In this thread i found a solution to pass and cast a normal asp safearray in to a c#...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.