1

It works well on the c# code. However, an exception handling error occurs if i execute at c++ with c# dll.

First, I'll show you my code.

  1. C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;



namespace ManagedLibrary1
{
    public interface LoginClass
    {
        void LogIn(string id, string pw, int t, int num);
        void LogOut();
    }


    public class Class1 : LoginClass
    {

        IWebDriver driver = new ChromeDriver();
        IWebElement a;
        Boolean b;

        public void LogIn(string id, string pw, int t, int num)
        {


            if (num == 0)  
            {
                driver.Navigate().GoToUrl("this is url address"); 
            }

            else
            {
                driver.Url = "this is url address2";   
            }

            Thread.Sleep(50);


            if (t == 0)    
            {
                a = driver.FindElement(By.XPath("this is Xpath")); 
                b = a.Displayed;

                if (b == true)
                {
                    a.Click();
                }
            }

            driver.FindElement(By.XPath("this is xpath2")).SendKeys(id);  
            Thread.Sleep(50);
            driver.FindElement(By.XPath("this is xpath3"]")).SendKeys(pw);          
            Thread.Sleep(50);
            driver.FindElement(By.XPath("this is xpath4")).Click();   
            Thread.Sleep(5000);
            a = driver.FindElement(By.XPath("this is xpath5"));   


            if (b == true)  
            {


                driver.Url = "this is url address 3";

            }



            driver.Manage().Window.Maximize();  
            driver.FindElement(By.ClassName("this is classname")).Click(); 

        }

        public void LogOut()
        {



            a = driver.FindElement(By.XPath("this is xpath6"));  

            b = a.Displayed;

            if (b == true)   
            {
                a.Click();  

            }
        }

    }

}


  1. C++ code
#include <windows.h>
#include "tchar.h"

#import "C:\Users\me\Desktop\Project Folder\ClassLibrary4\ClassLibrary4\bin\Debug\ClassLibrary4.tlb" raw_interface_only

using namespace ClassLibrary4;

int main()
{
    HRESULT hr = CoInitialize(NULL);

    LoginClassPtr pICalc(__uuidof(Class1));



    pICalc->LogIn("id","password",1,0);




    CoUninitialize();

    return 0;
}

when debugging C++ code, error generate here

LoginClassPtr pICalc(__uuidof(Class1));

If the process of receiving the function of dll was wrong, I tried to change the function part of C# into very simple function that calculates the sum of two integers, and it worked well without errors.

Only this one problem has kept me from going forward for two days. I want your help.

MX-4
  • 65
  • 10
  • Did you do a clean build? The code where it is failing is calling the constructor for Class1. So one of the following is failing : IWebDriver driver = new ChromeDriver(); or IWebElement a; – jdweng Feb 15 '20 at 20:58
  • Where is the OpenQA dll located? It must be in the same folder as the c# dll. – jdweng Feb 15 '20 at 20:59
  • @jdweng I downloaded selenium.chome.webdriver and selenium.support and selenium.webdriver from Nuget before building C# code. maybe OPENQA.dll is located in this file. – MX-4 Feb 16 '20 at 01:00
  • @jdweng you mean that because IWebDriver driver = new ChromeDriver(); or IWebElement a; this codes are not located in contructor of Class1? if this codes are located in constructor, driver and a code below have error. I want to keep this values in executing c++ program. and driver value sholud be maintained. where these values are located in for my purpose? – MX-4 Feb 16 '20 at 01:12
  • @jdweng I executed code after revise like this. IWebDriver driver; IWebElement a; public Class1() { driver = new ChromeDriver(); } but still return error. – MX-4 Feb 16 '20 at 01:34
  • @DebanjanB No... – MX-4 Feb 16 '20 at 01:38
  • When you step through a constructor you will see the global variables in the class are initialized before the constructor method is executed which makes a lot of sense. Most people do not realize this occurs.To allow for errors you would need to initialize these values in the constructor and put the code in an exception handler so you would continue when an exception occurs.First do a clear build and try again to make sure everything is recompiled.If still fails go into the bin folder and check timestamp of OpenQa dll.I suspect the OpenQa needs to be recompiled.What exception are you getting? – jdweng Feb 16 '20 at 08:32
  • When you use smart-pointer classes that are auto-generated by the #import statement (XxxxPtr types) then any runtime error is automatically converted to a C++ exception. If you don't use try/catch to catch the `_com_error` then such an error gets very hard to diagnose. https://stackoverflow.com/questions/19165849/how-do-i-catch-and-handle-com-error – Hans Passant Feb 16 '20 at 23:07

1 Answers1

0

I was working on similar issue, my c# dll was signed dll with a strong name. The selenium webdriver nuget package i was using was not having a strong name. Apparently strong named dll cannot load another dll which is not having strong name. I uninstalled selenium webdriver package and installed selenium webdriver.strongname ,this worked for me.

sumamb
  • 1
  • 1