Click here to Skip to main content
Rate this: bad
good
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 at 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
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 8 Oct 2012
Copyright © CodeProject, 1999-2014
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