instantiate COM object fail Unable to cast object - c#

I have a COM object created with C# .net framework 2.0, the COM client a windows application net framework 4.6.1.
COM client fail to instantiate COM object, the exception is - Unable to cast object.
By setting useLegacyV2RuntimeActivationPolicy in the COM client app.config to false, instantiate COM object successful.
Can you please explain this?
is there a way that we instantiate COM object with useLegacyV2RuntimeActivationPolicy set to true?
Code for COM object we took from :
https://www.codeproject.com/Articles/35532/C-COM-Object-for-Use-In-JavaScript-HTML-Including
We replace the client with windows application.
fail casting
registry com object

You can read about setting the V2 runtime activation policy at run-time here: http://reedcopsey.com/2011/09/15/setting-uselegacyv2runtimeactivationpolicy-at-runtime/

Related

MIME MAPI IConverterSession COMException on Activator.CreateInstance

I have been trying to use IConverterSession to convert from EML to MSG (MIME to MAPI), but I keep stumbling on COM errors.
I use a C# MAPIMethods class to wrap IConverterSession (like the one found here : Save Mail in MIME format (*.eml) in Outlook Add-In).
First, I had the problems of unknown clsid, solved with this post (https://blogs.msdn.microsoft.com/stephen_griffin/2014/04/21/outlook-2013-click-to-run-and-com-interfaces/).
Now that the proper registry keys have been edited, I face a new problem: first, I get an error message The operating system is not presently configured to run this application, and I get a COMException: Retrieving the COM class factory for component with CLSID {4E3A7680-B77A-11D0-9DA5-00C04FD65685} failed due to the following error: 8007013d The system cannot find message text for message number 0x in the message file for . (Exception from HRESULT: 0x8007013D).
My code is :
Type converter = Type.GetTypeFromCLSID(MAPIMethods.CLSID_IConverterSession, true);
object obj = Activator.CreateInstance(converter);
MAPIMethods.IConverterSession session = (MAPIMethods.IConverterSession)obj;
The error is raised on "object obj = Activator.CreateInstance(converter);"
A COMException normally means that "type is a COM object but the class identifier used to obtain the type is invalid, or the identified class is not registered.". So either Type converter = Type.GetTypeFromCLSID(MAPIMethods.CLSID_IConverterSession, true); does not return the proper type, or there is still a registry key missing somewhere.
I am using Office 15 (2013) C2R 32 bits on Win 64bits. The application is setup on an x86 build configuration.
Is there something I am missing somewhere? Can anyone help?
"The operating system is not presently configured to run this application" - it sure sounds like your app is compiled as x64 on a machine with a 32 bit version of Outlook.
Have you tried to use Redemption? It wraps IConverterSession for .Net languages. Something like the following should do the job.
using Redemption;
...
Redemmption.RDOSession session = new Redemmption.RDOSession();
Redemmption.RDOMail msg = session.CreateMessageFromMsgFile(#"c:\temp\test.msg");
msg.Import(#"c:\temp\test.eml", Redemption.rdoSaveAsType.olRFC822);
msg.Save();
olRFC822 format will use IConverterSession if it is available or the internal Redemption converter if IConverterSesison is not available (e.g. under Exchange version of MAPI or the latest versions of Outlook 2016 C2R where IConverterSession cannot be used).
Use olRFC822_Redemption or olRFC822_Outlook if you always want to force Redemption or Outlook (IConverterSession) converter.

Error while accessing DLL interface from Web Application

I have made one windows Form application in C#. I have added one DLL reference in that application and called method of that DLL. Its working fine. But when I have made web application and added reference of same DLL and tried to run then I am getting error as
Unable to cast COM object of type 'XXXXX.XXXX' to interface type 'XXXXX.XXXX'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{2DEEB1D7-0B44-4142-9E7B-07477781696C}' failed due to the following error: Bad variable type. (Exception from HRESULT: 0x80020008 (DISP_E_BADVARTYPE)).
The method in DLL, I am calling from web application as
m_SSCP.StartUp("FilePath", "value1", "value2", 6553703, 0, out return_code);
So if I am calling same function from Web application and Windows form application, Is there any difference between these two operation?
I tried to search on net but I am not getting any proper help.

COM Server with only one running instance (Service, Singleton, ...)

for a third party application I need to program a Com Server. This is no problem but I need the COM Server to run only in one instance. This instance should run as a service so that it is not created every time new.
The COM Application is created by vbs every time new.
Dim myCom
Set myCom = Nothing
Set myCom = CreateObject("MyCom.Application")
myCom.DoAction
The Com Server itself creates a database connection and I want to connect once and not for every creation.
I hope you understand what I mean. If not contact me pleade via comment. Thanks.
Chris
Take a look at ExeCOMServer:
ExeCOMServer encapsulates the skeleton of an out-of-process COM server in
C#. The class implements the singleton design pattern and it's thread-safe.
To start the server, call CSExeCOMServer.Instance.Run(). If the server is
running, the function returns directly.

Why does my call to a VB6 ActiveX DLL from C# app fail with "No such interface supported"?

I have a VB6 Active DLL that I'm trying to call from C#.
I've converted it using tblimp and imported it into VS 2010.
I then instantiate it using:
AppWebActiveX.Users appWebActiveX = new appWebActiveX.Users();
There's a Users interface and a UsersClass class when I look in the object browser.
Then I call:
bool isExistingUser = appWebActiveX.IsExistingUser("cat");
bool IsExisting(string) is a valid method, according to the object browser.
However, this returns:
Unable to cast COM object of type 'AppWebActiveX.UsersClass' to interface type 'AppWebActiveX._Users'. ... No such interface supported ...
The latest stack call is to System.StubHelpers.StubHelpers.GetCOMIPFromRCW.
I'm running the app on Windows Server 2003 Standard Edition (x86 - 32bit) and it's a 32-bit DLL. I'm making the call from a C# console app.
Any advice?
Turns out that I'd omitted the [STAThread] attribute from the main method.

C# COM server access from JScript

I have a COM server, implemented in C#. It exposes a class decorated like this:
[ComVisible(true)]
[ProgId("MyServer.MyClass")]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
I registered the DLL with "regasm /codebase MyServer". This server must be used from a WSC (a COM server implemented in JScript). Therefore I used the AutoDispatch interface.
When the WSC calls
o = new ActiveXObject("MyServer.MyClass");
I get the error "Automation serve can't create object".
I checked the registration of the COM server with this C++ program:
hr = ::CoInitialize(NULL);
hr = ::CLSIDFromProgID(L"MyServer.MyClass", &clsid);
hr = ::CoCreateInstance(clsid, NULL, CLSCTX_ALL, IID_IDispatch, (void**)&pObj);
and was able to create the object. All return values have been checked to be S_OK.
What is needed to make the COM server accessible for JScript?
If the problem is x64, see the answer of Hot to make COM ActiveX object work in IE 64 bit?
Are the types used in your class ole automation compatible? I don't know if jscript checks for oleautomation compatibility up front.

Categories