473,769 Members | 5,724 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how to pass array from managed to COM

Dear all,
I have written a C++ COM object:
STDMETHOD(myfun c)(INT* array)

Now I need to pass an array from c# to COM. I generated a COM interop and
the signature of the function is

myfunc(ref int)

in C# I have an array of ints.

If I call

myfunc(array[0])

and try to access from COM object the array[1],2.... they are incorrect.
Only array[0] is correct. What do I do wrong?

Thanks a lot,

Boni

Mar 16 '06 #1
6 1726
"Boni" <no****@parampa m.pam> wrote in message
news:OB******** ******@TK2MSFTN GP10.phx.gbl...
Dear all,
I have written a C++ COM object:
STDMETHOD(myfun c)(INT* array)

Now I need to pass an array from c# to COM. I generated a COM interop and
the signature of the function is

myfunc(ref int)

in C# I have an array of ints.

If I call

myfunc(array[0])

and try to access from COM object the array[1],2.... they are incorrect.
Only array[0] is correct. What do I do wrong?

Thanks a lot,

Boni

Your function definion is: STDMETHOD(myfun c)(INT* array)

From this function definition it is not clear what the size of the array
should be. I assume that the client and the server know the size of the
array for a reason not described so far. In this case you should define you
P/Invoke function like this:

[DllImport(".... ")]
static extern myfunc([MarshalAs(LPAra y int[] rgi);

Marcus
Mar 16 '06 #2
Hi Marcus,
I assume that the client and the server know the size of the array for a
reason not described so far.

No the size is passed as a second function argument (sorry I skiped this in
the description)
STDMETHOD(myfun c)(INT* array, int size).
And I don't use PInvoke explicitely (Visual Studo generats Interop for me).


Mar 16 '06 #3
"Boni" <oilia@nospam > wrote in message
news:uO******** ******@TK2MSFTN GP11.phx.gbl...
Hi Marcus,
I assume that the client and the server know the size of the array for a
reason not described so far.

No the size is passed as a second function argument (sorry I skiped this
in the description)
STDMETHOD(myfun c)(INT* array, int size).
And I don't use PInvoke explicitely (Visual Studo generats Interop for
me).


The COM Interop Type information generated by VS, by TLBIMP, and by
System::Runtime ::Interop::Type LibConverter is not sufficient for what you
need. This is especially the case, because the COM Type Info does not have
all the information that is necessary.

It is very likely that you have to write the type information yourself.
[
object,
uuid(... your IID ...),
oleautomation
]
interface IYourInterface : IUnknown {
STDMETHOD(myfun c)([size_is(size)] INT* array, int size);
}

in this case, you can define Interop Type Info like this one:

[
Guid("... your IID ..."),
InterfaceType(C omInterfaceType .InterfaceIsIUn known)
]
public interface IYouInterface
{
void myFunc([MarshalAs(Unman agedType.LPArra y, SizeParamIndex= 1)] int[]
rgints, int size);
}


Mar 16 '06 #4
Thanks a lot Marcus, I will try that.
"Marcus Heege" <NO****@heege.n et> schrieb im Newsbeitrag
news:%2******** ********@TK2MSF TNGP14.phx.gbl. ..
"Boni" <oilia@nospam > wrote in message
news:uO******** ******@TK2MSFTN GP11.phx.gbl...
Hi Marcus,
I assume that the client and the server know the size of the array for a
reason not described so far.

No the size is passed as a second function argument (sorry I skiped this
in the description)
STDMETHOD(myfun c)(INT* array, int size).
And I don't use PInvoke explicitely (Visual Studo generats Interop for
me).


The COM Interop Type information generated by VS, by TLBIMP, and by
System::Runtime ::Interop::Type LibConverter is not sufficient for what you
need. This is especially the case, because the COM Type Info does not have
all the information that is necessary.

It is very likely that you have to write the type information yourself.
[
object,
uuid(... your IID ...),
oleautomation
]
interface IYourInterface : IUnknown {
STDMETHOD(myfun c)([size_is(size)] INT* array, int size);
}

in this case, you can define Interop Type Info like this one:

[
Guid("... your IID ..."),
InterfaceType(C omInterfaceType .InterfaceIsIUn known)
]
public interface IYouInterface
{
void myFunc([MarshalAs(Unman agedType.LPArra y, SizeParamIndex= 1)] int[]
rgints, int size);
}

Mar 17 '06 #5
Just one more small question
void myFunc([MarshalAs(Unman agedType.LPArra y, SizeParamIndex= 1)] int[]

Is the pointer passed to the array a pointer to initial array or to a copy
of this array? I.e. If I change data inside of COM using this pointer, will
initial data change?
Mar 17 '06 #6
No, you pass a copy, so you can't change the managed arry this way.
Anyway, the easiest way of passing array's to/from COM is by means of self
describing SAFEARRAY's. The default COM interop marshaler will take care of
data marshaling for automation types.

IDL:
HRESULT PassInOutIntArr ay([in, out] SAFEARRAY(long) * arr);

Declaration:
....

STDMETHOD(PassI nOutIntArray)(S AFEARRAY ** arr);

Definition:

STDMETHODIMP CTest::PassInOu tIntArray(SAFEA RRAY ** arr)
{
int* temp;
SafeArrayAccess Data(*arr, (void**)&temp);
long ubound;
SafeArrayGetUBo und(*arr, 1, &ubound);
for(int i = 0; i <= ubound; i++)
{
temp[i] = temp[i] * 2; // each element * 2
}
SafeArrayUnacce ssData(*arr);
S_OK;
}

C#
....
Array iar = new int[10] { 12, 23, 45, 7, 89, 25, 12, 8, 9, 5 };a;
ComObj.PassInOu tIntArray(ref iar );
foreach(int in iar)
....

No need to define the interface in C#, just set a reference to the typelib
and you are done.
Willy.
"Boni" <oilia@nospam > wrote in message
news:eq******** ******@TK2MSFTN GP09.phx.gbl...
| Just one more small question
| >> void myFunc([MarshalAs(Unman agedType.LPArra y, SizeParamIndex= 1)]
int[]
| Is the pointer passed to the array a pointer to initial array or to a copy
| of this array? I.e. If I change data inside of COM using this pointer,
will
| initial data change?
|
|
Mar 17 '06 #7

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

Similar topics

2
4582
by: Matthew Louden | last post by:
When I pass an array as a function parameter, it yields the following compile error. Any ideas?? However, if I create a variable that holds an array, and pass that variable to the function parameter, then it's working fine. Any ideas?? Thanks! public static int getFreq(string s) {... } int res = getFreq({"eee", "ewsww"});
3
1809
by: J | last post by:
I'm at a loss on how to accomplish one item with C# entirely in managed code -- I'd like to make a call to a Win32 function, one of its parameters is a structure that contains a pointer to one or more structures. How do I setup the structure in C# to allow for the pointer to a structure? The structure that I'm referring to is MIXERLINECONTROLS and it's declaration is the following: typedef struct { DWORD cbStruct;
1
2428
by: wbaccay | last post by:
I have a byte of binary data received from a NetworkStream (C# code) that I need to pass to the IWMWriter object in a DLL written in Managed extensions for C++ (since the Windows Media SDK is not usable in C#) whose WriteSample function takes a byte* parameter Do I need to marshal data? I thought I did, so I ran into alot of issues with converting the byte to a string and then, in the MC++ DLL, marshalling that to an IntPtr via...
16
14138
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the byteArray and afterwards see the changes in C#. (if you wanna know more details: the goal is to write the content of an existing char-array in c++ precisely into the passed byteArray, so that after the function has proceded the content of the...
7
8485
by: SB | last post by:
What is the proper way to pass a character array (char *) from a "C" dll to a C# method (delegate) in my app? Getting the dll (which simulates a third party dll) to call my delegate works fine. However, as soon as I try to add any "char *" parameters, I start getting exceptions in my C# app. So far I've tried (among others): public delegate int Test(char version); and public delegate int Test(string version); // won't work because...
0
1091
by: Cindy | last post by:
I have created a managed C++ wrapper class to wrap the unmanaged C++ dll. The GUI is using C#. In C#, I want to pass an int array to my managed C++ class to get the result back to C#. Something likes below In C#: public void FuncA() { int a; a=new int; a=1; ...
5
2721
by: apm | last post by:
Any and all: Is there an efficient way to pass a large array from .NET to COM? Can references (or pointer) be passed from COM to NET and NET to COM without the object it refers to being copied? Thanks in advance. David
3
2294
by: Boni | last post by:
Dear all, I have written a C++ com object: STDMETHOD(myfunc)(INT* array) Now I need to pass an array from c# to com. I generated a COM interop and it the signature of the function is myfunc(ref int)
1
2555
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by reference: //Managed c++ QueryResult* qr = NULL; ExecuteQuery(1, "abc", qr); //invoke native c++ method passing qr by reference
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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
10211
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
10045
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...
1
9994
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5298
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...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3958
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
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.