Click here to Skip to main content
12,065,756 members (32,599 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C++ Managed
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:
[assembly:ComVisible(true)];

This is my BasicCommands.h:
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 7-Oct-12 5:45am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160207.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100