Jump to content
  • Advertisement
Sign in to follow this  
Plerion

.NET InvalidCastException

This topic is 2997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello! My application throws an InvalidCastException in the following line:
IScriptProvider^ prov = (IScriptProvider^)asme->CreateInstance("Wrapper.ScriptProvider", true);
With the text: "The object of type "Wrapper.ScriptProvider" cannot be converted to the type "IScriptProvider"." But basically this cannot be as the classes are like that:
	public interface class IScriptProvider
	{
	public:
		void InitLib(ScriptMgr^ pMgr);
	};
In the project that gets loaded in the above code this class gets implemented:
namespace Wrapper
{
    public class ScriptProvider : IScriptProvider
    {
        public ScriptProvider()
        {
        }

        public void InitLib(ScriptMgr init)
        {
            init.RegisterChatHook(new ChatHooker.ChatHook());
        }
    }
}
I made a test in another application and there isnt an exception. What could be the reason for that? Greetings and thanks Plerion

Share this post


Link to post
Share on other sites
Advertisement
Hold the returned result from asme->CreateInstance() a local object variable before casting it. Then put a break point on the line where is casted. Then you can see what is really being returned.

Share this post


Link to post
Share on other sites
Its a bit a bad setup to debug via breakpoints. I made the following:

System::Object^ obj = asme->CreateInstance("Wrapper.ScriptProvider", true);
Console::WriteLine("Type: {0}", obj->GetType()->ToString());
IScriptProvider^ prov = (IScriptProvider^)obj;


It displays "Wrapper.ScriptProvider".

Share this post


Link to post
Share on other sites
i found the reason for the exception but not the solution...

Imagine the situation:
.\Server.exe
.\scripts\Wrapper.dll
.\scripts\Managed\Script.dll

Server.exe calls a exported function of Wrapper.dll. This function creates instance of my ScriptMgr from the Assembly scripts\Wrapper.dll. Now it loads the assembly scripts\Managed\Script.dll. This depends on scripts\Wrapper.dll (it is referenced there). But the problem is that it is expecting just "Wrapper.dll". So it searches for Wrapper.dll in .\ . There of course is no Wrapper.dll and CreateInstance returns 0.

Ok, if i copy the Wrapper.dll to .\ CreateInstance loads the ScriptProvider which inherits from IScriptProvider. But now there is a problem as i could read on MSDN. This instance is of the type .\Wrapper.dll:IScriptProvider. If i now try to cast that in my .\scripts\Wrapper.dll to IScriptProvider it wants to cast it to .\scripts\Wrapper.dll:IScriptProvider. This is differenct for .NET and therefore invalid cast.

I tried to set the current directory of my application to .\scripts when i load the assembly .\scripts\Managed\Script.dll but it still wants to have the Wrapper.dll in .
Does anybody know a possbile solution for that?

Greetings
Plerion

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!