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.InteropServ ices;
using System.DirectoryServices;
namespace IDsAdminCreateObject
{
static void Main()
{
IDsAdminCreateObject co = new DsAdminCreateObject() as IDsAdminCreateObject;
object nativedsObject = new DirectoryEntry("LDAP://cn= users,dc=y ourdomain, dc=local") .NativeObj ect;
co.Initialize(nativedsObje ct, null, "user");
object newObject = co.CreateModal(DsAdminCrea teObject.G etDeskTopW indow());
}
[ComImport, Guid("53554A38-F902-11d2-8 2B9-00C04F 68928B"),
InterfaceType(ComInterface Type.Inter faceIsIUnk nown)]
public interface IDsAdminCreateObject
{
/// <summary>
/// Need to initialize before popping up the new object wizard
/// </summary>
/// <param name="ADsContainerObj">ini tialized dir object Container object eg: cn=users,DC=domain,dc=loca l</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.I Dispatch)] object ADsContainerObj,
[MarshalAs(UnmanagedType.I Dispatch), Optional(), DefaultParameterValue(null )] object ADsCopySource,
[MarshalAs(UnmanagedType.L PWStr)] string ClassName);
/// <summary>
/// Returns native ActiveDirectory object
/// </summary>
/// <param name="hwndParent">handle to parent window, specify 0 (mostly)</param>
[return: MarshalAs(UnmanagedType.ID ispatch)]
object CreateModal(IntPtr hwndParent);
}
/// <summary>
/// Have our CLSID_DsAdminCreateObject be imported by .NET
/// </summary>
[ComImport, Guid("E301A009-F901-11d2-8 2B9-00C04F 68928B")]
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();
}
}
using System;
using System.Collections.Generic
using System.Text;
using System.Runtime.InteropServ
using System.DirectoryServices;
namespace IDsAdminCreateObject
{
static void Main()
{
IDsAdminCreateObject co = new DsAdminCreateObject() as IDsAdminCreateObject;
object nativedsObject = new DirectoryEntry("LDAP://cn=
co.Initialize(nativedsObje
object newObject = co.CreateModal(DsAdminCrea
}
[ComImport, Guid("53554A38-F902-11d2-8
InterfaceType(ComInterface
public interface IDsAdminCreateObject
{
/// <summary>
/// Need to initialize before popping up the new object wizard
/// </summary>
/// <param name="ADsContainerObj">ini
/// <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.I
[MarshalAs(UnmanagedType.I
[MarshalAs(UnmanagedType.L
/// <summary>
/// Returns native ActiveDirectory object
/// </summary>
/// <param name="hwndParent">handle to parent window, specify 0 (mostly)</param>
[return: MarshalAs(UnmanagedType.ID
object CreateModal(IntPtr hwndParent);
}
/// <summary>
/// Have our CLSID_DsAdminCreateObject be imported by .NET
/// </summary>
[ComImport, Guid("E301A009-F901-11d2-8
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();
}
}
Just remove static void Main() method. COM class never have Main method
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.InteropServ ices;
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=u sers,dc=un ity,dc=win dev,dc=sym ark,dc=com ").NativeO bject;
co.Initialize(nativedsObje ct, null, "user");
object newObject = co.CreateModal(DsAdminCrea teObject.G etDeskTopW indow());
//}
}
}
}
Compiler error---------
Error 4 The name 'DsAdminCreateObject' does not exist in the current context C:\Projects\TestPinvok\Tes tPinvok\Fo rm1.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\Tes tPinvok\Fo rm1.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\Tes tPinvok\Fo rm1.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\Tes tPinvok\Fo rm1.cs 26
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.InteropServ
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=
co.Initialize(nativedsObje
object newObject = co.CreateModal(DsAdminCrea
//}
}
}
}
Compiler error---------
Error 4 The name 'DsAdminCreateObject' does not exist in the current context C:\Projects\TestPinvok\Tes
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\Tes
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\Tes
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\Tes
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you.