TheSwamp

Code Red => .NET => Topic started by: dukesys on October 10, 2006, 06:09:12 PM

Title: Unable to cast object of type X to type X
Post by: dukesys on October 10, 2006, 06:09:12 PM
Hello all,

I am working on a .NET project that needs to call out to a function in a VB6 DLL.  When it makes the call AutoCAD throws a wobbly with the following error.

Unable to cast object of type 'System.Object[]' to 'FileTools.clsMaskSpec[]'

I know the routine in the DLL works fine as I can call it ok from VB6, so I think it must me some sort of .NET issue.

The function takes the following form.

getMatchingstrings(byref specArray as system.Array, byref retval as System.Array)

The function takes two arrays as input, the second array is modified to return the result.  The function in the DLL has no defined return. eg.

Function getMatchingStrings(spec() as clsMaskSpec, retVal() as string)


Any help would be appreciated.


Regards

Martin.
Title: Re: Unable to cast object of type X to type X
Post by: MickD on October 10, 2006, 06:15:18 PM
Hi Martin, welcome aboard!

The first place I would look is at 'DllImport', I'm not up with vb (especially spec(), what  is that?) but you will need to marshal your types across the process boundaries from .net to unmanaged and back. If you work out how to do this you should be ok.

hth.
Title: Re: Unable to cast object of type X to type X
Post by: dukesys on October 10, 2006, 09:14:05 PM
Hi Mick,

thank you for your response, not sure how to proceed from here but will do some further research.


Cheers,

Martin.

Hi Martin, welcome aboard!

The first place I would look is at 'DllImport', I'm not up with vb (especially spec(), what  is that?) but you will need to marshal your types across the process boundaries from .net to unmanaged and back. If you work out how to do this you should be ok.

hth.
Title: Re: Unable to cast object of type X to type X
Post by: MickD on October 10, 2006, 09:52:52 PM
Sorry I couldn't be of anymore help.

Just looking at your code though -
Quote
The function takes the following form.

getMatchingstrings(byref specArray as system.Array, byref retval as System.Array)

both params are arrays and as such are simply a pointer to the start of the array itself, perhaps you can try something like

Code: [Select]
[DllImport("yourDllname.dll", CallingConvention=CallingConvention.Cdecl)] 
    private static extern void getMatchingstrings(System.IntPtr, System.IntPtr);
// then use it like:
getMatchingstrings(spec().unmanagedObject,retval.unmanagedObject);

..or something like that anyways.
hth,
Mick.
Title: Re: Unable to cast object of type X to type X
Post by: Glenn R on October 10, 2006, 10:09:59 PM
Code: [Select]
getMatchingstrings(byref specArray as system.Array, byref retval as System.Array)
Maybe it's just the VB stuff which I'm not used too, but that just looks wrong.
If I was to literally do that in C# it would look like this:

Code: [Select]
getMatchStrings(object[] specArray, object[] retVal)
However, what I think you want is this:

Code: [Select]
getMatchStrings(FileTools.clsMaskSpec[] specArray, string[] retval)
...or the VB.Not :) equivalent of somehing like that.
Title: Re: Unable to cast object of type X to type X
Post by: dukesys on October 12, 2006, 09:03:13 PM
Hello,

Thank you for all your help on this topic,  I have however decided to bight the bullet and convert the VB6 DLL into .NET.  Not really what I had hoped to do but probably the best thing to do.


cheers

Martin.
Title: Re: Unable to cast object of type X to type X
Post by: Glenn R on October 12, 2006, 09:15:57 PM
Told you  :-P  :-D
Title: Re: Unable to cast object of type X to type X
Post by: Kerry on October 12, 2006, 09:46:57 PM
[jedihandwave]

/// ...

[/jedihandwave]

:-)