Getting the COM IDataObject interface from the DataObject class

B

Bob Staheli

The .Net DataObject class implements the COM/OLE IDataObject interface , so
how do I get it.

I have tried this, but it does not work :

// Declare the COM/OLE IDataObject interface

[ComImport, Guid("0000010E-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleDataObject
{
[PreserveSig]
int OleGetData(FORMATETC pFormatetc, [Out] STGMEDIUM pMedium);
[PreserveSig]
int OleGetDataHere(FORMATETC pFormatetc, [In, Out] STGMEDIUM pMedium);
[PreserveSig]
int OleQueryGetData(FORMATETC pFormatetc);
[PreserveSig]
int OleGetCanonicalFormatEtc(FORMATETC pformatectIn, [Out] FORMATETC
pformatetcOut);
[PreserveSig]
int OleSetData(FORMATETC pFormatectIn, STGMEDIUM pmedium, int fRelease);
[return: MarshalAs(UnmanagedType.Interface)]
IEnumFORMATETC OleEnumFormatEtc([In, MarshalAs(UnmanagedType.U4)] int
dwDirection);
[PreserveSig]
int OleDAdvise(FORMATETC pFormatetc, [In, MarshalAs(UnmanagedType.U4)] int
advf, [In, MarshalAs(UnmanagedType.Interface)] object pAdvSink, [Out,
MarshalAs(UnmanagedType.LPArray)] int[] pdwConnection);
[PreserveSig]
int OleDUnadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int OleEnumDAdvise([Out, MarshalAs(UnmanagedType.LPArray)] object[]
ppenumAdvise);
}

-------------------

DataObject data = new DataObject();
IOleDataObject ido = data as IOleDataObject
--------------

When this is run, ido is null. I have also tried varrious other methods like
using Marshal.GetIUnknownForObject, Marshal.QueryInterface,
Marshal.GetTypedObjectForIUnknown, but none of the approaches work.

What am I doing wrong?

Thanks
Bob
 
N

Nicholas Paldino [.NET/C# MVP]

Bob,

You can use the IDataObject in the
System.Runtime.InteropServices.ComTypes namespace. It is the one you are
looking for.

The DataObject class in the System.Windows.Forms namespace implements
this as well.

Hope this helps.
 
B

Bob Staheli

Hi Nicholas,

Thanks for your reply.

However, System.Runtime.InteropServices.ComTypes.IDataObject is only
present in .Net 2.0. I am using .Net 1.1.

How can I do that in this case?

Bob


Nicholas Paldino said:
Bob,

You can use the IDataObject in the
System.Runtime.InteropServices.ComTypes namespace. It is the one you are
looking for.

The DataObject class in the System.Windows.Forms namespace implements
this as well.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bob Staheli said:
The .Net DataObject class implements the COM/OLE IDataObject interface ,
so how do I get it.

I have tried this, but it does not work :

// Declare the COM/OLE IDataObject interface

[ComImport, Guid("0000010E-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleDataObject
{
[PreserveSig]
int OleGetData(FORMATETC pFormatetc, [Out] STGMEDIUM pMedium);
[PreserveSig]
int OleGetDataHere(FORMATETC pFormatetc, [In, Out] STGMEDIUM pMedium);
[PreserveSig]
int OleQueryGetData(FORMATETC pFormatetc);
[PreserveSig]
int OleGetCanonicalFormatEtc(FORMATETC pformatectIn, [Out] FORMATETC
pformatetcOut);
[PreserveSig]
int OleSetData(FORMATETC pFormatectIn, STGMEDIUM pmedium, int fRelease);
[return: MarshalAs(UnmanagedType.Interface)]
IEnumFORMATETC OleEnumFormatEtc([In, MarshalAs(UnmanagedType.U4)] int
dwDirection);
[PreserveSig]
int OleDAdvise(FORMATETC pFormatetc, [In, MarshalAs(UnmanagedType.U4)]
int advf, [In, MarshalAs(UnmanagedType.Interface)] object pAdvSink, [Out,
MarshalAs(UnmanagedType.LPArray)] int[] pdwConnection);
[PreserveSig]
int OleDUnadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int OleEnumDAdvise([Out, MarshalAs(UnmanagedType.LPArray)] object[]
ppenumAdvise);
}

-------------------

DataObject data = new DataObject();
IOleDataObject ido = data as IOleDataObject
--------------

When this is run, ido is null. I have also tried varrious other methods
like using Marshal.GetIUnknownForObject, Marshal.QueryInterface,
Marshal.GetTypedObjectForIUnknown, but none of the approaches work.

What am I doing wrong?

Thanks
Bob
 
N

Nicholas Paldino [.NET/C# MVP]

Bob,

You should be able to declare this in your code, and it should work:

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("0000010E-0000-0000-C000-000000000046")]
public interface IDataObject
{
void GetData([In] ref FORMATETC format, out STGMEDIUM medium);
void GetDataHere([In] ref FORMATETC format, ref STGMEDIUM medium);
[PreserveSig]
int QueryGetData([In] ref FORMATETC format);
[PreserveSig]
int GetCanonicalFormatEtc([In] ref FORMATETC formatIn, out FORMATETC
formatOut);
void SetData([In] ref FORMATETC formatIn, [In] ref STGMEDIUM medium,
[MarshalAs(UnmanagedType.Bool)] bool release);
IEnumFORMATETC EnumFormatEtc(DATADIR direction);
[PreserveSig]
int DAdvise([In] ref FORMATETC pFormatetc, ADVF advf, IAdviseSink
adviseSink, out int connection);
void DUnadvise(int connection);
[PreserveSig]
int EnumDAdvise(out IEnumSTATDATA enumAdvise);
}-- - Nicholas Paldino [.NET/C# MVP] -
(e-mail address removed)

Bob Staheli said:
Hi Nicholas,

Thanks for your reply.

However, System.Runtime.InteropServices.ComTypes.IDataObject is only
present in .Net 2.0. I am using .Net 1.1.

How can I do that in this case?

Bob


Nicholas Paldino said:
Bob,

You can use the IDataObject in the
System.Runtime.InteropServices.ComTypes namespace. It is the one you are
looking for.

The DataObject class in the System.Windows.Forms namespace implements
this as well.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Bob Staheli said:
The .Net DataObject class implements the COM/OLE IDataObject interface ,
so how do I get it.

I have tried this, but it does not work :

// Declare the COM/OLE IDataObject interface

[ComImport, Guid("0000010E-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleDataObject
{
[PreserveSig]
int OleGetData(FORMATETC pFormatetc, [Out] STGMEDIUM pMedium);
[PreserveSig]
int OleGetDataHere(FORMATETC pFormatetc, [In, Out] STGMEDIUM pMedium);
[PreserveSig]
int OleQueryGetData(FORMATETC pFormatetc);
[PreserveSig]
int OleGetCanonicalFormatEtc(FORMATETC pformatectIn, [Out] FORMATETC
pformatetcOut);
[PreserveSig]
int OleSetData(FORMATETC pFormatectIn, STGMEDIUM pmedium, int fRelease);
[return: MarshalAs(UnmanagedType.Interface)]
IEnumFORMATETC OleEnumFormatEtc([In, MarshalAs(UnmanagedType.U4)] int
dwDirection);
[PreserveSig]
int OleDAdvise(FORMATETC pFormatetc, [In, MarshalAs(UnmanagedType.U4)]
int advf, [In, MarshalAs(UnmanagedType.Interface)] object pAdvSink,
[Out, MarshalAs(UnmanagedType.LPArray)] int[] pdwConnection);
[PreserveSig]
int OleDUnadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int OleEnumDAdvise([Out, MarshalAs(UnmanagedType.LPArray)] object[]
ppenumAdvise);
}

-------------------

DataObject data = new DataObject();
IOleDataObject ido = data as IOleDataObject
--------------

When this is run, ido is null. I have also tried varrious other methods
like using Marshal.GetIUnknownForObject, Marshal.QueryInterface,
Marshal.GetTypedObjectForIUnknown, but none of the approaches work.

What am I doing wrong?

Thanks
Bob
 
B

Bob Staheli

I tried your declaration but it didnt work..most wierd.

I should note that I changed IAdviseSink from the DAdvice method to an
[In, MarshalAs(UnmanagedType.Interface)] object - does that make a
difference - i would think it is only the GUID applied to the interface that
matters...

Once again..i am using this code :
DataObject d = new DataObject();
IDataObject id = d as IDataObject;

id is still null.

I am out of ideas now.

Bob



Nicholas Paldino said:
Bob,

You should be able to declare this in your code, and it should work:

[ComImport, InterfaceType(ComInterfaceType.InterfaceIsIUnknown),
Guid("0000010E-0000-0000-C000-000000000046")]
public interface IDataObject
{
void GetData([In] ref FORMATETC format, out STGMEDIUM medium);
void GetDataHere([In] ref FORMATETC format, ref STGMEDIUM medium);
[PreserveSig]
int QueryGetData([In] ref FORMATETC format);
[PreserveSig]
int GetCanonicalFormatEtc([In] ref FORMATETC formatIn, out FORMATETC
formatOut);
void SetData([In] ref FORMATETC formatIn, [In] ref STGMEDIUM medium,
[MarshalAs(UnmanagedType.Bool)] bool release);
IEnumFORMATETC EnumFormatEtc(DATADIR direction);
[PreserveSig]
int DAdvise([In] ref FORMATETC pFormatetc, ADVF advf, IAdviseSink
adviseSink, out int connection);
void DUnadvise(int connection);
[PreserveSig]
int EnumDAdvise(out IEnumSTATDATA enumAdvise);
}-- - Nicholas Paldino [.NET/C# MVP] -
(e-mail address removed)

Bob Staheli said:
Hi Nicholas,

Thanks for your reply.

However, System.Runtime.InteropServices.ComTypes.IDataObject is only
present in .Net 2.0. I am using .Net 1.1.

How can I do that in this case?

Bob


Nicholas Paldino said:
Bob,

You can use the IDataObject in the
System.Runtime.InteropServices.ComTypes namespace. It is the one you
are looking for.

The DataObject class in the System.Windows.Forms namespace implements
this as well.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

The .Net DataObject class implements the COM/OLE IDataObject interface
, so how do I get it.

I have tried this, but it does not work :

// Declare the COM/OLE IDataObject interface

[ComImport, Guid("0000010E-0000-0000-C000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleDataObject
{
[PreserveSig]
int OleGetData(FORMATETC pFormatetc, [Out] STGMEDIUM pMedium);
[PreserveSig]
int OleGetDataHere(FORMATETC pFormatetc, [In, Out] STGMEDIUM pMedium);
[PreserveSig]
int OleQueryGetData(FORMATETC pFormatetc);
[PreserveSig]
int OleGetCanonicalFormatEtc(FORMATETC pformatectIn, [Out] FORMATETC
pformatetcOut);
[PreserveSig]
int OleSetData(FORMATETC pFormatectIn, STGMEDIUM pmedium, int
fRelease);
[return: MarshalAs(UnmanagedType.Interface)]
IEnumFORMATETC OleEnumFormatEtc([In, MarshalAs(UnmanagedType.U4)] int
dwDirection);
[PreserveSig]
int OleDAdvise(FORMATETC pFormatetc, [In, MarshalAs(UnmanagedType.U4)]
int advf, [In, MarshalAs(UnmanagedType.Interface)] object pAdvSink,
[Out, MarshalAs(UnmanagedType.LPArray)] int[] pdwConnection);
[PreserveSig]
int OleDUnadvise([In, MarshalAs(UnmanagedType.U4)] int dwConnection);
[PreserveSig]
int OleEnumDAdvise([Out, MarshalAs(UnmanagedType.LPArray)] object[]
ppenumAdvise);
}

-------------------

DataObject data = new DataObject();
IOleDataObject ido = data as IOleDataObject
--------------

When this is run, ido is null. I have also tried varrious other methods
like using Marshal.GetIUnknownForObject, Marshal.QueryInterface,
Marshal.GetTypedObjectForIUnknown, but none of the approaches work.

What am I doing wrong?

Thanks
Bob
 

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

Similar Threads


Top