473,756 Members | 4,046 Online
Bytes | Software Development & Data Engineering Community
+ 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( ClassInterfaceT ype.None)] attribute with
an explicit interface to make the tlbexp tool explicitly define the interface
methods.
Jul 21 '05 #1
2 13435
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(C omInterfaceType .InterfaceIsDua l)]
[Guid("1ab6f6ea-83b5-4b16-934b-fb82d9af8e0a")]
public interface IDotNetInterfac e
{
int Write( Object aParam1); //[in]VARIANT
}

// class
[ClassInterface( ClassInterfaceT ype.None)]
[ProgId("Test.Do tNetIf")]
public class DotNetInterface : IDotNetInterfac e
{
public DotNetInterface () {}
public int Write( Object aParam1)
{
foreach(byte b in aParam1 as Array)
Console.WriteLi ne(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("T est.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*******@disc ussions.microso ft.com> wrote in message
news:E9******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.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(VARIA NT) (Variant array)

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

public int Write( object[] aParam1) // Object [] == SAFEARRAY(Varia nt)
{
foreach(byte b in aParam1) // Variant encapsulates a byte
Console.WriteLi ne(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("T est.DotNetIf")
r = o.Write ((ar)) 'Pass ar byval
'OR
r = o.Write ((arr)) 'Pass arr byval

Willy.

"Ken Layton" <Ke*******@disc ussions.microso ft.com> wrote in message
news:E9******** *************** ***********@mic rosoft.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( ClassInterfaceT ype.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
3551
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 this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
4
10323
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 exception. Can anybody help me ?
2
4682
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 this method and have it set the instance to hold the right data. >From what I've read it seems I should be able to pass C# objects to managed C++ methods and it should just work; however, when I try it, my C# instance comes out null. If I step...
2
426
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 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...
7
10238
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 information that needs to be passed can be stored as Key-Value pairs, and the method signature remains the same. That way, handling future requirements of passing additional details to the callee can be handled without changing the method signature. Is...
5
6350
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 complicated. Or maybe there is a managed wrapper class for safe array? Thanks in advance.
17
7253
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 to show the array data to the end user. Can I do that? How?
0
2901
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 and invokes a method in it which invokes a method on The command passed from java to .Net is a byte array and the response passed back is also a byte array the C# managed COM object which finally invokes a method on a regular C# managed dll....
0
3223
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# array: http://bytes.com/groups/net/114755-passing-safearray-bytes-vbscript-managed-com-object-c#goto_threadtools but no matter what i try. i cant pass a array that i get from Request.BinaryRead Method in to the component. i always get a error...
0
9255
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10014
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9844
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9689
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8688
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7226
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5119
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3780
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3326
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.