Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Register COM interop , creating an Interface?

$
0
0

Hello All,

This is my very first time building an application which requires registering COM interop. The output of this application should be a library so as to load it into a different XYZ application that has COM scripting ability. 

The problem I am facing is the application I built works fine on my machine that has Visual Studio 2012(checking Register COM interop)

But when I try it on a different machine, it wont properly in the XYZ application. 

After a lot of research and trying installShield installer , I have concluded that maybe I am missing something in my code. 

I was under the impression, that just declaring COMvisible to true and GUID attribute is enough. 

But I am surely missing something about the interface. 

I will really appreciate is someone can help me with a step by step tutorial for the same. 

Below is what I am doing currently. 

1) create a class library project and add the following code. 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using System.Runtime.InteropServices;
using Ivi.Visa.Interop;
using Microsoft.VisualBasic.CompilerServices;

namespace UsbLib
{

    [Guid("929E1CE0-8084-4814-B63B-F4ED62725227")]
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDual)]
    public class Usb
    {

        ResourceManager rMgr = new ResourceManager();
        FormattedIO488 src = new FormattedIO488();


        object resources = null;
        List<string> idn_response = new List<string>();



        public string FindResources(string resource_id)
        {

            resources = rMgr.FindRsrc("USB[0-9]*::?*");
            string[] resource_list = (string[])resources;



            for (int i = 0; i < resource_list.Length; i++)
            {

                src.IO = (IMessage)rMgr.Open(resource_list[i], AccessMode.NO_LOCK, 2000, null);
                src.IO.Timeout = 2000;

            }

            string allResources = String.Join(";", resource_list);
            return allResources;


            //return resource_list.ToArray();
        }

This part of the code returns a string array of all the VISA resources . 

2) Then I create a console application and call the following code:

sing System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
//using System.Threading.Tasks;
using ErUsbLib;
using System.Runtime.InteropServices;

namespace UsbConsole
{
    [ComVisible(true)]
    [Guid("CD4AD9B1-6FC1-4C38-8CBE-0B0A8C684216")]  
    [ClassInterface(ClassInterfaceType.AutoDual)]
    class Program
    {
        static void Main(string[] args)
        {
            Usb usb = new Usb();
            usb.FindResources("USB[0-9]*::?*");
            }
    }
}

3) Click on Register COM interop. 

And it works on my laptop. 

I am here to learn. I am the only amateur programmer and I am hoping someone will help me. 

I do not intend for someone to  give me a complete code but just suggest where I am going wrong and where I can improve. 

Thanks in advance!


Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>