Options

Easy way to make a com DLL in c#?

claras2005claras2005 Member Posts: 17
Hi

I am currently playing around with visual studio C# express 2005 beta 2 and want to make a dll that I can use as an automation from within Navision. I have done this sometimes before with VB .net and it was relatively easy because there is a wizard to make the comclass . But how do I do this in C#?
«1

Comments

  • Options
    DenSterDenSter Member Posts: 8,304
    It shouldn't matter if you use VB.NET or C#, since they are both using the same runtime and tools. It looks to me that you are using a VS beta that probably does it differently. Have you checked the public C# newsgroups on news.microsoft.com?
  • Options
    claras2005claras2005 Member Posts: 17
    I have searched the net a little but I have no solution.

    I found this:

    http://www.codeproject.com/csharp/CSCom ... ect=905084

    It seems that is not because i am using a Beta, somehow there a differences between the tools you have on VB.net in comparison to C# .net

    So I still need help :) Anyone who have made a dll for navsion using C# pls help! Even with input on how to manually make the DLL
  • Options
    fbfb Member Posts: 246
    Have you followed the link listed here?:

    http://www.mibuso.com/forum/viewtopic.php?t=6955
  • Options
    claras2005claras2005 Member Posts: 17
    No but it looks really promissing :)

    Thx for the link, I think that will make it work :)
  • Options
    claras2005claras2005 Member Posts: 17
    Hmmmm

    I have tried his example, I can build the dll and all. But I cannot register it :(

    not with regsvr32 (dllregisterserver is missing error), not with regasm.exe (no valid assembly error) and not from within navision (could not register OLE).

    Any ideas what I need to do?
  • Options
    Luc_VanDyckLuc_VanDyck Member, Moderator, Administrator Posts: 3,633
    No support using PM or e-mail - Please use this forum. BC TechDays 2024: 13 & 14 June 2024, Antwerp (Belgium)
  • Options
    claras2005claras2005 Member Posts: 17
    Eventhough the link had some nice info it did not help me (probably because im stupid).

    I still get an error when i use regasm.exe. It tells me that it could not read the file because its not a valid assembly.

    Do any of you have a small C# demo project they could share? Just a hello world function that can be called from navsion so that I can check it up against my code? I probably have a small detail wrong.

    What I have done so fare:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ClassLibrary1
    {
        [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    
        public interface INavisionTest
        {
            int Add2Numbers(int a, int b);
        }
    
        [ClassInterface(ClassInterfaceType.None)]
    
        public class NavisionTest : INavisionTest
        {
            public NavisionTest()
            {
            }
    
            public int Add2Numbers(int a, int b)
            {
               return (a + b);
            }
        }
    }
    
    I have set the properties in the assembly to show com and the interop property in the project is also set to true.
  • Options
    claras2005claras2005 Member Posts: 17
    Sometimes one gets the best ideas while sleeping J

    When I woke up this morning I had the idea that it might be the regasm.exe that I was using that gave me the problem. So I searched my hard disk for the newest version, and you know what I found a newer one! And now I can create the assembly. As I had to go to work I have not tested the DLL yet but I am pretty sure everything will work out fine now!

    Funny how one can focus so much on finding an error in the code and then totally overlook the real simple solution J

    Thx for all the excellent input to my forum thread and sorry for all my stupid noob questions!

    Ill post a entry when I have everything working.
  • Options
    DenSterDenSter Member Posts: 8,304
    There is no such thing as stupid noob questions. You are doing stuff that very few people do, so you're way ahead of many of us. If you're willing, I would be interested to see a little working sample, maybe in the tips and tricks section, or even a download.
  • Options
    claras2005claras2005 Member Posts: 17
    When (and if) I have something working I will post an example of a small hello world dll or something.
  • Options
    demy75demy75 Member Posts: 31
    I've written a post on my blog on how to write a DLL that works with Navision.
    Check it here:
    http://demiliani.com/blog/archive/2005/09/19/2965.aspx
  • Options
    kinekine Member Posts: 12,562
    Easy way:
    1) run Visual Studio (I am using 2003 .Net)
    2) File - new - project
    3) Visual C# - Windows Control Library (or Class Library), enter the name
    4) Define what you need to do (all methods you want to public must be defined as public...)
    5) use
    	[ClassInterface(ClassInterfaceType.AutoDual)]
    
    before your class definition
    (using System.Runtime.InteropServices;)
    6) Open properties for your project, Configuration properties - Register For COM interop = true
    7) Build the project
    8) use it in Navision...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    MrWhoMrWho Member Posts: 59
    Hi, thanks for the tip, it worked great!

    But is there a way to execute the "Windows Control Library" itself, so it will be shown in Navision. Or is that impossible since there`s nothing to add it on to?
  • Options
    kinekine Member Posts: 12,562
    Because I am begginer in C#, I do not know how to "public" something through inheritance (to not need define each function I want to public once more...)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    DenSterDenSter Member Posts: 8,304
    I don't know what you mean by 'windows control library', but you can check the automation server list for what is available. One of my coworkers got the speech thing to work, and now he's putting in all sorts of messages in his test databases :roll:

    Open the globals or the locals and go to the variables tab. Enter a variable name, set the type to automation and click the look up button. From there on out it's basically a trial and error process.
  • Options
    MrWhoMrWho Member Posts: 59
    Thank you for the answer, but I find my answer in Navision 4.00 Development II chapter 9, where it says "However, Microsoft Navision only supports non-visual controls".

    So, then there`s no point in using Windows Control Library, you can just use Control Library, since "Windows Control Library" can`t be added or shown anyhow in Navision.
  • Options
    ta5ta5 Member Posts: 1,164
    Hi
    I tried this in Visual Basic with info from demy75
    I've written a post on my blog on how to write a DLL that works with Navision.
    Check it here:
    http://demiliani.com/blog/archive/2005/09/19/2965.aspx

    my vb class:
    Imports System.Runtime.InteropServices ' für "<ClassInterFace..."
    Imports System.Reflection
    Public Interface INavControl
        Function hellofrom_dotNet() As String
    End Interface
    <ClassInterface(ClassInterfaceType.AutoDual)> _
    Public Class FirstNaviClass
        Implements INavControl
        Public Function hellofrom_dotNet() As String Implements INavControl.hellofrom_dotNet
            Return "Hello from dotnet"
        End Function
    End Class
    

    So far it works, what puzzles me is that in the symbol menu in Navision I see some Functions and Properties which I did not define myself (Equals, GetHashCode,GetType,ToString).

    Thank you in advance
    Thomas
  • Options
    kinekine Member Posts: 12,562
    Yes, I am solving same thing in Visual Studio 2005 now. What I found yet is, that the DualInterface is not recommanded. But I do not know, how to do it without this... If I do not use this, I do not see the functions and properties in Navision... (and I do not want to use external tools)...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    claras2005claras2005 Member Posts: 17
    It must be possible to make some kind of visual DLL because if you check the mibuso download section someone has made a mini internet browser in navision form via a DLL. And some other guy has made a treeview using delphi.
  • Options
    kauffmannkauffmann Member Posts: 56
    Here is an C# example of how to code a .Net dll that is available to Navision, without exposing all of the default Com methods.

    This example will expose just one property: HelloWorld()
    using System;
    using System.Runtime.InteropServices;
    
    namespace WhatEver
    {
    	
      public interface IMyClassInterface
      {
        [DispId(1)]
          string HelloWorld {get;}
      }
    
      [ClassInterface(ClassInterfaceType.None)]
      [GuidAttribute("E0A33D6F-FCD2-4e85-9A2D-1A908EC39598")]
      public class MyClass : IMyClassInterface
      {
        public MyClass() {}
    
        public string HelloWorld
        {
          get
          {
            return("Hello World!");
          }
        }
      }
    }
    
    A good programmer makes al the right mistakes
    My blog
  • Options
    kinekine Member Posts: 12,562
    Ok, thanks, it seems that the [DispId(x)] is the key I need... :-)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kauffmannkauffmann Member Posts: 56
    Yes, the fixed DispId is the key factor here.

    My thoughts about this: Navision is not capable of using late binding. For that reason you have to know the DispId at designtime. Specifying the DispId in your interface makes the DispId fixed, so Navision is able to store the correct DispId during compilation and use it runtime.
    A good programmer makes al the right mistakes
    My blog
  • Options
    ta5ta5 Member Posts: 1,164
    Hi
    I've adapted the c# code (from "kaufmann") to vb.net, looks like this:
    Imports System.Runtime.InteropServices
    Imports System.Reflection
    <InterfaceType(ComInterfaceType.InterfaceIsIDispatch)> _
    Public Interface INavControl
        Function hellofrom_dotNet() As String
    End Interface
    <ClassInterface(ClassInterfaceType.None)> _
    Public Class FirstNaviClass
        Implements INavControl
        Public Function hellofrom_dotNet() As String Implements INavControl.hellofrom_dotNet
            Return "Hello from dotnet"
        End Function
    End Class
    

    The default com methods are not shown any more in Navision, but still INavControl and FirstNaviClass are both visible. Is there a possibility to avoid that?
    Regards
    Thomas
  • Options
    kauffmannkauffmann Member Posts: 56
    By default, the interfaces used by a .NET Class are transformed to dual interfaces in the IDL. This allows the client to get the best of both early binding and late binding. However, there may be occasions when you want the interface to be a pure-dispinterface or a custom IUnknown only based interface. You can override the default type of the interface using the InterfaceTypeAttribute.
    To have the Interface hidden for Navision you have to specify that the Interface is type IUnknown.
    By using
    <InterfaceType(ComInterfaceType.InterfaceIsIUnknown)> _
    Interface MyInterface
    

    However, this will hide the interface information of the methods and properties. If you try this you will see that Navision is not able to determine the return type of the methods and instead is using something like
    HRESULT = Property(VARIANT parameter)
    

    The other option is to specify your Class as DualInterface. Because every .Net class derives the base interface from the _Object interface, you will see that the Equals, ToString and GetHash methods are specified, along with your interface methods.
    A good programmer makes al the right mistakes
    My blog
  • Options
    ta5ta5 Member Posts: 1,164
    Thank you!
    Regards
    Thomas
  • Options
    kinekine Member Posts: 12,562
    Still have problem with visibility of the functions and properties...
    I am using VS 2005.

    Example of my code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Drawing;
    using System.Data;
    using System.Text;
    using System.Windows.Forms;
    using System.Reflection;
    using System.Runtime.InteropServices;
    
    namespace MyWrapper2005
    {
        [GuidAttribute("MyIFGUID")]
        interface IWrapperClass
        {
            [DispId(1)]
            string Property1 { get; set; }
            [DispId(2)]
            public void Function1(string x, string y, short zoom);
        }
    
        [ClassInterface(ClassInterfaceType.None)]
        [GuidAttribute("myGUID")]
        public partial class WrapperClass : Form, IWrapperClass
        {
            public string Property1        {
                get
                {
                    return Component.Property1;
                }
                set
                {
                    Component.Property1 = value;
                }
            }
            public void Function1(string x, string y, short zoom)
            {
                Component.Function1(x, y, zoom);
            }
        }
    }
    

    But still I am not able to see the properties (I see only the class in Navision, not the interface). I am using StrongName for the class...

    I have "Make assembly COM-visible = true" in Project property, "Register for COM interop = true".

    Have someone tip where can be the problem?

    The class is wrapper for one Automation, which I am opening in new window (it is why the class is inherited from Form class) and on this form I placed visual component "Component".
    When I use ClassInterfaceType.AutoDual, I can use the class without problems (but I see all the functions and properties from _Object)...
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kauffmannkauffmann Member Posts: 56
    Try to add the attribute
    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
    

    to your interface.
    A good programmer makes al the right mistakes
    My blog
  • Options
    kinekine Member Posts: 12,562
    Added, without success... :-/
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
  • Options
    kauffmannkauffmann Member Posts: 56
    Obviously you are already using Visual Studio 2005. Maybe there is an difference between registering components for ComInterop between Visual Studio 2003 and 2005:?: I'm not sure, but will test it today or tomorrow.
    A good programmer makes al the right mistakes
    My blog
  • Options
    kinekine Member Posts: 12,562
    How can I unregister the .net interop DLL? (I know only the way of regsvr32 but this is not the correct one in this case)
    Kamil Sacek
    MVP - Dynamics NAV
    My BLOG
    NAVERTICA a.s.
Sign In or Register to comment.