I have 2 solutions under VS2019: RLibrary (contains C++ project) and VTApp (contains C# project).
Solution RLibrary has two projects: RLib C++ .dll and SwigW (wrapper project).
SwigW generated the following files: RLibCSHARP_wrap.cxx, RLib.cs, RLib.i and RLibPINVOKE.cs.
In the RLibPINVOKE.cs I have lines like:
class RLibPINVOKE {
//...
static RLibPINVOKE() {
}
[global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Create")]
public static extern global::System.IntPtr new_Create();
[global::System.Runtime.InteropServices.DllImport("SwigW", EntryPoint="CSharp_new_Request")]
public static extern global::System.IntPtr new_Request();
// ...
}
So, after building RLibrary solution I will have two .dll (also .lib) files: RLib.dll and SwigW.dll.
I want to use new_Create(), new_Request() functions from the VTApp.
VTApp has two projects as well: UI and Core. UI has a reference to Core. I added RLibPINVOKE.cs file to the Core project by just Right Click->Add->Existing item and tried to use above mentioned functions from the UI project within namespace UI, i.e.,
using System;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;
using System.Windows.Controls;
using System.Windows.Input;
using Core;
using Newtonsoft.Json;
namespace UI {
public partial class MainWindow {
private void Execute_Req(object sender, EREventArgs e)
{
//...
IntPtr p = new_Create();
}
}
}
Unfortunately, I am getting CS01013: The name 'new_Create' does not exist in the current context. error.
In my opinion, even without adding SwigW.dll to the Core project I shouldn't have had this kind of compile time error. So, what is the problem here? How can I add SwigW.dll into the Core project properly if I need to add it? Also, should I add RLib.dll to the Core as well?
Try RLibPINVOKE.new_Create(). The method is inside a class RLibPINVOKE
class RLibPINVOKE {
//...
You'll perhaps need a using, but the Visual Studio should be able to suggest it to you on the RLibPINVOKE.
Related
I have created a NuGet package on .NetCore class library and accessing it from another .net core class library. I can't call my public and static method from a public and static class even tho the class name is detected.
One point to make is that the Package works on a Console App properly, but I am having trouble in accessing funcions in a class library.
To create a package I went to property->Package->Create Package on build and also nuget.exe method. Both lead to the same thing
Both the package and the new project are .net core 3.1 class libraries
Can someone please help me out
NuGet package code
using System;
using System.Collections.Generic;
using System.Text;
namespace TrailCorePackage.helper
{
public static class Utility
{
public static void print(string value)
{
Console.WriteLine(value);
Console.ReadKey();
}
}
}
Code while calling the function in another class library
using System;
using TrailCorePackage.helper;
namespace ClassLibrary5
{
public class Class1
{
Utility.print("value");
}
}
Error
Severity Code Description Project File Line Suppression State
Error IDE1007 The name 'Utility.print' does not exist in the current context. ClassLibrary5 C:\Users\---\ClassLibrary5\Class1.cs Active
Since I can't comment yet, I'll add it like answer. Have you added a reference to your package into your console app?
UPD.
I've just noticed that you are calling function inside the class body. It won't work. Something like this will probably work.
using System;
using TrailCorePackage.helper;
namespace ClassLibrary5
{
public class Class1
{
public void AnyMethod()
{
Utility.print("value");
}
}
}
Very basic knowledge on c# and is the first i use anything p/invoke related.
Help me pls, i have made this code but it doesnt seem to work.
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;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
[DllImport("C:\\Users\\lchris\\Desktop\\SevenZipLib_9.13.2\\SevenZipLib\\SevenZipLib\\7z86.dll")]
public static extern void SevenZipArchive(string c);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SevenZipArchive archive = new SevenZipArchive("file.rar"))
{
foreach (ArchiveEntry entry in archive)
{
Console.WriteLine(entry.FileName);
}
}
}
}
}
It tells me that SevenZipArchive is a 'Method' and is being used like a 'type'.
I have include the library to my project already, i just dont know how to use it.
Here is the library:
https://sevenziplib.codeplex.com/
You first need to remove this code:
[DllImport("...")]
public static extern void SevenZipArchive(string c);
You do not need to provide any p/invoke declarations. The library wraps that up for you.
This is a .net assembly. You use it just as you use any other. Take the following steps:
Download the project. You already did that I think.
Build the solution. Make sure that
In your project, add a reference to the assembly that you built in the previous stage. It's SevenZipLib\bin\Debug\SevenZipLib.dll or SevenZipLib\bin\Release\SevenZipLib.dll depending on the target you selected.
Add using SevenZipLib; to your project code to to gain access to the namespace.
Once you've done that your code will work. You can use the tests project that is supplied as part of the download as a rich source of example code.
I'm having difficulty getting my settings out of App.config. The research I've done so far says that I should be able to use ConfigurationManager.AppSettings (which is part of System.Configuration).
However, I can't get it to compile. And of course it doesn't like the older method (ConfigurationSettings.AppSettings) either as its obsolete.
What have I missed here?
Is it my project's Target Framework (currently set to ".Net Framework 4 Client Profile
")?
And example of my code follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
namespace myNameSpace
{
class Program
{
private void LoadAppConfig()
{
string mySetting;
//This won't compile
mySetting =
System.Configuration.ConfigurationManager
.AppSettings["mySettingName"];
//This compiles but of course is obsolete, and I get that warning.
mySetting =
System.Configuration.ConfigurationSettings
.AppSettings["mySettingName"];
}
static void Main(string[] args)
{
// stuff happens
}
}
}
You need to add a reference to System.Configuration
I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!
If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.
You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.
If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.
I created a solution called Foo.
Added a class library called Foo.Common
Added a console app to call the library code from called ConsoleApp.
I referenced the Foo.Common from ConsoleApp and typed :
using Foo.Common;
public class Program
{
CommonClass c = new CommonClass();
static void Main(string[] args)
{
}
}
and get this back :
Error 1 The type or namespace name '**Foo**' could not be found (are you missing a using directive or an assembly reference?) Z:\Foo\Solution1\ConsoleApplication1\Program.cs 3 11 ConsoleApplication1
Why am i getting this?
what s going on?
Make sure that
The ConsoleApp project has a reference to the Foo.Common project (do not browse for Foo.Common.dll),
the file contains a using directive for the namespace in which CommonClass is declared, and
CommonClass is declared as public.
So your files should look like this:
CommonClass.cs in Foo.Common project:
namespace Foo.Common
{
public class CommonClass
{
public CommonClass()
{
}
}
}
Program.cs in ConsoleApp project:
using Foo.Common;
namespace ConsoleApp
{
public class Program
{
public static void Main()
{
CommonClass x = new CommonClass();
}
}
}
Ensure that under your project settings, the target framework is set as .NET Framework 4 and not .NET Framework 4 Client Profile. I got this same behavior when it was set to Client Profile and it was fixes as soon as I set it to just the regular .NET Framework 4.
Right Click on the new console app solution/project and Add Reference and add the project that contains the Foo namespace
Did you add a reference to the library? Look under "References" in the console project. If its not there, you need to add it.
I posted this as a comment, but I want to expand on it here. What's probably happening is it's seeing using as a statement and not a keyword. It appears you have something like the following:
using System;
namespace TestNamespace
{
using Foo.Common;
public Class { }
}
Try
using System;
using Foo.Common;
namespace TestNamespace
{
public Class { }
}
Instead.
It looks like Foo Bar got this error because his project's target framework was set to the client profile.
Just thought I'd add one more 'solution' -- I created a library that targeted the 4.5 framework. My older project was tarting the 4 framework. I got this error.
Changing the older project to 4.5 made it work.