Declaring Inteface

N

NG

Hi All,

I want to declare an interface in C#. I have the equivalent code for VB.NET
but when I try to write the corresponding code in C#, it does not work. Can
anyone please help me by providing the correct code.

I am pasting below both VB.NET code (working) and C# code (not working)

<VB.NET>
Option Explicit On

Option Strict On

Imports System.Runtime.InteropServices

<ComVisible(True), ComImport(),
Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _

InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _

Public Interface IPersistStreamInit

' IPersist interface

Sub GetClassID(ByRef pClassID As Guid)

<PreserveSig()> Function IsDirty() As Integer

<PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As Integer

<PreserveSig()> Function Save(ByVal pstm As UCOMIStream, ByVal
ByValfClearDirty As Boolean) As Integer

<PreserveSig()> Function GetSizeMax(<InAttribute(), Out(),
MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As Integer

<PreserveSig()> Function InitNew() As Integer

End Interface

</VB.NET>

<CSHARP>
using System.Runtime.InteropServices;

[Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"),

InterfaceType(ComInterfaceType.InterfaceIsDual)]

interface IPersistStreamInit // cannot list any base interfaces here

{

void GetClassID(System.Guid pClassID);

[PreserveSig] int IsDirty();

[PreserveSig] int Load([In] UCOMIStream pstm);

[PreserveSig] int Save([In] UCOMIStream pstm, [In] bool ClearDirty);

[PreserveSig] int GetSizeMax([In, Out, MarshalAs(UnmanagedType.U8)] ref long
pcbSize);

[PreserveSig] int InitNew();

}

</CSHARP>

Any help is much appreciated.

Thanks
NG
 
G

Guest

I just copied and pasted the exact code you have in this message and compiled
it (the C# version) and I do not get any errors or warning. By not working
do you mean it is not behaving as expected or an error is being thrown?
 
N

NG

Yes, it compiles perfectly but does not work when I use like the code below.
It gives the following error:
<ERROR>
An unhandled exception of type 'System.NullReferenceException' occurred in
mscorlib.dll
Additional information: Object reference not set to an instance of an
object.
</ERROR>

using mshtml;

HTMLDocument oDoc=new HTMLDocument();


IPersistStreamInit ips;

ips=(IPersistStreamInit) oDoc;

ips.InitNew();



IHTMLDocument2 iDoc2=oDoc.createDocumentFromUrl(http://yahoo.com , "");





Mark R. Dawson said:
I just copied and pasted the exact code you have in this message and compiled
it (the C# version) and I do not get any errors or warning. By not working
do you mean it is not behaving as expected or an error is being thrown?

NG said:
Hi All,

I want to declare an interface in C#. I have the equivalent code for VB.NET
but when I try to write the corresponding code in C#, it does not work. Can
anyone please help me by providing the correct code.

I am pasting below both VB.NET code (working) and C# code (not working)

<VB.NET>
Option Explicit On

Option Strict On

Imports System.Runtime.InteropServices

<ComVisible(True), ComImport(),
Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"), _

InterfaceTypeAttribute(ComInterfaceType.InterfaceIsIUnknown)> _

Public Interface IPersistStreamInit

' IPersist interface

Sub GetClassID(ByRef pClassID As Guid)

<PreserveSig()> Function IsDirty() As Integer

<PreserveSig()> Function Load(ByVal pstm As UCOMIStream) As Integer

<PreserveSig()> Function Save(ByVal pstm As UCOMIStream, ByVal
ByValfClearDirty As Boolean) As Integer

<PreserveSig()> Function GetSizeMax(<InAttribute(), Out(),
MarshalAs(UnmanagedType.U8)> ByRef pcbSize As Long) As Integer

<PreserveSig()> Function InitNew() As Integer

End Interface

</VB.NET>

<CSHARP>
using System.Runtime.InteropServices;

[Guid("7FD52380-4E07-101B-AE2D-08002B2EC713"),

InterfaceType(ComInterfaceType.InterfaceIsDual)]

interface IPersistStreamInit // cannot list any base interfaces here

{

void GetClassID(System.Guid pClassID);

[PreserveSig] int IsDirty();

[PreserveSig] int Load([In] UCOMIStream pstm);

[PreserveSig] int Save([In] UCOMIStream pstm, [In] bool ClearDirty);

[PreserveSig] int GetSizeMax([In, Out, MarshalAs(UnmanagedType.U8)] ref long
pcbSize);

[PreserveSig] int InitNew();

}

</CSHARP>

Any help is much appreciated.

Thanks
NG
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top