Por ejemplo, esta es la definición completa de la interfaz IFileOpenDialog, una interfaz de Windows Shell, tomado del sitio de Pinvoke:Al definir una interfaz API de Windows en C#, ¿tengo que definir todos los miembros? ¿Puedo solo definir los métodos que voy a usar?
[ComImport, Guid ("d57c7288-d4ad-4768-be02-9d969532d960"), InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
interface IFileOpenDialog : IFileDialog
{
// Defined on IModalWindow - repeated here due to requirements of COM interop layer
// --------------------------------------------------------------------------------
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), PreserveSig]
int Show ([In] IntPtr parent);
// Defined on IFileDialog - repeated here due to requirements of COM interop layer
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFileTypes ([In] uint cFileTypes, [In] COMDLG_FILTERSPEC[] rgFilterSpec);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFileTypeIndex ([In] uint iFileType);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetFileTypeIndex (out uint piFileType);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void Advise ([In, MarshalAs (UnmanagedType.Interface)] IFileDialogEvents pfde, out uint pdwCookie);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void Unadvise ([In] uint dwCookie);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetOptions ([In] FOS fos);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetOptions (out FOS pfos);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetDefaultFolder ([In, MarshalAs (UnmanagedType.Interface)] IShellItem psi);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFolder ([In, MarshalAs (UnmanagedType.Interface)] IShellItem psi);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetFolder ([MarshalAs (UnmanagedType.Interface)] out IShellItem ppsi);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetCurrentSelection ([MarshalAs (UnmanagedType.Interface)] out IShellItem ppsi);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFileName ([In, MarshalAs (UnmanagedType.LPWStr)] string pszName);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetFileName ([MarshalAs (UnmanagedType.LPWStr)] out string pszName);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetTitle ([In, MarshalAs (UnmanagedType.LPWStr)] string pszTitle);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetOkButtonLabel ([In, MarshalAs (UnmanagedType.LPWStr)] string pszText);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFileNameLabel ([In, MarshalAs (UnmanagedType.LPWStr)] string pszLabel);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetResult ([MarshalAs (UnmanagedType.Interface)] out IShellItem ppsi);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void AddPlace ([In, MarshalAs (UnmanagedType.Interface)] IShellItem psi, NativeMethods.FDAP fdap);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetDefaultExtension ([In, MarshalAs (UnmanagedType.LPWStr)] string pszDefaultExtension);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void Close ([MarshalAs (UnmanagedType.Error)] int hr);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetClientGuid ([In] ref Guid guid);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void ClearClientData ();
// Not supported: IShellItemFilter is not defined, converting to IntPtr
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetFilter ([MarshalAs (UnmanagedType.Interface)] IntPtr pFilter);
// Defined by IFileOpenDialog
// ---------------------------------------------------------------------------------
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetResults ([MarshalAs (UnmanagedType.Interface)] out IShellItemArray ppenum);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void GetSelectedItems ([MarshalAs (UnmanagedType.Interface)] out IShellItemArray ppsai);
}
Si sólo voy a utilizar dos métodos de esta interfaz, puedo definir es como:
[ComImport, Guid ("d57c7288-d4ad-4768-be02-9d969532d960"), InterfaceType (ComInterfaceType.InterfaceIsIUnknown)]
interface IFileOpenDialog : IFileDialog
{
// Defined on IModalWindow - repeated here due to requirements of COM interop layer
// --------------------------------------------------------------------------------
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime), PreserveSig]
int Show ([In] IntPtr parent);
[MethodImpl (MethodImplOptions.InternalCall, MethodCodeType = MethodCodeType.Runtime)]
void SetOptions ([In] FOS fos);
}
¿Va a funcionar? ¿O tengo que definir la interfaz completa con todos los métodos?
¿Lo intentaste? –
Sí, si solo llamo al método Show, funciona, si intento llamar a SetOptions, aparece una excepción: "Intenté leer o escribir en la memoria protegida. Esto a menudo indica que otra memoria está dañada". – AnAurelian