.NETGURU
OLEAutomation Interop with object
Messages   Related Types
This message was discovered on microsoft.public.dotnet.framework.interop.
Responses highlighted in red are from those people who are likely to be able to contribute good, authoratitive information to this discussion. They include Microsoft employees, MVP's and others who IMHO contribute well to these kinds of discussions.
Post a new message to this list...

Phil Wilson
There doesn't seem to be an obvious (to me anyway!) way to have an interface
in .NET that passes an object [] such that it can be used by (say) VBScript.
In other words, given a method:

[ComVisible(true), GuidAttribute("66F12379-0C8F---------- etc")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ProgId("blah.blah")]
public class Class1: IMyInterface
{
..
public void Connect(object [] initializeData )

}

What is required (MarshalAs or whatever) is that this kind of script works:

set obj = createobject("blah.blah")
dim parm(2)
parm (1) = "this"
parm(2)="that"

obj.Connect parm

The error is pretty consistent at 0x800A0005 Invalid procedure call or
argument: 'obj.Connect'

Help Welcome, thanks.
--
Phil Wilson [MVP Windows Installer]
----

Reply to this message...
 
    
Robert Jordan
Hi Phil,

[Original message clipped]

You don't need any special marschal instructions, because
the runtime already has a default marschaler for "object[]".

However, VBScript is not able to pass the expected SAFEARRAY
by value. It simply doesn't support that. You have to change
the managed signature from

public void Connect(object [] initializeData )

to

public void Connect(ref object [] initializeData )

[Original message clipped]

Rob
Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
"Robert Jordan" <Click here to reveal e-mail address> wrote in message
news:cgmqf4$980$00$Click here to reveal e-mail address...
> Hi Phil,

[Original message clipped]

Sure it has just put the arg between parens like this:

obj.Connect (parm)

when calling a function use double parens:
r = obj.Connect((param))

Following calls a method with two params one byval another byref.

r = obj.Method((p1), p2)

Willy.

Reply to this message...
 
    
Robert Jordan
[Original message clipped]

Thanks for the syntax. Does the interop now work?

Rob
Reply to this message...
 
    
Phil Wilson
It's not a syntax issue - that's just me doing sloppy cut&paste - the issue
is the 0x800A0005 Invalid procedure call or
argument: 'obj.Connect'. I tried a ref on the object [], still doesn't work.
This is the entire class library and VBScript. I just have a nagging feeling
it should work with the right incantation somewhere.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace TestCL
{
[ComVisible(true), GuidAttribute("EBFCDECB-CF01-467c-A15E-D57ABE4E3C6F")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ProgId("blah.blah")]
public class Class1
{
public void Connect(ref object [] initializeData )
{
MessageBox.Show (initializeData[0].ToString());
}
}
}

' VBScript source code
set obj = createobject("blah.blah")
dim parm(2)
parm (1) = "this"
parm(2)="that"
obj.Connect parm
--
Phil Wilson
[MVP Windows Installer]

"Robert Jordan" <Click here to reveal e-mail address> wrote in message
news:cgmvoe$jt3$06$Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
Yes it is a syntax issue, try and take a look at following small sample:

1. CS file inprocserver.cs

// Compile with csc /t:library inprocserver.cs
// and use Regasm /codebase inprocserver.dll to register
using System.Runtime.InteropServices;
using System;

// interface
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("1ab6f6ea-83b5-4b16-934b-fb98d9af8e0a")]
public interface IDotNetInterface
{
int WriteEx(ref object[] aParam1, string s); }
// class
[ClassInterface(ClassInterfaceType.None)]
[ProgId("Test.DotNetIf")]
public class DotNetInterface : IDotNetInterface
{
public DotNetInterface() {}
public int WriteEx(ref object[] aParam1, string s)
{
Console.WriteLine("Length: {0}", aParam1.Length);
int i = 0;
foreach(byte b in aParam1)
{
Console.WriteLine(b);
aParam1[i] = (byte)aParam1[i] + 1; // change each element in the array
i++;
}
return aParam1.Length
}
}

And the VBScript file:

Dim arr(2)
' pass byte array just for the fun
arr(0) = CByte(10)
arr(1) = CByte(20)
arr(2) = CByte(30)
set o = CreateObject("Test.DotNetIf")
r = o.WriteEx ((arr), "test")
' uncomment next to see how it fails
' r = o.WriteEx (arr, "test")
for each b in arr
WScript.Echo "Array elem = " & b
next

Feel free to ask more questions.

Willy.

"Phil Wilson" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Phil Wilson
Ok, got it, many thanks. Those double parentheses are what I hadn't noticed.
--
Phil Wilson
[MVP Windows Installer]
"Willy Denoyette [MVP]" <Click here to reveal e-mail address> wrote in message
news:Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
    
Willy Denoyette [MVP] (VIP)
Phil,

When using VBScript as client it's better to consider all arguments passes
as VARIANT's in C#. Note that VBScript considers all variables as variants,
but internaly the scripting engine uses the real variant types, so you need
to declare a variant to be passed as arg. Here's a sample.

public int ModArray(ref object aParam1)
{
object[] arr = aParam1 as object[]; // aParam1 is a now a VARIANT wrapping
a SAFEARRAY of VARIANT's
int i = 0;
foreach(object b in arr){ // for each object (VARIANT) in arr
Console.WriteLine(b);
arr[i] =(byte)( (int)arr[i] - 1); // change the values
i++;
}
return (aParam1 as Array).Length;
}

Dim arr(2)
' pass byte array just for the fun
arr(0) = CByte(10)
arr(1) = CByte(20)
arr(2) = CByte(30)
set o = CreateObject("Test.DotNetIf")
dim var 'declare variant variable
var = arr 'set variant variable to array
r = o.ModArray(var) 'pass variant not the array
for each b in var
WScript.Echo "Array elem = " & b
next

Willy.

"Phil Wilson" <Click here to reveal e-mail address> wrote in message
news:%Click here to reveal e-mail address...
[Original message clipped]

Reply to this message...
 
 
System.Console
System.Runtime.InteropServices.ClassInterfaceType
System.Runtime.InteropServices.ComInterfaceType
System.Runtime.InteropServices.GuidAttribute
System.Windows.Forms.MessageBox




ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
Ad


Need Dot Net Interview Questions?
Ask ExamGuru, Inc. for advice and help on Passing .Net Interviews
.Net Projects
Best-of-breed application framework for .NET projects, developed by ExamGuru, Inc. and ExamGuru IT
Free .net Help
Commission ExamGuru, Inc. and his team for your next bespoke software project
FogBUGZ
The only bug tracking system carefully crafted with one goal in mind: helping teams create great software.
Awesome Tools
If you don't know about these, you're missing out... IT Certification Questions
IT Interview Questions
Free Oracle 10g Training
MCSE Boortcamp
Cisco Study Guides
Cheap Study Guides
Exact Questions
Dot Net Interview Questions
Oracle OCP
Cheap Travel
Designer Perfumes - Wholesale Prices
Free Programming Tutorials
 
ExamGuru IT Solutions - .Net Guru is owned and operated by ExamGuru, Inc., the man behind .Net Guru. If you're in the market for bespoke software or software consultancy, why not get him and his highly trained team to help? - www.examguru.net/ITCertification
 Copyright © ExamGuru, Inc. 2001-2006
Contact Us - Terms of Use - Privacy Policy - www.dot-net-guru.com - www.examguru.net - www.oraclesource.net - www.itinterviews.net - www.examguru.net/ITCertification