Visual Studio 2010 doesn't find the [DllImport("")] - visual-studio-2010

I'm trying to use an C++ library in my c# application but my Visual Studio 2010 doesn't know the [DllImport("")]-method at all. He always ask me to generate the Methodstub for DllImport.
I already added the "using System.Runtime.InteropServices;" but it doesn't work.
Thanks for your helpt!

It sounds like you're using the DLLImport in the wrong spot in your code. You didn't post your code, but make sure its not in a method. Try putting it in your class:
public class MyClass
{
[DllImport("")]
//method
public Class()
{
}
// Other methods
}

Related

How can I handle the ManagementObject Class in .Net 6.0?

I'm with Microsoft Visual Studio Community 2022 (64-bit) Version 17.0.2, .Net 6.0, and I'm unable to work with the ManagementObject.
Here is the fragment of code I'm testing:
using System;
using System.Management;
public class Sample
{
public static void Main()
{
ManagementObject o = new ManagementObject();
o.Path = new ManagementPath("Win32_Process.Name='calc.exe'");
}
}
However, when I try to run the second statement, that is to say what is the object I want to work with (in this example 'calc.exe'), I get the error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
I'm using System.Management.dll found in the GAC and in c:\Windows\Microsoft.NET\Framework64\v4.0.30319.
I fear that there is mismach of versions, but I'm unable to find the correct version.
Or perhaps that object has been discontinued in .Net 6.0.
Please tell me.
Thanks

Visual Studio unit test not working and not showing tests

I am following MSDocs guide to put up some unit testing. I've never used them before and I've never been using them because to me they are totally useless, but I see it's a common practice, so I wanted to learn more.
Obviously, this is not working.
I've put up some .NET Standard 2.0 libraries with little to no code to do some log4net logging. I've addet a "Unit test project" as the guide suggests and added a method decorated with the "TestMethod" attribute and all the other attributes.
Test explorer windows does not show any test and I'm not able to even run them in any way. What am I missing?
Here's the code
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MrLogger_netstandard;
namespace UnitTests
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void ErrorLogOnFile_Default() => MrLogger.Instance.FileErrorLog("I'M AN ERROR!!! :)");
}
}
The MrLogger class it's just a class that does logger.Debug("Write message"), a classical log4net logging call.
I've tried this too and it changes nothing.
Jeez... help
For anyone wondering, I had to start visual studio as administrator and then it worked.... UGH! :(

VS 2013 Error-"No exports were found that match the constraint:"

I am using Visual Studio 2013 on Windows 10, Today I am getting error "No exports were found that match the constraint:"
I did following but still having same issue:
Solution giving in link
Error message "No exports were found that match the constraint contract name"
by removing/renaming "ComponentModelCache" folder, but still problem.
Run VS Repair, but still having same problem.
Finally, I Uninstalled Visual Studio 2013 and reinstalled, but still having
same problem.
Please suggest what do do now?
Finally, I did find answer myself, instead of deleting/renaming 'ComponentModelCache' folder in path
C:\Users\'username'\AppData\local\Microsoft\VisualStudio\12.0
I renamed folder 'Microsoft' in path
C:\Users\'username'\AppData\local\
This works for me.
You can see here more solutions which require deleting the cache in %APPDATA%.
In my case, the problem was that I was initializing and using class with the MEF framework but didn't declare the class as a MEF class.
How to import a MEF class:
[Import(typeof(ICalculator))]
public ICalculator calculator;
How to define and export a MEF class:
public interface ICalculator
{
String Calculate(String input);
}
[Export(typeof(ICalculator))]
class MySimpleCalculator : ICalculator
{
}
The code example is taken from here
Apologies for contributing to an old "answered" thread. however for me, using Win 10, VS 2013. I had to remove the 'ComponentModelCache' folder from each version of VS that I had installed before this issue was resolved.

how to refactor to a different class in visual studio

I can refactor a big code to a new method which automatically sets the parameters, return type, if static or not etc in the same class. Is there a way that I can move the code to a new function in a different class if I could specify the class somehow? May be I'm missing something silly.
In VIsual Studio there is no possibility to move methods between classes. But if you use ReSharper from JetBrains, you can move static methods between classes with Move To Another Type command.

How to create a COM dll using Visual Studio 2008

How do I create a COM dll using Visual Studio 2008? What are the custom settings needed for creating the dll? That dll should be used in Microsoft Navision(ERP PACKAGE).
Simply create a class library. In the project properties, open the dialog where you can change assembly information and mark the "Make assembly COM-visible" checkbox (sorry, don't know the exact name of the option, using German VS 2008).
Then, add the following attributes to any class that should be used from Navision:
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("YOUR-ID-GOES-HERE")]
[ComVisible(true)]
[Guid("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx")]
public class ComVisbleClass
{
....
}
I suggest that you also manually assign Disp-IDs to the public methods and properties using the DispId attribute. Otherwise, inserting new public methods or properties may break Navision functionality, as the Disp-IDs may be changed upon compilation.
Navision would then refer to the old Disp-IDs which may now "point" to different methods. This is a PITA to debug and solve, so use the DispId attribute from the start.

Resources