Author Topic: Unable to cast object of type X to type X  (Read 4005 times)

0 Members and 1 Guest are viewing this topic.

dukesys

  • Guest
Unable to cast object of type X to type X
« 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.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Unable to cast object of type X to type X
« Reply #1 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.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

dukesys

  • Guest
Re: Unable to cast object of type X to type X
« Reply #2 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.

MickD

  • King Gator
  • Posts: 3636
  • (x-in)->[process]->(y-out) ... simples!
Re: Unable to cast object of type X to type X
« Reply #3 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.
"Programming is really just the mundane aspect of expressing a solution to a problem."
- John Carmack

"Short cuts make long delays,' argued Pippin.”
- J.R.R. Tolkien

Glenn R

  • Guest
Re: Unable to cast object of type X to type X
« Reply #4 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.

dukesys

  • Guest
Re: Unable to cast object of type X to type X
« Reply #5 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.

Glenn R

  • Guest
Re: Unable to cast object of type X to type X
« Reply #6 on: October 12, 2006, 09:15:57 PM »
Told you  :-P  :-D

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: Unable to cast object of type X to type X
« Reply #7 on: October 12, 2006, 09:46:57 PM »
[jedihandwave]

/// ...

[/jedihandwave]

:-)


kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.