Search:
Namespaces
Discussions
.NET v1.1
Feedback
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
Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us
-
Terms of Use
-
Privacy Policy
-
www.dotnet247.com