473,770 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with Array from C# to COM

Hi,

i have a problem with a Type Library, which is written in C++.
I am developing an application in C#.NET and have to use functions from
this COM-Type Library. When I use these functions in the "old" VB it
works but not in .NET. I think it is a problem with marshalling but I
could not find a solution yet.

First I included the Type Library in VS.NET 2003. Other functions of
the TL work so the reference to the Lib must be correct.

Here the piece of code in C#.NET:

app = new nwApplication() ;
int[] id = new int[2];
id[0] = 1;
id[1] = 2;

broadcast = app.get_broadca st(id);

app is an Application-Object of the Library. The array id has two
numbers. The get_broadcast method needs this array as parameter and
returns a broadcast-object.

Here is the PROBLEM. I seems that the get_broadcast method does not get
the content of the array correctly.

I tried it in VB like this:

Dim app, broadcast, id
id = Array(1,2)
Set app = CreateObject("N wLib.nwApplicat ion")
Set broadcast = app.get_broadca st(id)

This works correctly.

When I look for the get_broadcast message in the object browser the
definition is like this:
nwApplication.g et_broadcast(ob ject)

When i compile my project, VS.NET creates a Interop.nw.dll. I
disassembled it to IL and looked for the definitions of the method. It
was like this:

......... get_broadcast(o bject marshal( struct) Value)....

Is it possible that the array in C# is not correctly marshalled?

Has anyone an idea where the problem could be? Thanks very much!

Peter

Jan 23 '06 #1
9 4098
It sounds likely that the array is not being correctly marshalled. For the
C# code you are using, you may have to modify your interop assembly usiing
ildasm, a text editor, and ilasm to compile back. The marshalling in the IL
may have to be changed to something like

marshal(int32[])

or even

marshal(safearr ay int32)

However I don't know what the method definition looks like in your TLB. The
marshalling specified in the interop dll may well be correct but what you
are trying to do in your C# is incorrect. (I don't know what the VB Array
operation does.)

"mupe" <p.*******@eise nmann.de> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi,

i have a problem with a Type Library, which is written in C++.
I am developing an application in C#.NET and have to use functions from
this COM-Type Library. When I use these functions in the "old" VB it
works but not in .NET. I think it is a problem with marshalling but I
could not find a solution yet.

First I included the Type Library in VS.NET 2003. Other functions of
the TL work so the reference to the Lib must be correct.

Here the piece of code in C#.NET:

app = new nwApplication() ;
int[] id = new int[2];
id[0] = 1;
id[1] = 2;

broadcast = app.get_broadca st(id);

app is an Application-Object of the Library. The array id has two
numbers. The get_broadcast method needs this array as parameter and
returns a broadcast-object.

Here is the PROBLEM. I seems that the get_broadcast method does not get
the content of the array correctly.

I tried it in VB like this:

Dim app, broadcast, id
id = Array(1,2)
Set app = CreateObject("N wLib.nwApplicat ion")
Set broadcast = app.get_broadca st(id)

This works correctly.

When I look for the get_broadcast message in the object browser the
definition is like this:
nwApplication.g et_broadcast(ob ject)

When i compile my project, VS.NET creates a Interop.nw.dll. I
disassembled it to IL and looked for the definitions of the method. It
was like this:

......... get_broadcast(o bject marshal( struct) Value)....

Is it possible that the array in C# is not correctly marshalled?

Has anyone an idea where the problem could be? Thanks very much!

Peter

Jan 23 '06 #2
....I'm also slightly surprised that the IL doesn't mark the parameter with
[in] (unless you missed that out).

Can you supply the method signature in the type library and the full method
definition in IL?

"mupe" <p.*******@eise nmann.de> wrote in message
news:11******** *************@f 14g2000cwb.goog legroups.com...
Hi,

i have a problem with a Type Library, which is written in C++.
I am developing an application in C#.NET and have to use functions from
this COM-Type Library. When I use these functions in the "old" VB it
works but not in .NET. I think it is a problem with marshalling but I
could not find a solution yet.

First I included the Type Library in VS.NET 2003. Other functions of
the TL work so the reference to the Lib must be correct.

Here the piece of code in C#.NET:

app = new nwApplication() ;
int[] id = new int[2];
id[0] = 1;
id[1] = 2;

broadcast = app.get_broadca st(id);

app is an Application-Object of the Library. The array id has two
numbers. The get_broadcast method needs this array as parameter and
returns a broadcast-object.

Here is the PROBLEM. I seems that the get_broadcast method does not get
the content of the array correctly.

I tried it in VB like this:

Dim app, broadcast, id
id = Array(1,2)
Set app = CreateObject("N wLib.nwApplicat ion")
Set broadcast = app.get_broadca st(id)

This works correctly.

When I look for the get_broadcast message in the object browser the
definition is like this:
nwApplication.g et_broadcast(ob ject)

When i compile my project, VS.NET creates a Interop.nw.dll. I
disassembled it to IL and looked for the definitions of the method. It
was like this:

......... get_broadcast(o bject marshal( struct) Value)....

Is it possible that the array in C# is not correctly marshalled?

Has anyone an idea where the problem could be? Thanks very much!

Peter

Jan 23 '06 #3
Hi,

yes i will post the code tomorrow when I am back in office.

I already disassembled the assembly to IL and then I tried to recompile
it but that did not work.
So I disassembled it and recompiled it without any changes but then I
got an error message
that an entry point was not found and the dll was not created. I will
post the exact error message tomorrow.

Thanks so long.

Jan 23 '06 #4
I don’t know what is the problem but as far as you received the
[nwApplication.g et_broadcast(ob ject)]
Object that means you receive the address of the array object and you just
need to work out the indexes/values of your object?

If I missed your point ignore the message.

Vlad P

"mupe" wrote:
Hi,

i have a problem with a Type Library, which is written in C++.
I am developing an application in C#.NET and have to use functions from
this COM-Type Library. When I use these functions in the "old" VB it
works but not in .NET. I think it is a problem with marshalling but I
could not find a solution yet.

First I included the Type Library in VS.NET 2003. Other functions of
the TL work so the reference to the Lib must be correct.

Here the piece of code in C#.NET:

app = new nwApplication() ;
int[] id = new int[2];
id[0] = 1;
id[1] = 2;

broadcast = app.get_broadca st(id);

app is an Application-Object of the Library. The array id has two
numbers. The get_broadcast method needs this array as parameter and
returns a broadcast-object.

Here is the PROBLEM. I seems that the get_broadcast method does not get
the content of the array correctly.

I tried it in VB like this:

Dim app, broadcast, id
id = Array(1,2)
Set app = CreateObject("N wLib.nwApplicat ion")
Set broadcast = app.get_broadca st(id)

This works correctly.

When I look for the get_broadcast message in the object browser the
definition is like this:
nwApplication.g et_broadcast(ob ject)

When i compile my project, VS.NET creates a Interop.nw.dll. I
disassembled it to IL and looked for the definitions of the method. It
was like this:

......... get_broadcast(o bject marshal( struct) Value)....

Is it possible that the array in C# is not correctly marshalled?

Has anyone an idea where the problem could be? Thanks very much!

Peter

Jan 23 '06 #5
@Vlad P: No, the object I get back from the method needs the value of
the array for internal use. And because of the object´s behaviour I
think that it does not get the value of the array correctly.

@Clive Dixon: Recompilation with ilasm now works. I forgot some options
yesterday. Here I have the pieces of IL code I found in the nw.il after
disassembling it from Interop.Nw.dll. The names of methods could be
different to those I posted yesterday but nwBroadcast is the object I
get back from the method get_nwBroadcast ().

.method public hidebysig newslot specialname virtual
instance class nwLib.nwBroadca st
marshal( interface)
get_nwBroadcast (object marshal( struct) Value) runtime
managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.override nwLib.InwApplic ation::get_nwBr oadcast
} // end of method nwApplicationCl ass::get_nwBroa dcast

....

.property class nwLib.nwBroadca st
nwBroadcast(obj ect)
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.get instance class nwLib.nwBroadca st
nwLib.nwApplica tionClass::get_ nwBroadcast(obj ect)
} // end of property nwApplicationCl ass::nwBroadcas t

....

.method public hidebysig newslot specialname abstract virtual
instance class nwLib.nwBroadca st
marshal( interface)
get_nwBroadcast (object marshal( struct) Value) runtime
managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
} // end of method InwApplication: :get_nwBroadcas t

....

.property class nwLib.nwBroadca st
nwBroadcast(obj ect)
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.get instance class nwLib.nwBroadca st
nwLib.InwApplic ation::get_nwBr oadcast(object)
} // end of property InwApplication: :nwBroadcast

Thats all I found to this method.

In another method I get back an object which I cast to System.Array in
my C#-Code. I found in IL code that this is also marshalled as struct.
But that works without any problems?! Here the code:

.method public hidebysig newslot abstract virtual
instance object
marshal( struct)
ScanNet([in] int32 lngStartID,
[in] int32 lngEndID) runtime managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 03 00 00 00 00 00 )
} // end of method InwNetwork::Sca nNet
I also tried marshal( int32[]) like you told me yesterday but that did
not work. I got an error message: "parameter #1 can not be marshalled.
Invalid managed/unmanaged type combination (the object class has to be
combined with Interface, IUnknown, IDispatch, AsAny or Struct."

I hope you can help me with the code above. Thanks in advance!

Peter

Jan 24 '06 #6
I viewed the Type Lib in the C++ TypeLib Viewer and there it looked
likes this:

[id(0x0000000f), propget, helpstring("pro perty nwBroadcast")]
HRESULT nwBroadcast(
VARIANT Value,
[out, retval] InwBroadcast** pVal);

Since my last entry I tried more things but nothing works....

Jan 24 '06 #7
Seems odd that the System.Array to VARIANT (IL 'struct') marshalling happens
one way but not the other.

Next up, I would suggest you try manually marshalling the array to VARIANT
before you pass it, using Marshal.GetNati veVariantForObj ect.

You will first need to allocate some memory for the out VARIANT using
Marshal.AllocCo TaskMem. I don't immediately see a way of allocating the
exact size for a VARIANT in C# so you'll probably have to try giving it a
"sufficient ly large" amount (from memory a VARIANT is 16 bytes, but don't
quote me on that).

"mupe" <p.*******@eise nmann.de> wrote in message
news:11******** ************@o1 3g2000cwo.googl egroups.com...
@Vlad P: No, the object I get back from the method needs the value of
the array for internal use. And because of the object´s behaviour I
think that it does not get the value of the array correctly.

@Clive Dixon: Recompilation with ilasm now works. I forgot some options
yesterday. Here I have the pieces of IL code I found in the nw.il after
disassembling it from Interop.Nw.dll. The names of methods could be
different to those I posted yesterday but nwBroadcast is the object I
get back from the method get_nwBroadcast ().

.method public hidebysig newslot specialname virtual
instance class nwLib.nwBroadca st
marshal( interface)
get_nwBroadcast (object marshal( struct) Value) runtime
managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.override nwLib.InwApplic ation::get_nwBr oadcast
} // end of method nwApplicationCl ass::get_nwBroa dcast

....

.property class nwLib.nwBroadca st
nwBroadcast(obj ect)
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.get instance class nwLib.nwBroadca st
nwLib.nwApplica tionClass::get_ nwBroadcast(obj ect)
} // end of property nwApplicationCl ass::nwBroadcas t

....

.method public hidebysig newslot specialname abstract virtual
instance class nwLib.nwBroadca st
marshal( interface)
get_nwBroadcast (object marshal( struct) Value) runtime
managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
} // end of method InwApplication: :get_nwBroadcas t

....

.property class nwLib.nwBroadca st
nwBroadcast(obj ect)
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 0F 00 00 00 00 00 )
.get instance class nwLib.nwBroadca st
nwLib.InwApplic ation::get_nwBr oadcast(object)
} // end of property InwApplication: :nwBroadcast

Thats all I found to this method.

In another method I get back an object which I cast to System.Array in
my C#-Code. I found in IL code that this is also marshalled as struct.
But that works without any problems?! Here the code:

.method public hidebysig newslot abstract virtual
instance object
marshal( struct)
ScanNet([in] int32 lngStartID,
[in] int32 lngEndID) runtime managed internalcall
{
.custom instance void
[mscorlib]System.Runtime. InteropServices .DispIdAttribut e::.ctor(int32)
= ( 01 00 03 00 00 00 00 00 )
} // end of method InwNetwork::Sca nNet
I also tried marshal( int32[]) like you told me yesterday but that did
not work. I got an error message: "parameter #1 can not be marshalled.
Invalid managed/unmanaged type combination (the object class has to be
combined with Interface, IUnknown, IDispatch, AsAny or Struct."

I hope you can help me with the code above. Thanks in advance!

Peter
Jan 24 '06 #8
This is the code I tried:

IntPtr mem;

int[] appId = new int[2];
appId[0] = 1;
appId[1] = 2;

mem = Marshal.AllocCo TaskMem(64); // also tried 32.....

Marshal.GetNati veVariantForObj ect(appId, mem);

nwBroad = nwApp.get_nwBro adcast(mem);

It does not work. It has the same effect like using appId directly as
parameter......

Do I have to edit the IL code for this too??

Thanks.

Jan 24 '06 #9
Today I tried something new. I wrote a VB6-DLL as "debug layer" which
gets the array as parameter. This dll hands the array over to the
getBroadcast method (in VB6 the whole thing works, so I chose VB6). I
returned the array from the dll back to my c# application and I got the
array with the correct numbers in it... but...
....if I hand over the array without doing anything other it does not
work.
If I define a new array in the dll and copy the content of the array I
get as parameter into that new array, it works! So it has something to
do with marshaling. The numbers in the parameter array are correct. If
I view them in debug mode they are of type Variant/Integer.
The new array i define is also Variant/Integer so I do not see any
difference?!?

Jan 27 '06 #10

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

Similar topics

4
3844
by: JesusFreak | last post by:
From: us_traveller@yahoo.com (JesusFreak) Newsgroups: microsoft.public.scripting.jscript Subject: toolbar script problem NNTP-Posting-Host: 192.92.126.136 Recently, I downloaded the following beautiful script "http://javascript.internet.com/navigation/toolbar-menu.html". It works like a charm. I made my webpage in frames, where the nav-frame shows the menubar, so whenever I click a link in the menubar, it opens in the frame below. But...
0
285
by: crawlerxp | last post by:
This is the problem: I do not get the output I need when encoding and decoding data using rijndael alghoritm. Look at the code and see what the problem is actually: Please paste this code into your Visual Studio and compile it + run it; so you can see what the actual problem is. Thanks. code:
8
15360
by: Brady | last post by:
Hi, I'm having a problem reading and writing to a file. What I'm trying to do is read a file, modify the portion of the file that I just read, and then write the modified data back to the same location in the file. What is happening, is I can read the file, either in entirety or only part of it. And no matter what I try setting the position to, using fsetpos, it always gets set back to the very beginning of the file. I
8
3372
by: mytfein | last post by:
Hi Everyone, Background: Another department intends to ftp a .txt file from the mainframe, for me to process. The objective is to write a vb script that would be scheduled to run daily to process this .txt file. Goal: I am working on a vba script to:
8
10719
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to receive a byte array as one of its parameters. The project is marked for COM interop, and that all proceeds normally. When I reference the type library in the VB6 project, and write the code to call the function that returns the byte array, it works
4
11231
by: daroman | last post by:
Hi Guys, i've problem with my small C++ programm. I've just small template class which represetns a array, everything works fine up to combination with std::string. I did tried it with M$ VC++ and with GCC (Cygwin and Linux) and my problem is when i try do this int main(int argc, char **argv) { array<std::stringa(10); a = "Huhuhu"; <--- with gcc i got a crash !
5
2562
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I use to run a UM program, I kept on getting error messages. I have used someone else's implementation and it runs fine. I have compared my code with other's and I still can't figure it out what's wrong with mine. So please help me out, after 3...
9
2519
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But *after* calling increase_arrays(), I received segmentation fault. I tried to step it through gdb, and I found out that after calling increase_arrays(), words's original value is modified, and if I tried to access it, I get <address 0x11 out of...
3
6351
by: raylopez99 | last post by:
Below is my problem. I've narrowed it down to one thing: my unfamiliarity on how class instances are instantiated in an array. This is because the "un-array" / "non-array" version of the program works fine (see below). So what is the problem? I get a null reference on the line below at *!&!* "Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.? RL
25
2362
by: biplab | last post by:
Hi all, I am using TC 3.0..there if I declare a integer array with dimension 162*219...an error msg saying that too long array is shown....what should I do to recover from this problem???
0
9592
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
10231
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
10059
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
10005
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
6679
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5313
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
3972
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
3576
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2817
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.