473,473 Members | 2,097 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Change values in SafeArray

I have some C++ code which creates a SafeArray and passes it to my C#
dll using COM:

SAFEARRAY *psaOut = SafeArrayCreateVector(VT_BSTR, 0, 3);

VARIANT pVarOut;
VariantInit(&pVarOut);
pVarOut.vt = VT_ARRAY | VT_BSTR;
pVarOut.parray = psaOut;

InvokeHelper(DispId, DISPATCH_METHOD, VT_I4, (void*)&result, parms,
&pVarIn, &pVarOut);

My C# code can see this fine. However, if I change an element of the
array, it isnt reflected when I return.

My question is, is my C# method signature the problem:

[Out,MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_BSTR)] ref String[] OutputArray

I assume this is the correct signature for what I need to do, since I
can access the array with no problems.

Might be a tricky one, but any help would be appreciated.

Steven


*** Sent via Developersdex http://www.developersdex.com ***
Jan 15 '08 #1
4 6529
"Steven Blair" <st**********@btinternet.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>I have some C++ code which creates a SafeArray and passes it to my C#
dll using COM:

SAFEARRAY *psaOut = SafeArrayCreateVector(VT_BSTR, 0, 3);

VARIANT pVarOut;
VariantInit(&pVarOut);
pVarOut.vt = VT_ARRAY | VT_BSTR;
pVarOut.parray = psaOut;

InvokeHelper(DispId, DISPATCH_METHOD, VT_I4, (void*)&result, parms,
&pVarIn, &pVarOut);

My C# code can see this fine. However, if I change an element of the
array, it isnt reflected when I return.

My question is, is my C# method signature the problem:

[Out,MarshalAs(UnmanagedType.SafeArray, SafeArraySubType =
VarEnum.VT_BSTR)] ref String[] OutputArray

I assume this is the correct signature for what I need to do, since I
can access the array with no problems.

Might be a tricky one, but any help would be appreciated.

Steven


*** Sent via Developersdex http://www.developersdex.com ***

You must pass the array by reference.
pVarOut.vt = VT_ARRAY|VT_BSTR|VT_BYREF;

Why are you using dispatch calls from C++, while early binding is perfectly
possible?

Willy.

Jan 15 '08 #2
For the moment, I am returning the Array as a return value and that
works but I will try the VT_BYREF.
It shouldn't matter that we are passing an array of BSTR, I was begining
to wonder if I could only pass ints etc?

As for the approach for calling the methods, InvokeHelper is the only
way I know to be honest. Do you mean link the dll in statically and make
calls this way?

Thank you for the reply, was worried no one would.

Steven

*** Sent via Developersdex http://www.developersdex.com ***
Jan 16 '08 #3
"Steven Blair" <st**********@btinternet.comwrote in message
news:OO**************@TK2MSFTNGP02.phx.gbl...
For the moment, I am returning the Array as a return value and that
works but I will try the VT_BYREF.
It shouldn't matter that we are passing an array of BSTR, I was begining
to wonder if I could only pass ints etc?

As for the approach for calling the methods, InvokeHelper is the only
way I know to be honest. Do you mean link the dll in statically and make
calls this way?
No, it's just a matter of using the COM support offered by the VC++
compiler.

Say you have a C# source file myAssembly, that declares/defines an interface
like this:

[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("xxxxxxxxxxxxxxxxxxxxxxx")]
public interface IFoo
{
void Bar(ref string[] param);
}
After building and registering this assembly (see regasm /tlb ...), you will
find a typelib cthat can be imported in your C++ code.

#import ".\myAssembly.tlb" no_namespace named_guids raw_interfaces_only

above will tell the pre-compiler to build a header from the tlb which will
automatically be included by the C++ compiler.
This header file contains all you need to use early binding using smart
pointer support for your COM interface(s).

// Initialize the COM library first (calling CoInitialize or
CoInitializeEx)
...

HRESULT hr = 0;
IFooPtr pFoo;
// create and fill the safearray
SAFEARRAY * sa = ....;
hr = pFoo.CreateInstance(__uuidof(IFoo));
if(!FAILED(hr))
{
pFoo->Bar(&sa); // call Bar passing a reference to a SAFEARRAY of
BSTR
// access safearray returned by C#
...
}
...

Please search MSDN for more details on using "COM support in C++".
Willy.



Jan 16 '08 #4
Ok,

Thanks for the help, much appreciated.

*** Sent via Developersdex http://www.developersdex.com ***
Jan 16 '08 #5

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

Similar topics

0
by: mic | last post by:
I'm stuck with following problem. Using webbrowser control I try to make custom-made request. I found working example of this written in VBS but porting it to Python doesn't seem to work. The key...
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...
1
by: Doug Taylor | last post by:
How do I write the C# equivalent of the following VC6 methods? They will be exposed on a COM interface and used by VC6, VB6, and .NET apps. int SetPolygon(SAFEARRAY(double)points); int...
5
by: Tronster Hartley | last post by:
I've written an ATL 7.1 component and have a C# 1.1 client that is consuming it. The ATL component has a method with a that returns a SAFEARRAY of INTs, but my C# component chokes on it. The...
5
by: Ahmad Jalil Qarshi | last post by:
i want to store binary data into my property. i read earlier positing on "microsoft.public.dotnet.languages.vc" group with subject as SAFEARRAY in attributed ATL7 Project. i followed the same...
1
by: Brendan Grant | last post by:
I’ve got a C# library that I’ve built into a COM component that will be used from a VC++ 6 application and while the creation of the COM object side of things seem to be working fine, using 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...
1
by: Christian Schmidt | last post by:
Hi all, I need to implement an unmanaged function that gets a SafeArray and hands it over to a managed function having the managed array-type. Using MarshalAs I can call unmanaged functions having...
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#...
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
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...
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: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.