Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

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

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();
}

}
Avatar of dungla
dungla
Flag of Viet Nam image

Just remove static void Main() method. COM class never have Main method
Avatar of lapucca
lapucca

ASKER

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

ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lapucca

ASKER

Thank you.