How did you declare the NEWCPLINFO struct?
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct NEWCPLINFO
{
public int dwSize;
public int dwFlags;
public int dwHelpContext;
public IntPtr lpData;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]public String szName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]public String szInfo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]public String
szHelpFile;
}
Which applet are you trying it on?
I tried with joy.cpl and desk.cpl
Are you sure it handles CPL_NEWINQUIRE, and not just CPL_INQUIRE?
I tried both, without success. It's probably something with my code, but I
can't figure out what. Here's the code of the ControlPanelApplet class (I
apologize for the quality of the code, I wrote it just to test the idea).
using System;
using System.Reflection;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Runtime.Remoting;
namespace ControlPanelTest
{
public class ControlPanelApplet
{
private string appletName;
private string appletDLL;
private static ModuleBuilder appletModuleBuilder;
private Type appletType;
private Object appletInstance;
public string Name;
public string Info;
public int CPL_INIT = 1;
public int CPL_GETCOUNT = 2;
public int CPL_NEWINQUIRE = 8;
public int CPL_STOP = 6;
public int CPL_EXIT = 7;
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct CPLINFO
{
public int idIcon;
public int idName;
public int idInfo;
public IntPtr lpData;
}
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct NEWCPLINFO
{
public int dwSize;
public int dwFlags;
public int dwHelpContext;
public IntPtr lpData;
public IntPtr hIcon;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=32)]public String szName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=64)]public String szInfo;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=128)]public String
szHelpFile;
}
public ControlPanelApplet()
{
appletName = "";
appletDLL = "";
}
public ControlPanelApplet(string name, string dll)
{
appletName = name;
appletDLL = dll;
}
public void Initialize()
{
MethodBuilder methodBuilder;
if( appletModuleBuilder == null )
{
AssemblyName assemblyName = new AssemblyName();
assemblyName.Name = appletName;
AssemblyBuilder assemblyBuilder =
AppDomain.CurrentDomain.DefineDynamicAssembly( assemblyName,
AssemblyBuilderAccess.Run );
appletModuleBuilder = assemblyBuilder.DefineDynamicModule(
"DynamicModule" );
}
TypeBuilder typeBuilder = appletModuleBuilder.DefineType( appletName );
Type[] methodType =
{typeof(IntPtr),typeof(int),typeof(int),typeof(IntPtr)};
methodBuilder = typeBuilder.DefinePInvokeMethod( "CPlApplet", appletDLL,
MethodAttributes.Public | MethodAttributes.Static |
MethodAttributes.PinvokeImpl,
CallingConventions.Standard, typeof(IntPtr), methodType,
CallingConvention.Winapi, CharSet.Auto );
methodBuilder.SetImplementationFlags( MethodImplAttributes.PreserveSig |
methodBuilder.GetMethodImplementationFlags() );
appletType = typeBuilder.CreateType();
appletInstance = Activator.CreateInstance( appletType );
}
public IntPtr Call(string method, IntPtr hwndCPl, int uMsg, int lParam1,
int lParam2)
{
IntPtr hr = (IntPtr)appletType.InvokeMember( method,
BindingFlags.InvokeMethod, null,
appletInstance, new Object[] { hwndCPl, uMsg, lParam1 , new
IntPtr(lParam2) } );
if ( (int)hr != 0 ) Marshal.ThrowExceptionForHR( (int)hr );
return hr;
}
public IntPtr Init( IntPtr hwndCPl )
{
IntPtr hr = (IntPtr)appletType.InvokeMember( "CPlApplet",
BindingFlags.InvokeMethod, null,
appletInstance, new Object[] { hwndCPl, this.CPL_INIT, 0, new
IntPtr(0) } );
if ( (int)hr != 0 ) Marshal.ThrowExceptionForHR( (int)hr );
return hr;
}
public IntPtr Inquiry( IntPtr hwndCPl, int lParam1 )
{
CPLINFO cplinfo = new CPLINFO();
IntPtr ptr;
ptr = Marshal.AllocHGlobal(Marshal.SizeOf(cplinfo));
Marshal.StructureToPtr(cplinfo, ptr, false);
IntPtr hr = (IntPtr)appletType.InvokeMember( "CPlApplet",
BindingFlags.InvokeMethod, null,
appletInstance, new Object[] { hwndCPl, this.CPL_NEWINQUIRE, lParam1-1,
ptr} );
if ( (int)hr != 0 ) Marshal.ThrowExceptionForHR( (int)hr );
CPLINFO n = (CPLINFO)Marshal.PtrToStructure(ptr,typeof(CPLINFO));
this.Name = n.idName.ToString();
this.Info = n.idInfo.ToString();
return hr;
}
public IntPtr NewInquiry( IntPtr hwndCPl, int lParam1 )
{
NEWCPLINFO newcplinfo = new NEWCPLINFO();
newcplinfo.dwSize = Marshal.SizeOf(newcplinfo);
IntPtr ptr;
ptr = Marshal.AllocHGlobal(newcplinfo.dwSize);
Marshal.StructureToPtr(newcplinfo, ptr, false);
IntPtr hr = (IntPtr)appletType.InvokeMember( "CPlApplet",
BindingFlags.InvokeMethod, null,
appletInstance, new Object[] { hwndCPl, this.CPL_NEWINQUIRE, lParam1-1,
ptr} );
if ( (int)hr != 0 ) Marshal.ThrowExceptionForHR( (int)hr );
NEWCPLINFO n =
(NEWCPLINFO)Marshal.PtrToStructure(ptr,typeof(NEWCPLINFO));
this.Name = n.szName;
this.Info = n.szInfo;
return hr;
}
}
}
Thank you,
Gabriele