all groups > dotnet interop > march 2005 >
You're in the

dotnet interop

group:

Empty arrays (2)



Re: Empty arrays (2) dgm
3/30/2005 7:02:57 AM
dotnet interop: Evert,

I would very much like to know how you were successful in sending the
initialized arrays. Could you show me a sample .NET method that is
successfully accepting an array marshalled from VB to .NET?

Dave
Empty arrays (2) Evert Timmer
3/30/2005 4:24:51 PM
Hi,

I have a question regarding COM interop.

As a reseller, we are developing extension modules for a legacy
application of which we have no source code. This application is written
in VB6 but will be ported to the .NET platform in the near future.

In order for us to make the transition fluent, we are in the process of
porting our modules to the .NET platform too, so we will be able to
service our customers once the new platform releases.

For the time beeing, we want to be able to have the VB6 application use
our .NET modules as it did with our VB6 Modules. This is where interop
comes in the picture.

The application creates a COM object using CreateObject, and then calls
a method in that object ( our component ).

The last two parameters of that method call are string arrays, but
sometimes there are no elements in those arrays. ( Uninitialized array )

When the application calls our component with uninitialized arrays, the
method call will error out. This does not occur in our VB6 modules.

The cause can not be the content of the method because there is no code
except a messagebox.show call inside, for testing purposes, so it must
be in the marshalling part.

Can someone tell me why this problem is occurring, and how to solve this?

Thanks in advance,
Re: Empty arrays (2) dgm
3/31/2005 5:43:42 AM
Well thanks very much for that sample code. The main difference is that
I didn't use an interface. Perhaps that is the missing piece.

Thanks again,

Dave
Re: Empty arrays (2) Evert Timmer
3/31/2005 9:10:03 AM
[quoted text, click to view]

Hmm,

Did not have a problem with that. I am using VS.NET 2003 EA with all
patches & updates applied.

Created a .NET project with the "Register for COM Interop" option set in
the projects configuration properties. Then applied a keyset to the
assemply in order to get it into the gac.

Coded an interface and implemented it in a class.

That's it.

Sample code:


============================================================================
using System;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace YourNamespace
{
[Guid("40FF80BA-AD50-4c22-A6F7-57DFCB4DF90D")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface _YourInterface
{
/// <summary>
/// StartWork public method.
/// </summary>
/// <param name="StringArray"></param>
/// <param name="SecondStringArray"></param>
/// <param name="FirstString"></param>
/// <param name="SecondString"></param>
/// <param name="FirstUninitializedArray"></param>
/// <param name="SecondUninitializedArray"></param>
/// <returns>Integer</returns>
[DispId(1)]
Int32 StartWork(
string[] StringArray,
string[] SecondStringArray,
string FirstString,
string SecondString,
string[] FirstUninitializedArray,
string[] SecondUninitializedArray);
}

[Guid("57473E2C-FC0A-4ef5-A6D5-96FC834BA142")]
[ClassInterface(ClassInterfaceType.None)]
[ProgId("YourCompany.YourComponent")]
public class YourComponent : _YourInterface
{
/// <summary>
/// YourComponent Constructor
/// </summary>
public YourComponent() {}

/// <summary>
/// StartWork public method.
/// </summary>
/// <param name="StringArray"></param>
/// <param name="SecondStringArray"></param>
/// <param name="FirstString"></param>
/// <param name="SecondString"></param>
/// <param name="FirstUninitializedArray"></param>
/// <param name="SecondUninitializedArray"></param>
/// <returns>Integer</returns>
/// <returns>Integer</returns>
public Int32 StartTransfer(
string[] StringArray,
string[] SecondStringArray,
string FirstString,
string SecondString,
string[] FirstUninitializedArray,
string[] SecondUninitializedArray);
{
for(int i=0;i<StringArray.Length;i++)
{
MessageBox.Show(".NET Module for job:" + StringArray[i]);
}
return 1;
}
}
Re: Empty arrays (2) Dave
4/4/2005 2:21:40 PM
Can you attach a debugger to the process to see if the method is called?

Could you possibly be getting a NullReferenceException?

--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
[quoted text, click to view]

Re: Empty arrays (2) Dave
4/5/2005 6:32:44 AM
Yea, I think adding a quick null-checking function is your best, and probably easiest, solution. I don't know why the Marshaller
isn't just sending in null.

Or you can can try declaring the parameter in .NET using the ref keyword (C#) so that you receive a pointer. You can then check the
pointer's referencing as being null by checking equality with IntPtr.Zero.

I agree, however, that it seems strange the Marshaller isn't handling this for you and passing in null. Maybe some of the Interop
gurus can take a crack at this?

I have a question, did you try passing the VB NULL keyword to the function? Just curious as to what would happen.

Good luck :)


--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
[quoted text, click to view]

Re: Empty arrays (2) Evert Timmer
4/5/2005 8:49:12 AM
[quoted text, click to view]

The call never reaches the functions inner code but fails somewhere in
the marshalling part.

We settle for a small VB6 dll now, between the application and our .NET
component, that forwards the call to .NET, but with the string arrays
checked for, and adjusted in case of, no elements.

The problem only occurs with empty arrays, not with dimensioned arrays.

Really weird, though. I can not understand why such an issue is not
caught and corrected by the marshaller.

Thanks.

AddThis Social Bookmark Button