Tocchetto
Tocchetto

Reputation: 176

COM object work in Console application but not in DLL

I couldn't quite find a question related to that or I was too dumb to understand that other solutions could have been applied to it. Here it goes.

I'm trying to create a DLL which should contain only one function that other programmers can reference on their project in order to use the function. The function simply gets an object that's already running and return it to the caller. It's a function to get the SAP object that's running and return it.

DLL code below

using System;
using SAPFEWSELib;
using SapROTWr;

namespace SAPCon
{
    public class SAPObj
    {
            public static GuiSession CreateNewSession()
        {
            SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();
            object SapGuilRot = sapROTWrapper.GetROTEntry("SAPGUI");
            object engine = SapGuilRot.GetType().InvokeMember("GetSCriptingEngine", System.Reflection.BindingFlags.InvokeMethod, null, SapGuilRot, null);
            GuiApplication sapGuiApp = engine as GuiApplication;
            GuiConnection connection = sapGuiApp.Connections.ElementAt(0) as GuiConnection;
            GuiSession session = connection.Children.ElementAt(0) as GuiSession;
            int c = (connection.Children as GuiComponentCollection).Count;
            session.CreateSession();

            int err = new int();
            int ses = new int();
            do
            {
                try
                {
                    session = connection.Children.ElementAt(c) as GuiSession;
                    ses = session.Info.SessionNumber;
                    err = 0;
                }
                catch (Exception e)
                {
                    err = e.HResult;
                }
            } while (err == 614);

            return session;
            }
    }
}

Here is the code from the program that's trying to test the dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using SAPFEWSELib;
using SapROTWr;
using SAPCon;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            GuiSession g;
            g = SAPCon.SAPObj.CreateNewSession();

            int s;
            s = g.Info.SessionNumber;
            Console.WriteLine(s);
            Console.ReadKey();
        }
    }
}

If I put the whole DLL code in a regular console application it works flawlessly, but when I reference the dll and try to use the function I get an exception on the first line, SapROTWr.CSapROTWrapper sapROTWrapper = new SapROTWr.CSapROTWrapper();, saying:

class is not registered

I figure this is something wrong with my DLL, not the SAP Dlls since they work ok when ran from an application. I did a regasm 32bit on my dll and it went ok, I tried a regsvr but it couldn't find the entry point.

What am I missing here?

Upvotes: 1

Views: 868

Answers (1)

Peuczynski
Peuczynski

Reputation: 4733

Few things you may want to check in such situation are:

  1. Check for the right architecture. There is very problem causing option in "runnable" types of projects "Prefer 32-bit" so you may be thinking you are running AnyCPU but in reality you force x86 architecture
  2. Check if applying [STAThreadAttribute] to the method in question solves the problem
  3. Try building both project to the same directory - if it solves the problem it means that you're missing some dependencies' files in the referencing project

Upvotes: 2

Related Questions