How to use .dll 32bit run on Excel 64bit VSTO C# - c#

I have 2 projects in a solution :One is Console Project where communicate with PLC and another is Excel add-in project.
My Problem: using .dll (x32bit) and run on Excel 64bit. Control button by Ribbon button to connect or disconnect to PLC. So, I added Excel add-in project in Console Project's references to use Excel add-in public class. But, i cannot call constructor of VSTO Addin
public ThisAddIn(global::Microsoft.Office.Tools.Word.ApplicationFactory factory, global::System.IServiceProvider serviceProvider) :
base(factory, serviceProvider, "AddIn", "ThisAddIn") {
Globals.Factory = factory;
}
Here my code in Console:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using Microsoft.Office.Tools.Ribbon;
using PLC2EXCEL_Addin;
using DATABUILDERAXLibLB;
using Microsoft.Office.Tools.Ribbon;
using System.Runtime.InteropServices;
using Microsoft.Office.Core;
using Microsoft.Office.Interop.Excel;
namespace PLC2EXCEL
{
class Program
{
public static DBCommManager dBCommManager = new DBCommManager();
static void Main(string[] args)
{
dBCommManager.PLC=DBPlcId.DBPLC_KV3000;
dBCommManager.Peer = "USB";
//Microsoft.Office.Tools.Excel.ApplicationFactory _fatory ;
//_fatory.GetRibbonFactory();
//ThisAddIn thisAddIn = new ThisAddIn(Microsoft.Office.Tools.Excel.ApplicationFactory,IServerProvider); //i am not sure about this
PLC2EXCEL_Ribbon pLC2EXCEL_Ribbon = new PLC2EXCEL_Ribbon();
if (pLC2EXCEL_Ribbon != null)
pLC2EXCEL_Ribbon.connectPLC_btn.Click += new RibbonControlEventHandler(startPLC2Excel);
dBCommManager.Connect();
Console.WriteLine("Connect PLC OK");
Console.ReadLine();
}
public event EventHandler startPLC2Excel1;
public static void startPLC2Excel(object sender, RibbonControlEventArgs eventArgs)
{
//DATABUILDERAXLibLB.DBCommManager dBCommManager = new DBCommManager();
//dBCommManager.PLC = DBPlcId.DBPLC_KV3000;
//dBCommManager.Peer = "USB";
dBCommManager.Connect();
Console.WriteLine("Connect PLC OK");
Console.ReadLine();
}
}
}
And code in Ribbon of Excel Addin:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Tools.Ribbon;
namespace PLC2EXCEL_Addin
{
public partial class PLC2EXCEL_Ribbon
{
private void PLC2EXCEL_Ribbon_Load(object sender, RibbonUIEventArgs e)
{
}
[ComVisible(true)]
public void connectPLC_btn_Click(object sender, RibbonControlEventArgs e) //How to catch this event from Console project
{
//Catch event
MessageBox.Show("Connect to PLC");
}
public void abc() { }
}
}
Sorry any inconvenience, this is my first post.
Thank in advance.

Impossible to use .dll 32bit on 64bit Excel,So I have solved my problem by create 2 separate projects:
One is C# app adding .dll 32bit and communicate to PLC and send data
to opened excel file.
Anther is Excel Addin with ribbon buttons,
launch/stop executive C# app when click button

Related

I'm using Gmap.net to show google maps on winforms but getting error 400 bad request. is there a way to fix it or either is there any alternative?

I'm trying to display a google map over winforms.
In my project I did : Tools > NuGET Package Manager > Manage NuGET packages for Solution... > Browse
type to search gmap and installed the top one version 2.1.7
then in form1 :
private void btnShowOnGoogleMaps_Click(object sender, EventArgs e)
{
GoogleMaps googleMaps = new GoogleMaps();
googleMaps.Show();
}
and in the GoogleMaps form :
using GMap.NET;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Weather
{
public partial class GoogleMaps : Form
{
public GoogleMaps()
{
InitializeComponent();
}
private void GoogleMaps_Load(object sender, EventArgs e)
{
gmap.MapProvider = GMap.NET.MapProviders.BingMapProvider.Instance;
GMap.NET.GMaps.Instance.Mode = GMap.NET.AccessMode.ServerOnly;
gmap.SetPositionByKeywords("Paris, France");
}
}
}
then when running the application and clicking the button to show the google maps i'm getting this :

C# Running program at startup

Hello I am working on c# simple project.
This is the code:
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using Microsoft.Win32;
using System.IO;
private void SetStartup()
{
RegistryKey rk = Registry.CurrentUser.OpenSubKey
("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", true);
if (chkStartUp.Checked)
rk.SetValue(AppName, Application.ExecutablePath);
else
rk.DeleteValue(AppName,false);
}
namespace web1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// open browser
Thread.Sleep(6000);
System.Diagnostics.Process.Start("https://google.com/");
}
}
}
It shows errors "The name "AppName" does not exist in the current context" and "The namespace cannot contain members such as fields or methods directly"
I want to:
If I click on this program .exe, it should run on the startup (every startup).
How to deal with it? I've tried so many ideas, but doesn't helped.
Thanks in advance.
BTW: it's my first time here, nice to meet you all!

C# UI Test being ignored and not running?

So I wrote a test and am trying to run it but when I right click it and hit, "Run Selected Test" the green loading bar moves up for less than a second and just stops. Fairly new to this so excuse my ignorance.
Test being ignored & not running.
This is the stack trace I get from the test output when I try to run it.
This is my session class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Remote;
public class CCSession
{
// Note: append /wd/hub to the URL if you're directing the test at Appium
protected const string WindowsApplicationDriverUrl = "http://127.0.0.1:4723";
protected static WindowsDriver<WindowsElement> session;
private const string ExcelAppId = #"C:\Program Files\Microsoft Office\root\Office16\EXCEL.exe"; // test
public static void Setup(TestContext context)
{
// Launch Excel application if it is not yet launched
if (session == null)
{
// Create a new session to launch Excel application
DesiredCapabilities appCapabilities = new DesiredCapabilities();
appCapabilities.SetCapability("app", ExcelAppId);
appCapabilities.SetCapability("deviceName", "WindowsPC");
session = new WindowsDriver<WindowsElement>(new Uri(WindowsApplicationDriverUrl), appCapabilities);
Assert.IsNotNull(session);
session.FindElementByName("Blank workbook").Click();
// Verify that Excel is started with untitled new file
Assert.AreEqual("Book1 - Excel", session.Title);
// Set implicit timeout to 1.5 seconds to make element search to retry every 500 ms for at most three times
session.Manage().Timeouts().ImplicitWait = TimeSpan.FromSeconds(1.5);
}
}
public static void TearDown()
{
// Close the application and delete the session
if (session != null)
{
session.Close();
try
{
// Dismiss Save dialog if it is blocking the exit
session.FindElementByName("Don't Save").Click();
}
catch { }
session.Quit();
session = null;
}
}
}
Test class:
using System;
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Api.Dto;
using CEL.CostCalculator.Api.CalculationResults;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA;
using OpenQA.Selenium.Appium.Windows;
using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Remote;
using Tests.Fixtures.DataPorts;
[TestClass]
public class CCExcelAddInTests : CCSession
{
[TestMethod]
public void EditorEnterText()
{
Assert.AreEqual("Book1 - Excel", session.Title);
}
[ClassInitialize]
public static void ClassInitialize(TestContext context)
{
Setup(context);
}
[ClassCleanup]
public static void ClassCleanup()
{
TearDown();
}
}
Any help's appreciated. :)
My test projects .csproj file was corrupted. Corrected and tests are visibly running again.

Why waitforexit dont work for all exe with C#?

Why some of the applications work with waitforexit but some doesn't work like notepad works fine bit dcomcnfg.exe doesn't what is the reason
Has edited the app.manifest to run this application with admin right
Application is debuged for any cpu
My code is:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace prchk
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Method();
}
public void Method()
{
Process prcs = new Process();
prcs.StartInfo.FileName = #"C:\Windows\System32\dcomcnfg.exe";
//p.StartInfo.Arguments = argu;
prcs.StartInfo.UseShellExecute = true;
prcs.Start();
prcs.WaitForExit();
MessageBox.Show("Exited");
}
}
}
Here exited is showing before the application is exited
What i tried:
1> Has tried hasexited
2> Has tried useshellexecution = false
Is there any way to wait for the application to exit for this kind of applications?

How to start a process/application on windows connection change event using C#

I can detect the network connection change event while running a C# code, how would I register an exe when Windows detects this event. What all details would I need. Below is how I am using this :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net.NetworkInformation;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
testing t = new testing();
Console.Read();
}
}
public class testing{
public testing()
{
NetworkChange.NetworkAvailabilityChanged += new NetworkAvailabilityChangedEventHandler(NetworkChange_NetworkAvailabilityChanged);
}
void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
{
if (e.IsAvailable)
{
Console.WriteLine("network is available");
}
}
}
}
What you could maybe do is in your method that is triggered to start a new process and execute your exe

Categories