Click here to Skip to main content
12,694,018 members (34,076 online)
Click here to Skip to main content

Stats

66.6K views
427 downloads
40 bookmarked
Posted

Bringing DCOM remoting functionality to Windows CE and .NET CF2.0

, 17 Apr 2006 CPOL
This article shows how to use DCOM on Windows CE 5.0. We will add full DCOM rich error information, and implement a DCOM interface between a Windows XP .NET 2.0 client and Windows CE DCOM server. With this code, it is possible to code .NET remoting alike functionality through DCOM interop.
////////////////////////////////////////////////////////////////////
//
//  Copyright (C) 2006 Werner Willemsens
//
////////////////////////////////////////////////////////////////////

using System;
using System.Drawing;
using System.Collections;
using System.Windows.Forms;
using System.Data;

using System.Runtime.InteropServices;

namespace NTLM
{
	/// <summary>
	/// Summary description for Form1.
	/// </summary>
	public class Form1 : System.Windows.Forms.Form
	{
		private System.Windows.Forms.Panel panel1;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.TextBox textBox1;
		private System.Windows.Forms.TextBox textBox2;
		private System.Windows.Forms.TextBox textBox3;
		private System.Windows.Forms.Button button1;
		private System.Windows.Forms.ListBox listBox1;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Button button2;
	
		[DllImport("Ntlmssp.dll")]
	    public static extern bool NTLMSetUserInfo(char[] pszUser, char[] pszPassword);
		[DllImport("Ntlmssp.dll")]
	    public static extern bool NTLMDeleteUser(char[] pszUser);
		[DllImport("Ntlmssp.dll")]
	    public static extern bool NTLMEnumUser(uint index, char[] pszUser, ref uint pcchUser);
	
		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 )
		{
			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()
		{
			System.Resources.ResourceManager resources = new System.Resources.ResourceManager(typeof(Form1));
			this.panel1 = new System.Windows.Forms.Panel();
			this.label3 = new System.Windows.Forms.Label();
			this.label2 = new System.Windows.Forms.Label();
			this.label1 = new System.Windows.Forms.Label();
			this.textBox1 = new System.Windows.Forms.TextBox();
			this.textBox2 = new System.Windows.Forms.TextBox();
			this.textBox3 = new System.Windows.Forms.TextBox();
			this.button1 = new System.Windows.Forms.Button();
			this.listBox1 = new System.Windows.Forms.ListBox();
			this.button2 = new System.Windows.Forms.Button();
			this.label4 = new System.Windows.Forms.Label();
			// 
			// panel1
			// 
			this.panel1.BackColor = System.Drawing.Color.LightGray;
			this.panel1.Controls.Add(this.label3);
			this.panel1.Controls.Add(this.label2);
			this.panel1.Controls.Add(this.label1);
			this.panel1.Controls.Add(this.textBox1);
			this.panel1.Controls.Add(this.textBox2);
			this.panel1.Controls.Add(this.textBox3);
			this.panel1.Controls.Add(this.button1);
			this.panel1.Controls.Add(this.listBox1);
			this.panel1.Controls.Add(this.button2);
			this.panel1.Location = new System.Drawing.Point(8, 24);
			this.panel1.Size = new System.Drawing.Size(312, 160);
			// 
			// label3
			// 
			this.label3.Location = new System.Drawing.Point(16, 72);
			this.label3.Size = new System.Drawing.Size(80, 20);
			this.label3.Text = "Confirm pwd";
			// 
			// label2
			// 
			this.label2.Location = new System.Drawing.Point(16, 40);
			this.label2.Size = new System.Drawing.Size(80, 20);
			this.label2.Text = "Password";
			// 
			// label1
			// 
			this.label1.Location = new System.Drawing.Point(16, 8);
			this.label1.Size = new System.Drawing.Size(80, 20);
			this.label1.Text = "User name";
			// 
			// textBox1
			// 
			this.textBox1.Location = new System.Drawing.Point(96, 8);
			this.textBox1.Size = new System.Drawing.Size(88, 22);
			this.textBox1.Text = "";
			// 
			// textBox2
			// 
			this.textBox2.Location = new System.Drawing.Point(96, 40);
			this.textBox2.PasswordChar = '*';
			this.textBox2.Size = new System.Drawing.Size(88, 22);
			this.textBox2.Text = "";
			this.textBox2.GotFocus += new System.EventHandler(this.textBox2_GotFocus);
			// 
			// textBox3
			// 
			this.textBox3.Location = new System.Drawing.Point(96, 72);
			this.textBox3.PasswordChar = '*';
			this.textBox3.Size = new System.Drawing.Size(88, 22);
			this.textBox3.Text = "";
			this.textBox3.GotFocus += new System.EventHandler(this.textBox3_GotFocus);
			// 
			// button1
			// 
			this.button1.Location = new System.Drawing.Point(40, 128);
			this.button1.Size = new System.Drawing.Size(104, 20);
			this.button1.Text = "Add user";
			this.button1.Click += new System.EventHandler(this.button1_Click);
			// 
			// listBox1
			// 
			this.listBox1.Location = new System.Drawing.Point(192, 8);
			this.listBox1.Size = new System.Drawing.Size(104, 100);
			// 
			// button2
			// 
			this.button2.Location = new System.Drawing.Point(192, 128);
			this.button2.Size = new System.Drawing.Size(104, 20);
			this.button2.Text = "Remove user";
			this.button2.Click += new System.EventHandler(this.button2_Click);
			// 
			// label4
			// 
			this.label4.Location = new System.Drawing.Point(8, 8);
			this.label4.Size = new System.Drawing.Size(88, 16);
			this.label4.Text = "NTLM user";
			// 
			// Form1
			// 
			this.ClientSize = new System.Drawing.Size(330, 191);
			this.Controls.Add(this.panel1);
			this.Controls.Add(this.label4);
			this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
			this.MaximizeBox = false;
			this.Text = "User Account Manager";
			this.Load += new System.EventHandler(this.Form1_Load);

		}
		#endregion

		/// <summary>
		/// The main entry point for the application.
		/// </summary>

		static void Main() 
		{
			Application.Run(new Form1());
		}

		private ArrayList GetUsers()
		{
			uint uiCount=0;
			bool b=false;
			ArrayList al = new ArrayList();
			do
			{
				char [] cUser = new char[128];
				uint uiSize = (uint)cUser.Length;
				string sUser = new string(cUser);
				b = NTLMEnumUser(uiCount, cUser, ref uiSize);
				if (b) al.Add(new string(cUser, 0, (int)uiSize));
				uiCount++;
			} while (b == true);

			return al;
		}

		private void Form1_Load(object sender, System.EventArgs e)
		{
			textBox1.Text = "";
			textBox2.Text = "********";
			textBox3.Text = "********";

			ArrayList al=GetUsers();
			for (int i=0; i<al.Count; i++)
			{
				listBox1.Items.Add(al[i]);
			}
			textBox1.Focus();
		}

		private void button1_Click(object sender, System.EventArgs e)
		{
			bool bUserAccountExists = false;
			if (textBox1.Text == "")
			{
				MessageBox.Show ("No user name entered", 
                                 "Add user account",
						         MessageBoxButtons.OK,
						         MessageBoxIcon.Exclamation,
						         MessageBoxDefaultButton.Button1);
				return;
			}

			if (textBox2.Text != textBox3.Text)
			{
				MessageBox.Show ("Password verification failed",
                                 "Add user account",
						         MessageBoxButtons.OK,
						         MessageBoxIcon.Exclamation,
						         MessageBoxDefaultButton.Button1);
				return;
			}

			for (int i=0; i<listBox1.Items.Count; i++)
			{
				if (textBox1.Text == listBox1.Items[i].ToString())
				{
					DialogResult r = MessageBox.Show("User account '" + textBox1.Text + "' already exists\n" +
						                             "Are you sure you want to change its password?",
                                                     "Add user account",
						                             MessageBoxButtons.YesNo,
						                             MessageBoxIcon.Question,
						                             MessageBoxDefaultButton.Button2);
					if (r == DialogResult.No) return;
					bUserAccountExists = true;
				}
			}

			bool b;
			string sUser = textBox1.Text + "\0";
			string sPwd  = textBox2.Text + "\0";
			b = NTLMSetUserInfo(sUser.ToCharArray(), sPwd.ToCharArray());
			if (!b) 
				MessageBox.Show ("Failed adding new user account",
					             "Add user account",
								 MessageBoxButtons.OK,
								 MessageBoxIcon.Exclamation,
								 MessageBoxDefaultButton.Button1);
			else if (bUserAccountExists == false) 
			{
				listBox1.Items.Add(textBox1.Text);
				MessageBox.Show ("Succesfully added new user account '" + textBox1.Text + "'",
								 "Add user account",
								 MessageBoxButtons.OK,
								 MessageBoxIcon.None,
								 MessageBoxDefaultButton.Button1);
			}
			else
				MessageBox.Show ("Succesfully updated existing user account '" + textBox1.Text + "'",
								 "Add user account",
								 MessageBoxButtons.OK,
								 MessageBoxIcon.None,
								 MessageBoxDefaultButton.Button1);

			textBox1.Text = "";
			textBox2.Text = "********";
			textBox3.Text = "********";
		}

		private void button2_Click(object sender, System.EventArgs e)
		{
			bool b;
			if (listBox1.SelectedIndex > -1)
			{
				string sUser = listBox1.SelectedItem.ToString();
				DialogResult r = MessageBox.Show("Are you sure you want to remove user '" + sUser + "'?",
					"Remove user account",
					MessageBoxButtons.YesNo,
					MessageBoxIcon.Question,
					MessageBoxDefaultButton.Button2);
				if (r == DialogResult.Yes)
				{
					sUser += "\0";
					b = NTLMDeleteUser(sUser.ToCharArray());
					if (b) listBox1.Items.Remove(listBox1.SelectedItem.ToString());
				}
			}
		}

		private void textBox2_GotFocus(object sender, System.EventArgs e)
		{
			textBox2.Text = "";		
		}

		private void textBox3_GotFocus(object sender, System.EventArgs e)
		{
			textBox3.Text = "";		
		}
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Werner Willemsens
Team Leader
Belgium Belgium
No Biography provided

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.170118.1 | Last Updated 17 Apr 2006
Article Copyright 2006 by Werner Willemsens
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid