Bill
That's a "run-time callable wrapper".
http://msdn.microsoft.com/en-us/library/8bwh56xe.aspx
it is a managed object that wraps a COM interface pointer.
Define "fails". To me, "fails" means that the expression in the "if"
statement evaluates to "false", which would mean that "theSheets" is a
reference to an object that _is_ of the
Microsoft.Office.Interop.Excel.Worksheetstype.
But your question seems to be more likely to indicate that the condition
itself is true, and that the object is not of that type.
I do not know whether the Officeinteroptypes provide a way to get a
managedWorksheetsinstance from a COM object. But if they do, that
would what you want to do. You'll have to use the
Marshal.GetComInterfaceForObject() to resolve the RCW to an actual COM
interface pointer, unless the Officeinteropsupport can unwrap an RCW
itself.
If Officeinteropdoes not allow conversion from COM to managed type,
then you will have to use the COM interface directly. See the abovelink
for other links to more details about how to do that. Basically, you will
need to declare a COMinteropinterface type and use that type with the
RCW to access members in the COM interface. Keeping in mind, of course,
that _all_ of the COM types related to that will also require similar
type declarations in order to use their members.
Pete
On Thursday, September 02, 2010 6:29 PM Bill Swartz wrote:
ary/8bwh56xe.aspx
heets",
l
,
First of all, I mean by fails that the the cast is not non-null, that
is, it is null. Here is a test interface and class:
using System.Runtime.InteropServices;
using System.Reflection;
using System;
namespace MirrorCalTestLib
{
[ComVisibleAttribute(true)]
[GuidAttribute("AC062A45-9918-493d-8E49-677EA3E4B892")]
[InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)]
[AutomationProxy(true)]
public interface ITestHelper
{
void ReceiveSheets(
[MarshalAs(UnmanagedType.Interface)]
ref object theSheets
);
}
[ComVisibleAttribute(true)]
[GuidAttribute("3A48FE42-2254-4db5-8ECF-B47133C7D4F1")]
[ClassInterfaceAttribute(ClassInterfaceType.None)]
public class TestHelper
{
ExLog _ExLog;
public TestHelper()
{
Assembly executingAssembly =3D
Assembly.GetExecutingAssembly();
string thisToString =3D this.ToString();
_ExLog =3D new ExLog(10, executingAssembly.Location,
thisToString);
_ExLog.TraceEvent("{0} ctor", thisToString);
_ExLog.TraceAssemblies(executingAssembly);
}
public void ReceiveSheets(
ref object theSheets
)
{
_ExLog.TraceEvent("{0}.ReceiveSheets {1}",
this.ToString(), theSheets.GetType());
if ((theSheets as
Microsoft.Office.Interop.Excel.Worksheets) !=3D null)
{
_ExLog.TraceEvent("{0}.ReceiveSheetsWorksheets",
this.ToString());
}
IntPtr ip =3D Marshal.GetIDispatchForObject(theSheets);
_ExLog.TraceEvent("{0}.ReceiveSheets {1}",
this.ToString(), ip.ToInt32());
}
}
}
The call to GetIDispatchForObject returns something. "ip.ToInt32()"
evaluates to 42074960. I will add a call to GetComInterfaceForObject.
Bill
On Friday, September 03, 2010 11:50 AM Bill Swartz wrote:
I
brary/8bwh56xe.aspx
ksheets",
n
nk
'll
se,
on,
sheets",
eets);
Further testing shows that I can successfullypasstheseexcelobject
model items and, on thec#side, cast back to the item:
Range
Worksheet (individual)
Workbook
For some reason, it does not work for theWorksheetscollection.
Bill
On Sunday, September 05, 2010 6:04 PM Bill Swartz wrote:
. I
library/8bwh56xe.aspx
orksheets",
f"
a
ion
t
M
W
link
ou'll
he
urse,
tion,
rksheets",
Sheets);
Even further testing shows that theWorksheetscollectiongets passed
as the Sheetscollection, and can be cast to a Sheets object.
Bill
Submitted via EggHeadCafe - Software Developer Portal of Choice
ExcelTips: Net Present Value (NPV) and Internal Rate of Return (IRR)
http://www.eggheadcafe.com/tutorials/aspnet/e426181a-90f1-4bdb-a58a-e...