Unknown name ComException on a referenced dll in a C# project - c#

I'm writting a C# application which use one of the dll written in C++ as reference. I can use that dll namespace and my project compiles fine. However when I run it I keep getting error in one of the line where I assign a property its value. The exception error I got was the following:
A first chance exception of type 'System.Runtime.InteropServices.COMException' occurred in mscorlib.dll
2013-06-03 12:26:32 - Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))
2013-06-03 12:26:32 - at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
2013-06-03 12:26:32 - at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
2013-06-03 12:26:32 - at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
2013-06-03 12:26:32 - at sstObjTapLib._DsstObjTap.set_CapCode(String )
2013-06-03 12:26:32 - at hpOutput.CTapPagerCom.MessageLoop() in c:\shs\Arial 8.1\XmarkClient\hpOutput\CTapPagerCom.cs:line 225
I initiated the dll object using the following code:
m_ctlTap = new sstObjTapLib.ctlTap();
Below are the three screenshots:
The properties of the DLL I use
The object browser of the dll I use
A breakpoint that shows where the exception occurs.
Looking at the stack error above, it seems that .NET is trying to call some method that does not exist, but I'm just setting a property value. Can somebody point me in the right direction or what I might have missed?
A noted point: While in debugging, I'm looking at my loaded module view, however I don't see this dll name one in the list of my loaded module.

I'm not sure why but the computer I worked with had this same dll installed. I know this by viewing it using the third party tool called RegDllView .
So I went in, unregister the dll I was using, verify it is also gone from the registry and then re-register it again. Clean out my project and recompile everything, and got the exact code to work.
Looking at what I did I think for some reason the application did not recognize the GUID supplied to point to the proper COM.

Related

What is causing visual studio errors looking for defaultbinder.cs?

Just reinstalled visual studio 2019 as I was getting this error: https://github.com/dotnet/roslyn/issues/18814
I have never seen it before. After reinstalling, I no longer see it but I get something else. "Source not found" looking for defaultbinder.cs when running my WPF application from the debugger. Running it outside of the debugger works fine.
I know I have seen this before but I couldn't find any info on the web. -
$exception {"Object reference not set to an instance of an
object."} System.NullReferenceException
at System.DefaultBinder.BindToMethod(BindingFlags bindingAttr, MethodBase[] match, Object[]& args, ParameterModifier[] modifiers, CultureInfo cultureInfo, String[] names, Object& state) in f:\dd\ndp\clr\src\BCL\system\defaultbinder.cs:line 66
at MS.Internal.Xaml.Runtime.DynamicMethodRuntime.CreateInstanceWithCtor(Type type, Object[] args)
at MS.Internal.Xaml.Runtime.ClrObjectRuntime.CreateInstance(XamlType xamlType, Object[] args)
As Kyle mentioned, just my code should be enabled. This allowed me to start the application. Hope this helps someone else or if no one else, me when I come looking for it a year from now.

Using reflection to instantiate a class from an external assembly

I am currently trying to develop a method of running test classes in external projects programmatically using reflection. Here is a simplified chunk of code that should showcase my problem.
string pathToDLL = #"C:\Path\To\Test\Project\UnitTests.dll";
IEnumerable<Type> testClasses = assembly.GetExportedTypes();
Type testClass = testClasses.First();
object testClassInstance = assembly.CreateInstance(testClass.FullName);
This code throws the following exception:
'assembly.CreateInstance(testClass.FullName)' threw an exception of type 'System.Reflection.TargetInvocationException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2146232828
HelpLink: null
InnerException: {System.IO.FileNotFoundException: Could not load file or assembly 'Project.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'. The system cannot find the file specified.
File name: 'Project.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null'
at Project.UnitTests.TestClass..ctor()}
Message: "Exception has been thrown by the target of an invocation."
Source: "System.Private.CoreLib"
StackTrace: " at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)\r\n at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)\r\n at System.Activator.CreateInstance(Type type, Boolean nonPublic)\r\n at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)\r\n at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)\r\n at System.Reflection.Assembly.CreateInstance(String typeName, Boolean ignoreCase, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)\r\n at System.Reflection.Assembly.CreateInstance(String typeName)"
In the stack trace it states that it "Could not load file or assembly 'Project.Core...'".
This project is one that the target DLL references directly (one that it tests). Does anyone know why this won't be able to pick up these DLLs automatically?
I've investigated ways of solving this problem:
It could be the way that the dlls have been compiled - this can be changed as I am in control of this - which is currently by running dotnet build */*/project.json at solution level. This successfully compiles everything, and all of the relevant DLLs seem to be populated in the bin folder. I've also investigated whether or not changing to dotnet publish or dotnet build */*/project.json --configuration Release though neither seem to have helped.
I've also looked into using different methods of compilation like Activator.CreateInstance again no dice.
I don't seem to see a way to load multiple DLLs into the same Assembly class so that I can control the references. Since AppDomains have been removed from .NET Core this doesn't look like it is possible, though I may be mistaken/looking in the wrong area.
If what I'm doing doesn't seem like it will be possible, does anyone know if this kind of functionality can be achieved using a different method? I.e. Roslyn?
I just thought that I would update this question with the solution that I managed to find, just in case someone else was having the same problem as I was. Though I would like to thank #Emrah Süngü for pointing me in the right direction.
Emrah drew my attention to the fact that I needed to import the dependencies of the DLL that I wanted to load in order to invoke the classes stored within it. One way to do this is to extend your app.config in order to import those dependencies - however I wanted to do this at runtime (with projects that I didn't know I was going to run prior starting the program) so I needed to look for another solution.
If you aren't using .NET Core this is relatively simple since AppDomains can be used to load all of the dependencies and execute your code. However, since this has been removed from .NET Core I needed to find another solution that would be compatible.
I toyed with the idea of running a separate process (or Powershell), and changing the working directory so that the process was running in the directory that stored all of the dependencies it needed. However, I couldn't find a way of doing this that allowed me to react to the outcome of running the methods.
Later I investigated manipulating the AssemblyLoadContext class, but (at the time of writing) there is little to no documentation on how this class. I did find this answer which was able to helped significantly... https://stackoverflow.com/a/37896162/6012159
In order for it to work I did have to make a slight change, instead of creating a new AssemblyLoader every time (which would cause exceptions to be thrown when trying to invoke methods within the Assembly), I reused the AssemblyLoader each time (Which removed this problem).
public class AssemblyLoader : AssemblyLoadContext
{
private string folderPath;
public AssemblyLoader(string folderPath)
{
this.folderPath = folderPath;
}
protected override Assembly Load(AssemblyName assemblyName)
{
var deps = DependencyContext.Default;
var res = deps.CompileLibraries.Where(d => d.Name.Contains(assemblyName.Name)).ToList();
if (res.Count > 0)
{
return Assembly.Load(new AssemblyName(res.First().Name));
}
else
{
var apiApplicationFileInfo = new FileInfo($"{folderPath}{Path.DirectorySeparatorChar}{assemblyName.Name}.dll");
if (File.Exists(apiApplicationFileInfo.FullName))
{
return this.LoadFromAssemblyPath(apiApplicationFileInfo.FullName);
}
}
return Assembly.Load(assemblyName);
}
}
Which can be use to load assemblies like this:
string directory = #"C:\Path\To\Project\bin\Debug\netcoreapp1.0\publish\";
string pathToDLL = #"C:\Path\To\Project\bin\Debug\netcoreapp1.0\publish\project.dll";
AssemblyLoader al = new AssemblyLoader(directory);
Assembly assembly = al.LoadFromAssemblyPath(pathToDLL);
I am assuming that "UnitTests.dll" depends on (references) other dll(s) and your program does not know where to look for those referenced dll(s). You should (in fact have to) tell it to where to look for those dll(s) as well. By default is the same directory as your EXE. You can use app.config for telling where else to look. For Load() to succeed dependant dll(s) must be stored in your app's probing path.
That is the reason why you are getting an error.
Here you can find related article.
https://msdn.microsoft.com/en-us/library/823z9h8w.aspx

Exception has been thrown by the target of an invocation - An attempt was made to load a program with an incorrect format. DbGeography [duplicate]

This question already has an answer here:
EF 6 and spatial type DbGeography on Windows Azure
(1 answer)
Closed 6 years ago.
Before I say anything - yes, I've read everything on this question so far, about building in X86/X64 modes and so on. I recently changed PC and have set up my environment again. The code is from a reporting service within a silverlight application. The silverlight application all builds fine and it has been publish ok as well. It just seems there are a few small errors somewhere within the reporting section that don't seem to want to play nice.
double Latitude = (double) parameters["GeofenceLatitude"];
double Longitude = (double) parameters["GeofenceLongitude"];
double Radius = (double) parameters["GeofenceRadius"];
DbGeography Geofence = GetPointFromLatLong(Latitude, Longitude);
Geofence = Geofence.Buffer(Radius);
So everything works fine, the GetPointFromLatLong method (a custom one) returns a correct DbGeography object, however the .Buffer(Radius) causes an exception detailed below.
InnerException = {"An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)"}
Message = "Exception has been thrown by the target of an invocation."
Source = "mscorlib"
StackTrace = "at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) at System.Data.Entity.SqlServer.SqlSpatialServices.Buffer(DbGeography geographyValue, Double distance) at System.Data.Entity.Spatial.DbGeography.Buffer(Nullable distance) at GpsSilverlight.Web.Reports.ReportService.OnLoadData(IDictionary`2 parameters, String reportName, ReportSlot reportSlot) in PROJECT:line 1514"
I cannot figure out what's going on here. Does anyone have any experience with this?
static DbGeography GetPointFromLatLong(double Latitude, double Longitude)
{
DbGeography geogTest;
geogTest = DbGeography.PointFromText(string.Format("POINT({1} {0})", Latitude.ToString(), Longitude.ToString()), 4326);
return geogTest;
}
The most common problem when this exception is thrown is that the CLR is running in 32/64 bit mode and is attempting to load an assembly compiled for 64/32 bit mode.
Sometimes solution, project and configuration settings can be a bit confusing and not always producing binaries the way you think.
The best way to verify what the compiler REALLY has done to build your assemblies (or ones you rely on) is to disassemble them and look at the assembly manifest information of all your binaries. Often one assembly is not built in the configuration you think it should.
This type of problem still bugs me sometimes and disassembling usually helps.

How can I specify a [DllImport] path at runtime (embedded inside .msi file)

I want to import a dll into my C# project to call it's functions using DllImport.
I have my dll that needs to be imported as part of my .msi file.
It does work when I specify the full path to the DLL, but that is outside .msi file.
I am facing the dllNotFoundException problem.
<Binary Id="CustomAction2.CA.dll"
src="../artifacts/CustomAction2.CA.dll" />
<CustomAction Id="Install"
Execute="deferred"
BinaryKey="CustomAction2.CA.dll"
DllEntry="CustomAction1" />
<CustomAction Id="InstallWithProperty"
Property="Install"
Value="location=[DEFAULT_INSTALLDIR]$FULL_NAME;name=myDll.dll"
Execute="immediate"/>
<InstallExecuteSequence>
<Custom Action="InstallWithProperty" After="InstallFiles"/>
<Custom Action="Install" After="InstallWithProperty" />
</InstallExecuteSequence>
when Custom action is invoked it says
I get below exception
Exception thrown by custom action: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.DllNotFoundException: Unable to load DLL 'myDll.dll': The specified module could not be found. (Exception from HRESULT: 0x8007007E) at CustomAction2.CustomActions.ConfigDriver(IntPtr hwndParent, UInt16 fRequest, String lpszDriver, String lpszArgs, String lpszMsg, UInt16 cbMsgMax, Int64& pcbMsgOut) at CustomAction2.CustomActions.CustomAction1(Session session) --- End of inner exception stack trace --- at System.RuntimeMethodHandle._InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object arguments, SignatureStruct& sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.RuntimeMethodHandle.InvokeMethodFast(IRuntimeMethodInfo method, Object target, Object arguments, Signature sig, MethodAttributes methodAttributes, RuntimeType typeOwner) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture, Boolean skipVisibilityChecks) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object parameters, CultureInfo culture) at Microsoft.Deployment.WindowsInstaller.CustomActionProxy.InvokeCustomAction(Int32 sessionHandle, String entryPoint, IntPtr remotingDelegatePtr) CustomAction Install returned actual error code 1603 (note this may not be 100% accurate if translation happened inside sandbox)
Can somebody help. I want to use myDll.dll for further installation, which is part of .msi file.
If I understand you correctly, you have MSI file which has .DLL inside it and you want to pInvoke it?
Whatever you're doing this for, it sounds bad. You might rehink your approach,
however, for an actual answer:
To get you started, here is a little bit of C# code:
String inputFile = #"C:\\Install1.msi";
// Get the type of the Windows Installer object
Type installerType = Type.GetTypeFromProgID("WindowsInstaller.Installer");
// Create the Windows Installer object
WindowsInstaller.Installer installer = (WindowsInstaller.Installer)Activator.CreateInstance(installerType);
// Open the MSI database in the input file
Database database = installer.OpenDatabase(inputFile, MsiOpenDatabaseMode.msiOpenDatabaseModeReadOnly);
// Open a view on the Property table for the version property
View view = database.OpenView("SELECT * FROM Property WHERE Property = 'ProductVersion'");
// Execute the view query
view.Execute(null);
// Get the record from the view
Record record = view.Fetch();
// Get the version from the data
string version = record.get_StringData(2);
Now mind you, it does not extract file from the MSI, but it's good start. In order to actually extract from MSI, you need to change the code a little.
Here is VB code that does it, note the SQL-syntax like query.
Function ExtractIcon(IconName, OutputFile)
Const msiReadStreamAnsi = 2
Dim oDatabase
Set oDatabase = Session.Database
Dim View
Set View = oDatabase.OpenView("SELECT * FROM Icon WHERE
Name = '" & IconName & "'")
View.Execute
Dim Record
Set Record = View.Fetch
Dim BinaryData
BinaryData = Record.ReadStream(2, Record.DataSize(2),
msiReadStreamAnsi)
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Dim Stream
Set Stream = FSO.CreateTextFile(OutputFile, True)
Stream.Write BinaryData
Stream.Close
Set FSO = Nothing
End Function
You need to convert it into C#, it's going to be easy task.
After that, you need to actually use dynamic pInvoke. Ps you can also use normal pInvoke if you extract the DLL into your projects folder, and use relative paths.
To use dynamic pInvoke:
follow this article; http://blogs.msdn.com/b/jmstall/archive/2007/01/06/typesafe-getprocaddress.aspx
It's little neat code:
using(UnmanagedLibrary lib = new UnmanagedLibrary("kernel32") // becomes call to LoadLibrary
{
Action<String> function = lib.GetUnmanagedFunction<Action<String>>("DeleteFile"); // GetProcAddress
function(#"c:\tmp.txt");
} // implict call to lib.Dispose, which calls FreeLibrary.
If you feel like a god, you can load the DLL into memory never extracting it anywhere. This is useful if you have amazing DLL that should be always hidden from anyone. It's just for information - it's pretty hard ^_^

Excel problem with deleting CustomProperty in Korean

On a Korean OS I'm getting the following exception when trying to call the Delete() method on the CustomProperty object. I'm doing this in C# using the Office2003 PIA for Excel.
This HRESULT isn't that helpful to me, can anyone help figure out what's going on here?
"Exception from HRESULT: 0x800A03EC"} System.Exception {System.Runtime.InteropServices.COMException}
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at Microsoft.Office.Interop.Excel.CustomProperty.Delete()

Categories