Click here to Skip to main content
Sign Up to vote bad
good
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 4:45am


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.
  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
Your Filters
Interested
Ignored
     
0 Christian Graus 533
1 Ron Beyer 326
2 Tadit Dash 253
3 samadhan_kshirsagar 229
4 OriginalGriff 198
0 Sergey Alexandrovich Kryukov 7,041
1 Prasad_Kulkarni 3,815
2 OriginalGriff 3,557
3 _Amy 3,372
4 CPallini 3,034


Advertise | Privacy | Mobile
Web01 | 2.6.130619.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid