I used the MATLAB Compiler to generate a .NET Assembly with a very little MATLAB code:
function output_arg = extest( input_arg1,input_arg2 )
output_arg = input_arg1+input_arg2;
end
I generated the dll with the wizard.
Within my Visual Studio project I added the reference to the generated dll (extest.dll) and to the MATLAB Runtime dll (C:\Program Files\MATLAB\MATLAB Runtime\v92\toolbox\dotnetbuilder\bin\win64\v4.0\MWArray.dll) as mentioned in the "Assembly Description".
This is my c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MathWorks.MATLAB.NET.Utility;
using extest;
namespace DllTesting
{
class Program
{
static void Main(string[] args)
{
ExClass e1 = new ExClass();
}
}
}
It builds without errors an intelisense is working (so all references should be good from my understanding)
But when I launch it, the following exception gets thrown (on new ExClass()):
An unhandled exception of type 'System.TypeInitializationException' occurred in DllTesting.exe
Additional information: The type initializer for 'extest.ExClass' threw an exception.
Any suggestions what is wrong with this code or whats missing?
Try adding this before the class definition
[assembly: MathWorks.MATLAB.NET.Utility.MWMCROption("-nojit")]
Also make sure that the .NET version you use for assembly is the same or lower than the one used for your Visual Studio project.
Another solution might be adding the path of the MATLAB runtime (e.g. C:\Program Files\MATLAB\MATLAB Runtime\v92\runtime\win64) to the PATH Environment Variable.
If none of these helps, have a look here and here, you might have a 64/32 bit mismatch.
Related
I am building a Class Libray using the .NET Framework 4.8 in C# with Visual Studio 2019.
I want to call functions in my .NET Class Library from Access 2016 VBA.
When I try to call the function from VBA, I get the following error:
Run-time error '-2147024894 (800700002)':
Automation error
The system cannot find the file specified.
The Reference for the Custom Class Library has been added in VBA under Tools | References.
The C# assembly is built using the Copy Local option on each referenced assembly.
I tried a SFC /SCANNOW from an Administrator:Command Prompt. This reported that some DLLs were replaced. A follow up run of SFC /SCANNOW reported that no issues were found.
The referenced assemblies are all Microsoft .NET Framework 4.8 assemblies. There are no third-party DLLs involved.
There are no errors or warnings that occur during the Build.
The assembly is marked as COM Visible.
The assembly was successfully registered with regasm.
A have tried a few additional approaches. No success yet.
I restarted from scratch - and created a .NET Framework 4.72 DLL.
The Assembly Information has 'Make assembly COM-Visible'checked.
The Build Information has 'Register for COM Interop' checked.
I successfully added the references to the Tools | References in the VBA Editor.
Now I get the following error:
Run-time error 429 activex component can't create object.
The code for the class is below:
using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
namespace NWBLibrary
{
[ClassInterface(ClassInterfaceType.AutoDual)]
public class ReedDecibelLogger
{
/// <summary>
/// GetSha256FileHash computes a SHA256 hash of a file.
/// </summary>
public string GetSha256FileHash(string fileSpec)
{
string fileHash = string.Empty;
byte[] hashValue;
SHA256 managedSha256 = SHA256.Create();
FileStream fileStream;
using (fileStream = new FileStream(fileSpec, FileMode.Open))
{
hashValue = managedSha256.ComputeHash(fileStream);
fileStream.Close();
}
fileHash = Convert.ToBase64String(hashValue);
return fileHash;
}
}
}
The reference for the assembly above has been added to the Tools | References section of the VBA editor.
The VBA Code is:
' Returns the SHA256 near unique hash for the specified file.
Public Function filehash(ByVal fileSpec As String) As String
Dim loggerObj As New NWBLibrary.ReedDecibelLogger
filehash = loggerObj.GetSha256FileHash(fileSpec)
End Function
My questions are:
Where should .NET assemblies (both custom and references) be located so that VBA can find all required DLLs?
Do I need to modify search paths on my development boxes so that Access VBA can find the DLLs?
Is this an assembly the the Microsoft assemblies depend upon that are not included when Copy Local is used?
Should I use the Fusion Log Viewer (fuslogvw.exe) to see if a dependent assembly is missing?
Could someone recommend a reference or third-party book that discusses building libraries for use with VBA?
If this is a problem of the CLR not seeing all dependencies, how would you make the GAC and all dependencies visible to the CLR?
Are additional steps required so that VBA can see the assemblies and GAC?List item?
Do I need to create a DLL entry point for the C# Assembly?
Do I need to try a different version of Microsoft Access?
I can get this to run on one workstation but it fails with the 429 error on another workstation. This is probably a dependency issue.
I am using the Fusion Logger to see if a dependency is missing on the machine that throws the 429 error.
I have just started studying databases and now I'm trying to connect to a SQL database with Visual Studio. I made the main model. There are tables and stuff. Now I created a console application. I made reference to the project with the data (where the diagram with tables is). But when I try to compile the project I get this error
error CS1502: The best overloaded method match for
'System.Data.Entity.Core.Objects.ObjectContext.ExecuteFunction(string,
params System.Data.Entity.Core.Objects.ObjectParameter[])' has some
invalid arguments
and this error
error CS1503: Argument 2: cannot convert from
'System.Data.Objects.ObjectParameter' to
'System.Data.Entity.Core.Objects.ObjectParameter[]'
And on the other project with the data I get this error
error CS0006: Metadata file 'C:\Users\United\Documents\Visual Studio
2012\Projects\C#\EntityFrameworkDemo.Data\EntityFrameworkDemo.Data\bin\Debug\EntityFrameworkDemo.Data.dll'
could not be found
I couldn't find a solution to any of the errors. Can you suggest something?
Here is the sample code that I'm trying to run:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using EntityFramework;
namespace EntityFramework
{
class Program
{
static void Main(string[] args)
{
NorthwindEntities db = new NorthwindEntities();
foreach (var customer in db.Customers)
{
Console.WriteLine(customer.ContactName);
}
db.Dispose();
}
}
}
It looks like you have conflicts between the types defined in the Sysytem.Data.Entity assembly and the EntityFramework assembly. If you're using EF 6 or later, remove all references to System.Data.Entity and try recompiling. You may need to remove some lingering using statements for those types as well.
I am new to C# programming .
I am trying to a compile a C# program which requires mono cecil
This is the code i am trying to compile
I dono how to add reference ...can someone help me out in this?
using System;
using Mono.Cecil;
using Mono;
public class Program {
public static void Main() {
string infile = "in.exe";
string outfile = "out.exe";
Guid guid = Guid.Parse("12345678-1234-1234-1234-1234567890ab");
AssemblyDefinition asm = AssemblyDefinition.ReadAssembly(infile);
ModuleDefinition mod = asm.MainModule;
mod.Mvid = guid;
asm.Write(outfile);
}
}
I get the following error when i compile the program using mcs
error CS0234: The type or namespace name 'Cecil' does not exist in the namespace 'Mono'.Are you missing an assembly reference?
I couldnt find Mono.Cecil.dll in /usr/lib/mono/4.0 & /usr/lib/mono/2.0.
Mono.Cecil.dll is present only in /usr/lib/monodevelop/bin/
Kindly let me know if i am missing anything? and how do i get rid of this error ???
Regards
Puliyan
You need to tell the compiler with the -r, -pkg, or -lib options where to find Mono.Cecil.dll.
A solution that always works is building the .dll from source:
git clone https://github.com/mono/cecil.git
cd cecil
xbuild /property:Configuration=net_4_0_Release
You can also use other configurations (e.g., net_4_0_Debug). Check the .sln or .csproj file for values. You'll find Mono.Cecil.dll in the obj subdirectory. You can then copy that library to whatever location you want and compile with -r:/path/to/Mono.Cecil.dll, -lib:/path/to/libdirectory -r:Mono.Cecil.dll, or if you're using MonoDevelop, add a reference to the library (in MonoDevelop, you should also be able to reference the project directly).
Cecil should normally also be available via the pkg-config mechanism; however, the cecil.pc file seems to be misconfigured. Normally, just using -pkg:cecil should suffice, but that appears to be broken and instead you have to use something like:
dmcs -r:`pkg-config --variable=Libraries`
in order to get the full path to Mono.Cecil.dll in the GAC.
Also, because mono is split up in a number of packages under Debian, you may have to install additional libraries if the above doesn't work (I don't know at the moment whether Cecil is part of the core package or not).
This problem has been causing me a headache for a few days, and I cannot find a reason for it. I'm pretty sure this is an environmental issue particular to my machine, but still its causing me problems with testing.
I'm creating a DLL in C# using Visual Studio 2010 Professional. Version one is below;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestCOM
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
[System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
public class Class1
{
public void showMessage()
{
MessageBox.Show("Hello from TextCom");
}
}
}
This assembly compiles fine, and everything is good. I run the following script to register it as a COM object (first for 32-bit, then for 64-bit);
C:\Windows\Microsoft.NET\Framework\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM32.tlb
C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regasm.exe TestCOM.dll /codebase /nologo /tlb:TestCOM64.tlb
And then use the following script to test it;
dim tc
set tc = CreateObject("TestCOM.Class1")
tc.showMessage()
I use csript to test the script, so I can control which bit depth it uses - I test it once with 32-bit and once with 64-bit. So far everything is good.
Now, when I modify the original assembly to add a function, as follows
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace TestCOM
{
[ClassInterface(ClassInterfaceType.AutoDual)]
[System.Runtime.InteropServices.ComVisible(true)]
[System.Runtime.InteropServices.ProgId("TestCOM.Class1")]
[System.Runtime.InteropServices.Guid("803A1B2F-1CDA-4571-9084-87500388693B")]
public class Class1
{
public void showMessage()
{
MessageBox.Show("Hello from TextCom");
}
public void HelloWorld()
{
MessageBox.Show("Hello World!!");
}
}
}
Before the modification, I unregistered the library using "regasm /unregister" and it reported all types unregistered successfully.
When i register the library, now with changes, the original test script works perfectly. If I extend the test script to call the new HelloWorld function;
In 32-bit scripts, it works perfectly.
In 64-bit scripts, it complains that no such function exists for the TestCOM.Class1 object
I've tried this every which way I can, but I cannot identify why the new function is available to the 32-bit callers, but not the 64-bit calls.
What am I doing wrong ? is there a cache somewhere for the 64-bit stuff I'm not aware of, or a registry setting that needs changed?
To be clear;
1. Build assembly
2. Register using regasm, once for 32 and once for 64
3. Test using script - everything works
4. Unregister library
5. Make modifications, rebuild
6. Register as per Step 2
7. Tests work in 32-bit, but not 64. Wtf ?
Clearly you are suffering from DLL Hell, always around with COM, it is loading an old version of your DLL. Your GAC could have gotten polluted by earlier experiments, it will always find the GACed version first. You are making it worse by specifying the [Guid], making your new class look the same as the old one, even though it is not identical. Preventing COM from telling you that it cannot find the new version of the class.
The most reliable, although noisy, way to see where the DLL came from is by using SysInterals' ProcMon utility. You'll see it reading the registry key and loading the DLL. You can see what directory it came from. Make sure it is not the GAC, remove it with gacutil /u if that's the case, and make sure that you got it rebuilt by checking the timestamp on the file.
I'm doing some exercises with C# in the trial version of VS 2012. I want to execute a cmd command from a CS file. For this, I've tried Process.Start as well as System.Diagnostics.Process that are mentioned in these posts:
Run Command Prompt Commands
Execute CMD command from code
However, despite I added "using System.Diagnostics" and "using System.ComponentModel", I'm still getting "The type or namespace name 'Process' does not exist in the namespace 'System.Diagnostics', missing assembly reference" error. ¿Any suggestion so I can i get rid of this error? Thanks in advance.
This usually happens when you have Target framework = .NET Framework Client Profile, but DLL you reference is from .NET Framework (full). Make sure you have System.dll in your references from valid framework.
I just did the same - created empty console application with the following code:
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.ComponentModel;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var prc = Process.Start("explorer.exe");
}
}
}
Works perfectly fine for me.
Additional thing to check is Intellisense - when you start typing "System.Diagnostics.Proc"... - does it show you dropdown with "Process" there?
UPDATE:
Windows Store projects are based on different version of target .NET Framework - .NET for Windows Store apps, which does not support functionality you need.
For more details do web search:".NET for Windows Store apps". Helpful links:
http://msdn.microsoft.com/en-us/library/windows/apps/br230302.aspx
http://msdn.microsoft.com/en-us/library/windows/apps/br230232.aspx