Custom HRESULT from COM component - c#

Creating custom HRESULT has been covered here
Creating your own HRESULT?
Solution to above question is fine for c++ native clients, they can use the returned HRESULT to FormatMessage() and get error string, what about C# and JavaScript client? For discussion's sake let us forget FormatMessage() via PInvoke.
e.g. if a COM function returns E_ACCESSDENIED and C# client will receive it via nicely packaged exception, complete with error string and symbolic name(E_ACCESSDENIED)
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
It is understood system error codes have been mapped to .Net exceptions.
Questions is how to return an message compiler generated custom error codes packaged in HRESULT?
Is MAKE_HRESULT the answer?

Related

Instantiating .NET Print Server

Final Edit
Apparently I've been barking up the wrong tree.
https://msdn.microsoft.com/en-us/library/system.printing.printsystemjobinfo(v=vs.110).aspx
“Classes within the System.Printing namespace are not supported for use within a Windows service or ASP.NET application or service. Attempting to use these classes from within one of these application types may produce unexpected problems, such as diminished service performance and run-time exceptions.”
Edit
I haven't been able to get the .NET print server code to work. We have a collection of web services that perform small steps in a workflow, one of them is to print PDFs. I need to be able to send requests to a print server via .NET without having the print drivers installed the web server hosting the web services. Is there even a way to do this? If so, could someone point me in the right direction?
Original ?
I'm trying to connect to a remote print server in a different domain with the following code.
ps = new PrintServer(#"\\scanprn01",PrintSystemDesiredAccess.AdministratePrinter);
Every time I do so though, it throws the following Access Denied error. I've verified the account I'm using (via System.Security.Principal.WindowsIdentity.GetCurrent().Name) and that account is an administrator on the print server and has explicit full permissions to all the printers. Any advice?
Access Denied Error
System.Printing.PrintServerException was unhandled
HResult=-2147024891
Message=An exception occurred while creating the PrintServer object. Win32 error: Access is denied.
Source=System.Printing
StackTrace:
at System.Printing.PrintServer.Initialize(String path, String[] propertiesFilter, PrinterDefaults printerDefaults)
at System.Printing.PrintServer..ctor(String path, PrintSystemDesiredAccess desiredAccess)
at StubBP2Tester.frmBP2Tester.btnConnectPQ_Click(Object sender, EventArgs e) in C:\vsworkspace\StubBP2Tester\StubBP2Tester\Form1.cs:line 36

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.

DebugDiag not seeing exception

I am supporting an application team and they have an issue with their web page - the below is shown:
Retrieving the COM class factory for component with CLSID {0002E569-0000-0000-C000-000000000046} failed due to the following error: 80070005.
I have looked around here and other sites but cannot find the culprit of the access denied object. The object itself is registered in the registry as "Microsoft Office Spreadsheet 11.0".
I have decided to take a dump of the this whilst it happens so I can see where in the code the problem is occurring and possibly track down some info from there.
I have tried debugdiag and tried loads of exception settings however when I go to the website and raise the exception of System.UnauthorizedAccessException it is not saving any dump.
There is no default exception in the exception list called 80070005 so I am putting it down as a .NET 4.0 app and 80070005 - this is the Hex value of access denied as appears in the error message.
Any ideas what settings I should choose for debugdiag to see the exception?

Is it possible to create both client and server using IIOP.Net

I'm new to IIOP.Net.
I have done a Adder app using IIOP.Net ( Server is written in Java and client is C#). Now I want to create both client and server using IIOP.Net written in C# ( I know there are many easier ways to do that but I just want to use IIOP.Net).
What I did is:
Create Server. OK
Because my client is C#, I don't have the idl file to compile to the cls, so I did a bad trick here. I create a libary that is the implementation of my corba object, then build it to a .dll file, finally copy it to my client project ( add references...).
Create client. OK
Run Server. OK
Run Client. Not OK
I got an error:
An unhandled exception of type 'omg.org.CosNaming.NamingContext_package.NotFound' occurred in mscorlib.dll
at line:
Adder a = (Adder) nm.resolve(nameService);
//where Adder is implementation of corba object (in library).
I have no idea to fix that. I googled it but does not give me any expected result.
Hope to see your answers soon.
Thank you.

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.

Categories