Resolve .net tlb reference - c#

Right now I am writing a .net dll which should be useable within VBA code (Excel, Access etc.). The following setup is working fine:
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[ComVisible(true)]
[Guid("8079e4a4-1e4b-4788-92ba-9d5b017fa9be")] //Allocate your own GUID
public interface ICommunication
{
string TestProp { get; set; }
string Login();
string Disconnect();
}
[ClassInterface(ClassInterfaceType.None)]
[ComVisible(true)]
[Guid("19fd86e2-f1b9-478c-ba7a-bd76bdf19b85")] //Allocate your own GUID
[ProgId("TestDll.Communication")]
public class Communication : ICommunication
{
public string TestProp { get; set; }
public string Login()
{
// use of BouncyCastle here
return "logged in";
}
public string Disconnect()
{
// ...
return "disconnected";
}
}
By referencing the generated tlb file I can properly use the Property aswell Disconnect function however calling the Login function leads to problems ("File not found" messagebox in Excel) which I guess are related to the usage of referenced BouncyCastle.
Sub Button1_Click()
Dim o: Set o = CreateObject("TestDll.Communication")
Dim value As String
value = o.Login()
MsgBox value
End Sub
What is the best way to deal with references to other .net assemblies inside com visible libraries? So far I tried registering bouncycastle to the GAC with no success.
Thanks :)

It was indeed like suggested above a file which could not be read:
On each build I place a .txt in the bin directory of the project which is read runtime.
Using the dll within my solution the file could be found since the relative path was my bin directory however using the tlb the relative root path was the document folder of the signed in windows user.
Funny how I thought the whole time that the error was related to the way I setup my dll as com visible :).

Related

ReadSoft Invoices could not create instance of class name

I am currently trying to develop a custom ReadSoft Invoices plugin, nothing special.
I have gotten the plugin to work on two different machines, but the third machine I have, simply refuses to load the plugin.
The "funny" part about this, is that the third machine is a complete replica of the other two (they are all virtual machines).
The error shown when launching manager is: Plug-ins could not be loaded. (TESTPLUGIN(ClassName=TestPlugin.Test.Connect)).
I have run Regasm.exe on the dll so all the types are registered.
I have updated eilocal.ini with this:
;***********************
;* Manager *
;***********************
[Plugins::eimngr]
Plugin1=Common
Plugin2=DBMaintenance
Plugin3=ERPImport
Plugin4=InvoiceSelection
Plugin5=AutoValue
Plugin6=COLLECTOR
Plugin7=ReadSoft.InvoiceRules
Plugin8=TestPlugin
[eimngr::TestPlugin]
Name=TestPlugin
Type=COM
SupportIDispatch=TRUE
Classname=TestPlugin.Test.Connect
I have stripped all code from the plugin, just to make sure.
This is the code I am trying to run:
namespace TestPlugin
{
[ClassInterface(ClassInterfaceType.AutoDispatch)]
[ProgId("TestPlugin.Test.Connect")]
public class TestConnect
{
public TestConnect()
{
}
public int Connect(object objEHIApp, string sIniFile, string sIniSection)
{
File.AppendAllText(#"C:\ProgramData\ScanSolutions\TestPlugin.txt", "Plugin Connected!");
return 0;
}
public int Disconnect(object objEHIApp)
{
return 0;
}
public int IsVistaConverted()
{
return 0;
}
}
}
Since the error message from manager really does not help me track down the problem, I have been looking for a log, but have not yet found one.
To debug my issue I have then attached a debugger to the manager module, which just tells me this, when trying to load my plugin:
Could not create instance of class name TestPlugin.Test.Connect with
clsid {B21CE504-ADDA-3174-BEB8-D359F85D9A63}.
Does anyone what is happening here?
Thanks in advance.

Could not load file or assembly in Solidworks addin

I'm attempting to create a Solidworks plugin with AngelSix's SolidDna library.
I've used the standard setup as per the examples:
public class Integration : AddInIntegration
{
// All overrides left blank
public override void ApplicationStartup()
{
}
public override void ConfigureServices(FrameworkConstruction construction)
{
}
public override void PreConnectToSolidWorks()
{
}
public override void PreLoadPlugIns()
{
}
}
public class MySolidDnaPlugin : SolidPlugIn
{
public override string AddInTitle { get; } = "foo";
public override string AddInDescription { get; } = "bar";
public override void ConnectedToSolidWorks()
{
// This works fine...
Boo.Lang.List l = new Boo.Lang.List();
// ....But this doesn't :(
Boo.Lang.Compiler.BooCompiler c = new Boo.Lang.Compiler.BooCompiler();
}
public override void DisconnectedFromSolidWorks()
{
}
}
I'm attempting to add support for scripting in Boo, so users can edit the scripts on the fly. I've added a reference to Boo.Lang.dll, Boo.Lang.Compiler.dll and Boo.Lang.Parser.dll, and all 3 DLLs are definitely copied to the /bin folder.
I'm able to use classes from the Boo.Lang namespace, but as soon as I try and use the Boo.Lang.Compiler namespace it throws an error:
Could not load file or assembly 'Boo.Lang, Version=2.0.9.4, Culture=neutral, PublicKeyToken-32c39770e9a21a67' or one of its dependencies. The system cannot file the file specified.
The same code/setup works fine when running outside Solidworks, e.g. in a console app.
The SolidDna docs show the process of registering the plugin DLLs with regasm.exe /codebase, so I tried running that with the 3 Boo DLLs, but that doesn't make any difference.
Any suggestions or pointers?
My guess would be that you added those binaries to addin folder, while process is looking for those files in solidworks.exe folder.
I suggest you to confirm it with ProcMon tool.
If that is the case there are multiple ways to resolve this:
1 add those binaries to solidworks folder
2 add those folders to any of the folders in the path environment variable
3 add your addin folder to path environment variable
4 If those binaries as well as your addin are .net assemblies you can use ilmerge to combine them into one.
5 If your addin is .net assembly and you have access to appdomain object you can subscribe to AssemblyResolve event and provide path dynamically.

Error 429 activex component can't create object while using self create reference

I created some add-in for excel in C#. In it is one public class for using in VBA. On my machine all works ok. When I install add-in on tester computer (I'm using InstallShield 2015 Limited Edition for Visual Studio to create setup file) I can't set object.
C# code
using System;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
using Microsoft.Win32;
namespace PMTAddin
{
[Guid("B2350EC1-522E-4B75-BB02-86BB0FD1A60E")]
[ClassInterface(ClassInterfaceType.AutoDual)]
[ComVisible(true)]
public class PublicClass
{
public void test()
{
System.Windows.Forms.MessageBox.Show(
"test."
, "test"
, System.Windows.Forms.MessageBoxButtons.OK
, System.Windows.Forms.MessageBoxIcon.Error
);
}
private int GetWorksheetID(Excel.Workbook wb, string worksheetName)
{
int result = 0;
foreach (Excel.Worksheet ws in wb.Worksheets)
{
if (ws.Name == worksheetName)
{
result = ws.Index;
break;
}
}
return result;
}
[ComRegisterFunctionAttribute]
public static void RegisterFunction(Type type)
{
Registry.ClassesRoot.CreateSubKey(GetSubKeyName(type, "Programmable"));
RegistryKey key = Registry.ClassesRoot.OpenSubKey(GetSubKeyName(type, "InprocServer32"), true);
key.SetValue("", System.Environment.SystemDirectory + #"\mscoree.dll", RegistryValueKind.String);
}
[ComUnregisterFunctionAttribute]
public static void UnregisterFunction(Type type)
{
Registry.ClassesRoot.DeleteSubKey(GetSubKeyName(type, "Programmable"), false);
}
private static string GetSubKeyName(Type type, string subKeyName)
{
System.Text.StringBuilder s = new System.Text.StringBuilder();
s.Append(#"CLSID\{");
s.Append(type.GUID.ToString().ToUpper());
s.Append(#"}\");
s.Append(subKeyName);
return s.ToString();
}
}
}
In VBA project I checked reference to it on the list. It calls PMT.
VBA
Sub dsf()
Dim o As PMT.PublicClass
Set o = New PMT.PublicClass 'at this lane on other computer I got error 429. On my computer all work smoothly and method test is running.
o.test
End Sub
I thought that maybe it was something .NET Framework, but it is installed. Any ideas?
EDIT:
I create two diffrent version for bittnes, but the same error.
But I found some new info. In registry on my computer it looks like this
and on tester machine it looks like this
There are no CodeBase value... Do you think this is the problem? If it is, how I need to modify RegisterFunction method to correct this?
After long seeking I found the solution. It's kind of partial, because for 64 bit Excel we need to register library manually (maybe someone knows, how to add bat file to installation file).
I found the answer on this site.
While creating Install Shield instalation file we need to do two things.
Add .tlb file to application files (this step was done by me, before posting on stackoverflow)
Click right on project.Primary output file and choose properities like in screenshot (for *.tlb file we need to check the same, but without "COM Interop")
Without this the installer will not properlly register add-in in registry.
Install file created like this would register add-in for 32-bit excel only. If you want to use it also in 64-bit Excel you need to register library manually. I created simple bat file like this:
c:
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319
regasm.exe "[pathToDLL]\AddInName.dll" /tlb:"[pathToDLL]\AddInName.tlb" /codebase
pause
Remember, that you need to run it with admin rights.

C#: use "Class Library" Project for plugins

this question follows my previous question.
I have a c# ASP.NET application and i want to provide support for plugins. Plugins can be custom c# classes, javascript, html, css, images, etc.
I see no problem as long as my application is extended with c# classes because all the user has to do is create a new "class library" project in visual studio and implement the interfaces, i provide. Then build a dll out of it and upload it to my server. The plugin-developer can add static files (html, js, css, etc.) into this project as well but i found some problems with that:
Every static file i add to the plugin project gets the build action "content" and it seems i cannot read those files from my server. (see my previously answered question). I have to manually select "Embedded Resource" on each file, so it is packed with the plugin dll.
I want to support Typescript for the plugins. The Typescript compiler generates javascript files in the same directory as the typescript-files. But the javascript files are not included in the project and therefore i have to include these in the plugin project and then set the correct build action. I don't want the plugin developers to do that all the time.
If the static files have the build action "enbedded resources", then the server can pickup these files by using the assembly.GetManifestResourceNames() method. This method returns the resources as a string. The path is not separated by \ or / but with a dot instead. So i am not able to distinguish between file path (this is relevant) or filename (also relevant to pickup the correct files), because the original filename can also have dots.
So i am starting to question the "class library" project type is right for my needs. Is there a way to get around of my issues or do i have to use another project type?
Thank you for any help!
Edit: Changed the question a little bit so it is better to understand.
You could make a zip package with the plugin dll and files. NuGet also uses this mechanism. A .nupkg is also just a zip file.
I would start by looking at MEF (Managed Extensibility Framework).
MSDN information can be found here: https://msdn.microsoft.com/en-us/library/dd460648(v=vs.110).aspx
From that link you can get more information and I believe there is a tutorial as well.
Oh, for me it seems very simple.
Let the developer create the plugin freestyle and put all the additional files in a directory, let's call it extras
To implement the your interface they will need your assembly so I guess you will ship it via nuget, or just some link. No matter what the case, provide them with some powershell script what will be required to run before the final build
The script would create zip archive from the extras directory and add it to the ClassLibrary project as EmbeddedResource.
As you mentioned earlier, you can access EmbeddedResource. So all you would do is to unpack it and you would have the exact directory tree.
The best idea would be to provide project template with script included, and also the empty zip archive added as embedded resource (it will be easier to just pack the files in the script and replace the file), and pre-build action set to run the script.
Am I missing something?
What about this.
In your web application, you could add a function that loop into your plugin directory and find DLL implementing an Iplugin (name is up to you) interface.
The interface is defined in a class library that both your web application and plugins have to implement.
You can use the Httpcontext Server mappath to read javascript and other files.
Here is a very basic implementation
First, you have the plugin interface (a class library implemented both by the web application and the individual plugins) I implemented sample properties and methods...
using System.Web;
public interface IPlugin
{
string Name { get; set; }
string Output { get; set; }
void Load(ref httpcontext Context);
void Dispose();
void Display();
}
Next, you have the Actual plugin class library we want to implement.
using System.Web;
using IPlugins;
public class AwesomePlugin : IPlugins.IPlugin
{
private string _Name = "AwesomePlugin";
private HttpContext _Context;
public string Name {
get { return _Name; }
set { _Name = value; }
}
public string Output {
get { return "Yay !!!"; }
set {
throw new NotImplementedException();
}
}
public void Display()
{
throw new NotImplementedException();
}
public void Dispose()
{
throw new NotImplementedException();
}
public void Load(ref Web.HttpContext Context)
{
}
}
Finally, you dynamically load your plugins so you can use them in your application.
private Dictionary<string, IPlugins.IPlugin> _Plugins = new Dictionary<string, IPlugins.IPlugin>();
public void LoadPlugins()
{
lock (static_LoadPlugins_IpluginType_Init) {
try {
if (InitStaticVariableHelper(static_LoadPlugins_IpluginType_Init)) {
static_LoadPlugins_IpluginType = typeof(IPlugins.IPlugin);
}
} finally {
static_LoadPlugins_IpluginType_Init.State = 1;
}
}
string ServerPath = HttpContext.Current.Server.MapPath("~") + "Plugins";
dynamic Plugins = io.Directory.GetFiles(ServerPath);
foreach (string PluginPath in Plugins) {
dynamic Assembly = system.Reflection.Assembly.LoadFile(PluginPath);
Type PluginClass = Assembly.GetTypes.Where(T => T.GetInterface("IPlugin") != null).First;
IPlugins.IPlugin MyPlugin = Activator.CreateInstance(PluginClass);
MyPlugin.Load(httpcontext.Current);
_Plugins.#add(PluginClass.ToString, MyPlugin);
}
}
static bool InitStaticVariableHelper(Microsoft.VisualBasic.CompilerServices.StaticLocalInitFlag flag)
{
if (flag.State == 0) {
flag.State = 2;
return true;
} else if (flag.State == 2) {
throw new Microsoft.VisualBasic.CompilerServices.IncompleteInitialization();
} else {
return false;
}
}
That way, you can implement whatever you want in your plugin.
I believe you could load your plugins in a separate appdomain with restricted permissions to everything.
The files (Javascript / CSS / Html) should be available by accessing the full path of the file.
string ServerPath = HttpContext.Current.Server.MapPath("~") + "Plugins";
If the resources is embedded into the plugin DLL, you could read the stream from the loaded assembly or let the plugin manage its own embedded files.
For question Number 2, you can use
MS Build
to change the contenttype during build process.
You have to make yourself confident with MS Build

Warning “The type X in Y.cs conflicts with the imported type X in Z.dll”

The main.cs of my project returns the following warning:
Warning 1 The type 'Extensions.MessageDetails' in 'PATH\Extensions.cs' conflicts with the imported type 'Extensions.MessageDetails' in 'path\lib.dll'. Using the type defined in 'path\Extensions.cs'. path\main.cs
What is wrong with my project? How to get rid of the warning?
The code of my project has the following structure:
Extensions.cs
namespace Extensions
{
public class MessageDetails
{
public string message { get; set; }
public string link { get; set; }
public string picture { get; set; }
public string name { get; set; }
public string caption { get; set; }
public string description { get; set; }
public string userid { get; set; }
public string username { get; set; }
public object actions { get; set; }
public object privacy { get; set; }
public object targeting { get; set; }
}
}
lib.dll
namespace MyClassLib {
public class MyClassLibFoo {
public void foo(MessageDetails parameters) {
/* .. */
}
}
}
main.cs
using MyClassLib;
using Extensions;
class Program
{
static void Main(string[] args)
{
MessageDetails md = new MessageDetails();
}
}
In my case, with Visual Studio 2013, I found that one of my class libraries had developed a reference to itself. I think it happened when I added a new project to my solution or it was a bug, but either way it was causing this exact issue.
Check your project references for any circular references.
It seems like Extensions.cs is both part of the project that builds lib.dll and your main.exe
Remove it from one of the project to fix this issue.
I had this kind of issue where I had reverted from a target .NET Framework version of 4.5.2 to 4.0.
Classes in my App_Code folder had methods that called methods in other classes in that folder. When I created a standard folder I named "AppCode", and moved my classes into it, I no longer had the issue.
If I re-created the "App_Code" folder and move my classes back into it, I will have this issue again. I'm convinced it has to do with my .NET Framework version or that Visual Studio just doesn't deal well with changing it after being initially built/targeted to another version.
You can't have two copies of the extensions class, even though the code is the same they are not seen as the same object. Both your dll and main application will need to reference the exact same one.
You could try creating a 'Common Files' class library and add the extensions class to it, that way you will always be using the correct class
I had this problem with a project that is also hosted on NuGet. I checked all project references. Finally, the object browser revealed that the DLL of an older version of my NuGet package was somehow loaded in Visual Studio from the NuGet cache folder ("C:\Users\{username}\.nuget\packages"). I removed the package from the cache folder, it disappeared from the object browser and everything was working fine again.
I had a Shared Project, "Project A," which was included in both "Project B" and "Project C."
"Project A" was added as a Shared Project in "Project B" and "Project C."
"Project A" also included a traditional reference to "Project B."
To correct the problem, I removed the reference to "Project B" from "Project A."
If you really need to have both classes declared or referenced in two separate dll, you can mark your class as internal.
Internal types or members are accessible only within files in the same assembly, therefore it will prevent the collision.
After reading through many answers on SO the solution was still unclear. My situation was similar but the solution was found by:
Example project Name: My.Example.Project
Opening my project
Open the References dropdown
Finding My.Example.Project in the References section
Deleting the reference to My.Example.Project
That fixed it!
I had faced same problem. Just a simple solution for that.
Check your project references there must be same project reference. just remove that, it will work.
sometimes I get this error - it's a bug though in my case.. All I have to do to fix it is change the first letter of my script file name from upper case to lowercase in the file in Explorer /
(or in Unity Engine in my case)
and then change the name / class accordingly in my script. Idk why this happens.. just does - and Idk why this fix works .. but in my case it always does. - Otherwise you probably have 2 copies of the same script / same class name for 2 diff scripts. Hope this helps.
I fixed this error by deleting the .suo file in the directory sturcture vs directory. stop vs then delete restart vs. that worked for me.

Categories