Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I'm wanting to use the MSScriptControl from C++ (Visual Studio 2012). When I call the AddObject in runtime I get a "Specified Cast Is Invalid" error.

I have this is my AssemblyInfo.cpp:
C++
[assembly:ComVisible(true)];


This is my BasicCommands.h:
C++
namespace BasicCommands
{
	ref class BasicCommandsClass
	{
		//speak a phrase
	public:
		void SpeakPhrase(String^ phrase)
		{
			SpeechSynthesis::SpeakPhrase(phrase);
		}
		
	

		void RunScript(String^ script)
		{
			MSScriptControl::ScriptControlClass^ pScriptControl=gcnew MSScriptControl::ScriptControlClass();
			pScriptControl->Language = "vbscript";
			pScriptControl->AllowUI = true;
			pScriptControl->AddObject("MyCommands",this,true);
			pScriptControl->ExecuteStatement(script);
		}
	};
}


The AddObject should be adding the SpeakPhrase command so I can call it from my vbscript.

Any idea why it's not working?

Cheers,
Mike.
Posted

1 solution

Hi.

Try add [ComVisibleAttribute(true)] to the class BasicCommandsClass.

MSScriptControl uses COM technology to access to the object properties and methods, so if the class is not COM visible, it cannot be used by any script.

Regards,
Daniele.
 
Share this answer
 
Comments
Mike Hodgson 8-Oct-12 4:12am    
Thanks Daniele, I tried that out putting it above the class, it compiled as normal but I'm still getting the 'Specified Cast Is Not Valid'. I tested in vb.net and it worked fine, so not sure what's up with C++.

Cheers,
Mike.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900