• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 507
  • Last Modified:

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

}
0
lapucca
Asked:
lapucca
  • 2
  • 2
1 Solution
 
dunglaCommented:
Just remove static void Main() method. COM class never have Main method
0
 
lapuccaAuthor Commented:
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
 
dunglaCommented:
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
 
lapuccaAuthor Commented:
Thank you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now