我用C#编写浏览器,看到这篇文章
http://www.seji.cn/tech/infoView/Article_1335.html
里面用C++的语言,把右键菜单的编码菜单提取出来,放到顶部菜单那。我写成C#语言后,发现最后那函数返回的是Variant类,C#没有这个类,所以发生了以下错误,请问怎么解决呢。未处理的“System.ArgumentException”类型的异常出现在 testform.exe 中。其他信息: 该方法返回了与 Interop 不兼容的 COM Variant 类型。我的源代码是
private void menuSetCode_Click(object sender, System.EventArgs e)
{
IOleCommandTarget cmdt; Guid c =new Guid("000214D1-0000-0000-C000-000000000046");
cmdt = (IOleCommandTarget)GetDocument();
int t=27;

object i=null;
object o=null; cmdt.Exec(ref c, (uint)t, (uint)OLECMDEXECOPT.OLECMDEXECOPT_DODEFAULT, ref i, ref o);

}
我IOleCommandTarget接口的定义是:
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
public struct OLECMDTEXT
{
public uint cmdtextf;
public uint cwActual;
public uint cwBuf;
[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]public char rgwz;
}[StructLayout(LayoutKind.Sequential)]
public struct OLECMD
{
public uint cmdID;
public uint cmdf;
}// Interop definition for IOleCommandTarget.
[ComImport,
Guid("b722bccb-4e68-101b-a2bc-00aa00404770"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
public interface IOleCommandTarget
{
//IMPORTANT: The order of the methods is critical here. You
//perform early binding in most cases, so the order of the methods
//here MUST match the order of their vtable layout (which is determined
//by their layout in IDL). The interop calls key off the vtable ordering,
//not the symbolic names. Therefore, if you switched these method declarations
//and tried to call the Exec method on an IOleCommandTarget interface from your
//application, it would translate into a call to the QueryStatus method instead.
void QueryStatus(ref Guid pguidCmdGroup, UInt32 cCmds,
[MarshalAs(UnmanagedType.LPArray, SizeParamIndex=1)] OLECMD[] prgCmds, ref OLECMDTEXT CmdText);
void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, ref object pvaIn, ref object pvaOut);}有什么办法可以成功接住这个Variant还有请教C#的axwebBrowser如何实现脱机工作和调用HTML编辑器(就是平时IE按一下编辑,就可以自动弹出WORD之类的东西来编辑当前网页。)谢谢

解决方案 »

  1.   

    to 有什么办法可以成功接住这个Variant如果你是用object定义的话,不妨试试用IntPtr来定义参数类型。to 把右键菜单的编码菜单提取出来,放到顶部菜单那。ContextMenu放到MainMenu中?
    在C#做这个操作很复杂吗
      

  2.   

    我一开始也试过用IntPtr,我是这样做的,把Exec重新定义
    void Exec(ref Guid pguidCmdGroup, uint nCmdId, uint nCmdExecOpt, ref object pvaIn, IntPtr  pvaOut);但是仍然出现未把对象引用赋值的错误。
    to ContextMenu放到MainMenu中?
    在C#做这个操作很复杂吗我是第一次开发Form程序,不太懂啊,希望高手指点一下。
      

  3.   

    说错了
    那个错误应该是System.NullReferenceException: 未将对象引用设置到对象的实例。
      

  4.   

    to 我是第一次开发Form程序,不太懂啊,希望高手指点一下。例如:
    mnuFile.MenuItems.Add( mnuContext.MenuItems[0] );
    //mnuFile is a menu item in main menu
    //mnuContext is a context menu object
      

  5.   

    to 例如:
    问题的核心是那个contextmenu不是在我的form里面,它是在我的axwebBrowser里面,是不能显式调用的。