Hi guys!
Please, help.
Win 10 1906 / 3ds max 2019 / VS2019
I am writing this:
private static readonly IInterface_ID IVIEWPORT_SETTINGS_INTERFACE_ID = GlobalInterface.Instance.Interface_ID.Create(0x761840a9, 0x437f4090);
//...//
var vpt = GlobalInterface.Instance.COREInterface19.ActiveViewExp; var vset = (IIViewportViewSetting) vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID);
I get an error:
System.InvalidCastException: Unable to cast object of type 'Autodesk.Max.Wrappers.BaseInterface' to type 'Autodesk.Max.MaxSDK.Graphics.IIViewportViewSetting'
Maybe I'm doing something wrong?
And how then to access this interface?
Thanks for any help!
Solved! Go to Solution.
Solved by Swordslayer. Go to Solution.
Solved by Swordslayer. Go to Solution.
try to access it from unmanaged memory using GCHandle, GCHandle.ToIntPtr, and GCHandle.FromIntPtr...
it's just an idea. I didn't try it but don't see a problem.
I could not access using GCHandle, because I do not fully understand how this can be done. Maybe there is something more obvious?
this is an example of 'downcasting' ... in c++ you can do "dynamic_cast" but in c# it's not such easy. because IIViewportViewSetting is an abstract class, I don't see any other way than to do it unsafe using a pointer... I might be wrong and maybe anyone else with better c# knowledge can give you the better solution.
the situation we have:
class BaseInterface { // the things are here }; class IIViewportViewSetting : BaseInterface { // the things are here };
so my guess of what can work is:
BaseInterface bi = // vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID); GCHandle gch = GCHandle.Alloc(bi); IntPtr ptr = GCHandle.ToIntPtr(gch); gch.Free(); IIViewportViewSetting vs = (IIViewportViewSetting)GCHandle.FromIntPtr(ptr).Target;
again.. it's never tested and just my guess...
Yes, that's exactly what I tried.
and in this line of code:
IIViewportViewSetting vs = (IIViewportViewSetting) GCHandle.FromIntPtr(ptr).Target;
same error:
Unable to cast object of type 'Autodesk.Max.Wrappers.BaseInterface' to type Autodesk.Max.MaxSDK.Graphics.IIViewportViewSetting'.
I'm at a dead end.
Спасибо Денис, что помогаете мне.
There're marshallers specifically for that purpose:
using Wrappers = Autodesk.Max.Wrappers; using Graphics = Autodesk.Max.MaxSDK.Graphics; ... var vset = (Graphics.IIViewportViewSetting)Graphics.Wrappers.CustomMarshalerIViewportViewSetting.GetInstance(string.Empty).MarshalNativeToManaged((vpt.GetInterface(IVIEWPORT_SETTINGS_INTERFACE_ID) as Wrappers.BaseInterface).INativeObject__NativePointer);
Copy the parts that I posted, don't rewrite it how you think it should look. The marshaller is not part of the general wrappers, it's part of the Autodesk.Max.MaxSDK namespace. And it looks like you also haven't added a reference to the Autodesk.Max.Wrappers.dll (it's in the %MAXROOT%/bin/assemblies/ folder).
Oh, thanks a lot.
I did not know about the existence of Autodesk.Max.Wrappers.dll separately.
Thank you again, everything works like a charm!
Can't find what you're looking for? Ask the community or share your knowledge.