Error with com reference in WCF with dll done in VFP - c#

I have a dll done in VFP that I need to use to be able to perform some processes in the DBF, I add the normal reference, I can use the class in my WCF project.
public API _api;
var _api = new API();
_api.version();
When I make the first call it works fine, but when I make the other call to the service the following error appears:
Failed to create an instance of the COM component with CLSID {FD80E726-1A66-
4823-9D96-D6F00C8B2665} from IClassFactory due to the following error:
80004005 Unspecified error (Exception of HRESULT: 0x80004005 (E_FAIL)).
I do not know what could happen when the class returns.
public Document PostDocument(Document document)
{
var _api = new API();
_api.importar();
return document;
}

Related

VSTO Unit Testing Office AddIn in C# .NET via RequestComAddInAutomationService

I have worked and read through various StackOverflow questions and other tutorials and documentation over the past weeks (N.B. some of them below) to try and find a way of unit testing a VSTO AddIn.
Unfortunately, it always results in an E_NOINTERFACE exception in my tests.
The code I am using is below - one extract of the ThisAddin partial class overriding the RequestComAddinAutomationService, another describing the test utility interface, the test itself, as well as an additional assembly extract proving that the AddIn assembly and its internals are visible to the test.
My question is: why is this not working? I am pretty sure that this follows the generally accepted practices of VSTO testing.
If the below is not possible anymore, how should one go about testing VSTO? Is .NET remoting/IPC the only solution?
ThisAddin.cs
public partial class ThisAddin
{
#region Testing Utilities
private AddinHelper _comAddinObject;
protected override object RequestComAddInAutomationService()
{
// This is being called when I debug/run my project, but not when I run the tests
return _comAddinObject ?? (_comAddinObject = new AddinHelper());
}
#endregion
}
#region Testing Utilities
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinHelper
{
Presentation GetPresentation();
string GetString();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[ComSourceInterfaces(typeof(IAddinHelper))]
public class AddinHelper : StandardOleMarshalObject, IAddinHelper
{
public Presentation GetPresentation()
{
return Globals.ThisAddin... [...];
}
public string GetString()
{
return "Hello World!";
}
}
#endregion
AssemblyInfo.cs
[assembly: InternalsVisibleTo("MyProject.Tests")]
MyUnitTest.cs (has a reference to MyProject)
[TestClass]
public class BasicTest
{
[TestMethod]
public void TestInstantiates()
{
var application = new Application();
var doc = application.Presentations.Open(#"C:\Presentation.pptx",
MsoTriState.msoFalse, MsoTriState.msoFalse, MsoTriState.msoFalse);
var comAddins = application.COMAddIns;
var comAddin = comAddins.Item("MyProject"); // Returns okay
var comObject = (IAddinHelper) comAddin.Object; // Exception occurs
Assert.AreEqual(true, true); // Never reached
doc.Close();
}
}
Furthermore, the project's settings are set to "Register for COM interop" and Visual Studio runs elevated without errors - running it as non-admin results in the DLLs not being registered; therefore, I also know that the COM objects are registered.
Resulting Exception
An exception of type 'System.InvalidCastException' occurred in MyProject.Tests.dll but was not handled in user code
Additional information: Unable to cast COM object of type 'System.__ComObject' to interface type 'MyProject.IAddinHelper'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{59977378-CC79-3B27-9093-82CD7A05CF74}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
StackOverflow
How to call VSTO class from other c# project
Unit Testing VSTO projects
VSTO Add-ins, COMAddIns and RequestComAddInAutomationService (Register for COM Interop doesn't change anything)
Why cannot I cast my COM object to the interface it implements in C#? (the issue is likely not STAThread related)
https://sqa.stackexchange.com/questions/2545/how-do-i-unit-test-word-addin-written-in-c
Microsoft
https://blogs.msdn.microsoft.com/varsha/2010/08/17/writing-automated-test-cases-for-vsto-application/ <-- Even with this supposedly working sample project, I am getting the exact same error.
https://msdn.microsoft.com/en-us/library/bb608621.aspx
General workflow background: https://msdn.microsoft.com/en-us/library/bb386298.aspx
I honestly don't know how one is supposed to test VSTO Add-Ins without this working.
Solution
The Project with methods to be Tested has to use the PIA Microsoft.Office.Interop.PowerPoint via the .Net reference tab.
In the Unit Test Project you have to use the Microsoft Powerpoint 1X.0 Object Library via the COM reference tab - its an ActiveX.
The confusing thing is in Solution Explorer they are both called: Microsoft.Office.Interop.Powerpoint
When you specify the correct References you'll see this error on the line where the exception occurs:
Missing compiler required member 'Microsoft.CSharp.RuntimeBinder.CSharpArgumentInfo.Create'
To solve that simply add a .Net reference to the Microsoft.CSharp.dll in the Unit Test project.
Next we will run into the error you're seeing.
Firstly add a unique GUID to the Interface & class (this will overcome the error):
[ComVisible(true)]
[Guid("B523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IAddinHelper
{
string GetPresentation();
string GetString();
}
[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
[Guid("A523844E-1A41-4118-A0F0-FDFA7BCD77C9")]
[ComSourceInterfaces(typeof(IAddinHelper))]
public class AddinHelper : StandardOleMarshalObject, IAddinHelper
Secondly temporarily make the private AddinHelper _comAddinObject; public in Scope (you can do your [assembly: InternalsVisibleTo("MyProject.Tests")] later when its working).
Thirdly, check that Powerpoint has not disabled the COM add-in. This sometimes happens silently, without the Office App complaining.
Lastly, make sure Register for COM is ticked.
Viola:
Ref: I pulled my hair out working this out years ago helping this fellow SO'r: Moq & Interop Types: works in VS2012, fails in VS2010?

Spotfire COM Automation

I am trying to automate save to library using COM object of spotfire. I looked at the examples of Spotfire SDK for COM automation, it calls OpenDocument method using InvokeMethod. When I try to call Save method using the same technique it throws me Missing Member Exception.
public bool OpenDocument(string filePath)
{
var test = this.InvokeMethod("OpenDocument", filePath);
return (bool)test;
}
public bool SaveDocument()
{
return (bool)this.InvokeMethod("Save", null);
}
In the above code snippet, OpenDocument is working perfectly fine but Its throwing error for Save. The com object is of System.__comobject type so I am not even able to inspect and get the metadata (methods / properties) to see the actual method name, which I can use as a parameter to InvokeMethod

Interop type could not be embedded

I have a dll from an external vendor; it contains 2 classes:
namespace RDTiffDataAccess
{
public class RDTiffDataClass : IRDTiffData, RDTiffData
{
public RDTiffDataClass();
}
}
and
namespace RDTiffDataAccess
{
public interface IRDTiffData
{
// some code
}
}
I try to call the constructor:
RDTiffDataAccess.IRDTiffData Rdt1 = new RDTiffDataAccess.RDTiffDataClass();
It gives me an error:
Interop type 'RDTiffDataAccess.RDTiffDataClass' cannot be embedded. Use the applicable interface instead.
I have searched and tried a possible solution by changing Embed Interop Types = false.
Now it compiles ok, but at run-time, an exception is thrown:
Retrieving the COM class factory for component with CLSID failed
due to the following error: 80040154 Class not registered
(Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).
How do I go about resolving this?
I know this is an old question but could help someone else.
Try to change the project properties to x86 platform target on Build section.
Maybe the dlls were built targeting 86 bits.

Retrieving the COM class factory for component with CLSID {XXXX} failed due to the following error: 80040154 VB.net

I code in VB.net under Visual Studio 2008. I have this error and I don't understand why.
Retrieving the COM class factory for component with CLSID {XXXX} failed due to the following error: 80040154
I use a DLL and I want to compile it in 64bit. I have the following code:
Imports MetroProDAT_Library
Public Class Data
Public attr As ZygoDataAttributes
Public oData As ZygoDataFile
Public Sub LoadData(ByVal FileName As String)
Dim idx As Double
Dim jdx As Double
oData = New ZygoDataFile '///this sentence bug
attr = oData.Attribute
End Sub
End Class
it might be the dll file. do you have a specific dll files that needed to run the program ?
register it to the machine and test it again

SecurityPermission error when instantiating media player

I'm using a Class Library exporting a function via COM.
When calling this function from a JScript, it fails with the following error:
Error: System.Security.Permissions.SecurityPermission
Code: 8013150A
Here's the C# code of my library:
[ComVisible(true), Guid("B40EFE10-FC1B-43A0-ADA1-C923935F3537")]
public class ExodusGadget
{
private WindowsMediaPlayer wmp;
public void Play(string file)
{
wmp = new WindowsMediaPlayer();
//wmp.URL = file;
//wmp.controls.play();
}
}
And I'm using this JScript code (running in Windows Scripting Host, i.e. without IE-related restrictions) to call it:
var obj = new ActiveXObject('ExodusGadget.ExodusGadget');
obj.play('somefile.mp3');
Calling other methods in my class which do not instantiate windows media player work fine.
Thanks to microsofts unhelpful error messages I don't really have an idea what's going wrong.
Problem solved. It was due to the project and thus the DLL being on a network drive which windows considers as untrusted. Moving it to a local HDD fixed the problem.

Categories