当我使用 C++ 中的 C# dll 来使用 Selenium 时,存在异常处理问题

when i use C# dll from C++ for using Selenium, there is exception handling problem

本文关键字:存在 异常 Selenium 处理问题 dll C++ 中的      更新时间:2023-10-16

它在 c# 代码上运行良好。但是,如果我使用 c# dll 在 c++ 上执行,则会发生异常处理错误。

首先,我将向您展示我的代码。

  1. C# 代码
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++代码
#include <windows.h>
#include "tchar.h"
#import "C:UsersmeDesktopProject FolderClassLibrary4ClassLibrary4binDebugClassLibrary4.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;
}

调试C++代码时,在此处生成错误

LoginClassPtr pICalc(__uuidof(Class1));

如果接收dll函数的过程是错误的,我尝试将C#的函数部分更改为非常简单的函数,该函数计算两个整数的总和,并且运行良好,没有错误。

只有这一个问题让我两天没有前进。我需要你的帮助。

我正在处理类似的问题,我的 c# dll 是用强名称签名的 dll。我使用的Selenium Webdriver nuget包没有一个强名称。显然,强命名的 dll 无法加载另一个没有强名称的 dll。我卸载了硒网络驱动程序包并安装了硒网络驱动程序.strongname,这对我有用。