?
Solved

I need help with creating a Dll so I can call using COM interop

Posted on 2006-05-10
4
Medium Priority
?
504 Views
Last Modified: 2008-02-07
Hi, I got the following code in a project for creating dll but I'm getting compiling error.  I copied the code (from Main()) from Ineternet but becuase I don't know how COM interop and how to code to create a dll that I'm sure the code looks weird to those know how.  Can someone tell me how to arrange the code I have below so it would compile into a dll that I can call later?  Thank you.


using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
using System.DirectoryServices;

namespace IDsAdminCreateObject
{
static void Main()

{

IDsAdminCreateObject co = new DsAdminCreateObject() as IDsAdminCreateObject;
object nativedsObject = new DirectoryEntry("LDAP://cn=users,dc=yourdomain,dc=local").NativeObject;
co.Initialize(nativedsObject, null, "user");
object newObject = co.CreateModal(DsAdminCreateObject.GetDeskTopWindow());

}

[ComImport, Guid("53554A38-F902-11d2-82B9-00C04F68928B"),

InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]

public interface IDsAdminCreateObject

{

/// <summary>
/// Need to initialize before popping up the new object wizard
/// </summary>
/// <param name="ADsContainerObj">initialized dir object Container object eg: cn=users,DC=domain,dc=local</param>
/// <param name="ADsCopySource">can be null, specifies original object if you want a copy!</param>

/// <param name="ClassName">contains "User", "group", "contact", "inetOrgPerson" etc</param>
void Initialize( [MarshalAs(UnmanagedType.IDispatch)] object ADsContainerObj,
[MarshalAs(UnmanagedType.IDispatch), Optional(), DefaultParameterValue(null)] object ADsCopySource,
[MarshalAs(UnmanagedType.LPWStr)] string ClassName);

/// <summary>
/// Returns native ActiveDirectory object
/// </summary>
/// <param name="hwndParent">handle to parent window, specify 0 (mostly)</param>
[return: MarshalAs(UnmanagedType.IDispatch)]
object CreateModal(IntPtr hwndParent);



}

 

/// <summary>

/// Have our CLSID_DsAdminCreateObject be imported by .NET
/// </summary>
[ComImport, Guid("E301A009-F901-11d2-82B9-00C04F68928B")]
public class DsAdminCreateObject

{

/// we just needed a pointer to a window, if you run this code within a Windows Form, you can fetch a handle to it and hand it over to CreateModal!
[DllImport("user32", EntryPoint = "GetDesktopWindow", ExactSpelling = true, SetLastError = false)]
public static extern IntPtr GetDeskTopWindow();
}

}
0
Comment
Question by:lapucca
  • 2
  • 2
4 Comments
 
LVL 13

Expert Comment

by:dungla
ID: 16656615
Just remove static void Main() method. COM class never have Main method
0
 

Author Comment

by:lapucca
ID: 16660605
Great, that compiled.  I put the main() code into a new Window form project as follow but it's complaining about not finding the dll.  How should I address this?  Thanks.

sing System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.DirectoryServices;

namespace TestPinvok
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
//            static void Main()

//{

IDsAdminCreateObject co = new DsAdminCreateObject() as IDsAdminCreateObject;
object nativedsObject = new DirectoryEntry("LDAP://cn=tcope,cn=users,dc=unity,dc=windev,dc=symark,dc=com").NativeObject;
co.Initialize(nativedsObject, null, "user");
object newObject = co.CreateModal(DsAdminCreateObject.GetDeskTopWindow());

//}
        }
    }
}

Compiler error---------
Error      4      The name 'DsAdminCreateObject' does not exist in the current context      C:\Projects\TestPinvok\TestPinvok\Form1.cs      29
Error      2      The type or namespace name 'DsAdminCreateObject' could not be found (are you missing a using directive or an assembly reference?)      C:\Projects\TestPinvok\TestPinvok\Form1.cs      26
Error      1      The type or namespace name 'IDsAdminCreateObject' could not be found (are you missing a using directive or an assembly reference?)      C:\Projects\TestPinvok\TestPinvok\Form1.cs      26
Error      3      The type or namespace name 'IDsAdminCreateObject' could not be found (are you missing a using directive or an assembly reference?)      C:\Projects\TestPinvok\TestPinvok\Form1.cs      26

0
 
LVL 13

Accepted Solution

by:
dungla earned 2000 total points
ID: 16664082
You must using it

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.DirectoryServices;
// make sure that you have add reference to this assembly.
using IDsAdminCreateObject;
0
 

Author Comment

by:lapucca
ID: 16665015
Thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Loops Section Overview
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question