讨论:VS 2003下的组件程序如何转换为2005下的组件程序?

来源:互联网 发布:cms影视系统商业版 编辑:程序博客网 时间:2024/05/22 02:14
微软提供了两个在VS 2003下实现的较简单的组件程序示例实例1 新建一个解决方案Solution;创建一个简单的组件,项目名称为HelloWorld,该组件中有一个方法WakeUp,作用是显示一条信息“Hello”;创建一个简单的客户端,项目名称为HelloClient,在客户端引用组件,客户端有一个按钮cmdWakeUp,一个文本框txtResponse,单击按钮(调用组件中的WakeUp方法),显示“Hello”。应该说是比较简单的实验,但在VS 2005中调试是出现以下错误信息: .未处理 System.UnauthorizedAccessException  Message="拒绝访问。 (异常来自 HRESULT:0x80070005 (E_ACCESSDENIED))"  Source="mscorlib"  StackTrace:       在 System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)       在 System.EnterpriseServices.Thunk.Proxy.CoCreateObject(Type serverType, Boolean bQuerySCInfo, Boolean& bIsAnotherProcess, String& uri)       在 System.EnterpriseServices.ServicedComponentProxyAttribute.CreateInstance(Type serverType)       在 System.Runtime.Remoting.Activation.ActivationServices.IsCurrentContextOK(Type serverType, Object[] props, Boolean bNewObj)       在 HelloClient.Form1.cmdWakeUp_Click(Object sender, EventArgs e) 位置 D:/Task/Solution/Solution/HelloClient/Form1.cs:行号 102       在 System.Windows.Forms.Control.OnClick(EventArgs e)       在 System.Windows.Forms.Button.OnClick(EventArgs e)       在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)       在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)       在 System.Windows.Forms.Control.WndProc(Message& m)       在 System.Windows.Forms.ButtonBase.WndProc(Message& m)       在 System.Windows.Forms.Button.WndProc(Message& m)       在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)       在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)       在 System.Windows.Forms.Application.Run(Form mainForm)       在 HelloClient.Form1.Main() 位置 D:/Task/Solution/Solution/HelloClient/Form1.cs:行号 97       在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)       在 System.Threading.ThreadHelper.ThreadStart()   为便于理解,特将组件程序代码和相关的客户端代码附在下面:  HelloWorld.cs //////////////////////////////////////////////////// using System; using System.EnterpriseServices; [assembly: ApplicationName("Hello World")] [assembly: ApplicationActivation(ActivationOption.Server)] namespace HelloWorld{  public class MyComponent : ServicedComponent  {   public string WakeUp()   {   return "Hello World!";  }  } }   HelloClient.Fomr1.cs using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data; namespace HelloClient{ /// <summary> /// Summary description for Form1. /// </summary> public class Form1 : System.Windows.Forms.Form {  private System.Windows.Forms.Button cmdWakeUp;  private System.Windows.Forms.TextBox txtResponse;  /// <summary>  /// Required designer variable.  /// </summary>  private System.ComponentModel.Container components = null;   public Form1()  {   //   // Required for Windows Form Designer support   //   InitializeComponent();    //   // TODO: Add any constructor code after InitializeComponent call   //  }   /// <summary>  /// Clean up any resources being used.  /// </summary>  protected override void Dispose( bool disposing )  {   if( disposing )   {    if (components != null)     {     components.Dispose();    }   }   base.Dispose( disposing );  }   #region Windows Form Designer generated code  /// <summary>  /// Required method for Designer support - do not modify  /// the contents of this method with the code editor.  /// </summary>  private void InitializeComponent()  {   this.cmdWakeUp = new System.Windows.Forms.Button();   this.txtResponse = new System.Windows.Forms.TextBox();   this.SuspendLayout();   //    // cmdWakeUp   //    this.cmdWakeUp.Location = new System.Drawing.Point(176, 64);   this.cmdWakeUp.Name = "cmdWakeUp";   this.cmdWakeUp.Size = new System.Drawing.Size(80, 24);   this.cmdWakeUp.TabIndex = 0;   this.cmdWakeUp.Text = "Wake Up";   this.cmdWakeUp.Click += new System.EventHandler(this.cmdWakeUp_Click);   //    // txtResponse   //    this.txtResponse.Location = new System.Drawing.Point(168, 112);   this.txtResponse.Name = "txtResponse";   this.txtResponse.Size = new System.Drawing.Size(88, 20);   this.txtResponse.TabIndex = 1;   this.txtResponse.Text = "";   //    // Form1   //    this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);   this.ClientSize = new System.Drawing.Size(292, 266);   this.Controls.AddRange(new System.Windows.Forms.Control[] {                    this.txtResponse,                    this.cmdWakeUp});   this.Name = "Form1";   this.Text = "Form1";   this.ResumeLayout(false);   }  #endregion   /// <summary>  /// The main entry point for the application.  /// </summary>  [STAThread]  static void Main()   {   Application.Run(new Form1());  }   private void cmdWakeUp_Click(object sender, System.EventArgs e)  {   HelloWorld.MyComponent obj = new HelloWorld.MyComponent();   txtResponse.Text=obj.WakeUp();  } }}
原创粉丝点击