I am trying to build a C# project (downloaded code) in Visual Studio Express 2008. I get the error (my translation):
The type or namespace name "Allegro"
couldn't be found.
for the line
using Allegro;
I know the 2D graphics library Allegro, of course, but I can't find much information on how to use it in C#. It is being used for visualization in the project I am trying to compile.
I also get the warning
This reference couldn't be resolved.
The Universal assembly couldn't be
found.
I haven't been working with C# before and and I barely know Visual Studio Express. These are newbie mistakes - but I just need a fast solution for this problem. Could someone provide me with a short step-by-step solution?
Remove old reference of Allegro.dll from project and add it again.
Allegro isn't a .NET library; it's written in traditional unmanaged C. If you downloaded code that references an Allegro C# library, then perhaps the author was using some unofficial C# bindings. If so, the solution is to find that additional library and use it.
Otherwise, there is no short, step-by-step solution. You would need to use DllImport on each function that you need, and follow the standard practices for using unmanaged C DLLs in C#.
Related
I want to create a Windows application using a C# GUI that calls some C++ code but I have troubles setting up the project in Visual Studio.
I understood that I have to do it the following way : 3 projects (the C++ project, the C# project and a wrapper CLR project) that are linked to each others in some way. I just can't figure out how the link works.
If anyone can explain me in details what I have to do, I'd be really happy :3
Otherwise, I can use a Github link of a project already set up that I could copy.
Thanks in advance to anyone who will take some time to answer.
http://www.astro.com/swisseph/swephprg.htm
I am trying to add swedll32.dll as reference into visual studio 2010 through add Reference in my project.
But it's giving an error message:
reference can not be added, please make sure that the file is accesible,
and that is a valid assembly or com component
Can anybody help me in adding this dll to visual studio?
The dll is a native, unmanaged dll. The development started 1997, 4 years before .NET even became available. You will need to find a way to either PInvoke the methods or use a programming language better suited to interface with it like Cor C++.
You may want to start reading about DllImport, which is a way to import native dlls to call from managed code. This is a good starting point.
I've read many solutions to the issue of using C# code in a C++ project, including this one: How to use c# code in C++ project
I've also read this, and one answerer says Compile your C++ code with the /clr flag. With that, you can call into any .NET code with relative ease.
Does this mean that I can use C++ & C# code together, within the same project if I append the /clr flag?
I'm a little confused by this, and it'll just waste time if I went the long route of converting the C# project that I'm trying to use parts of to C++, if doing so isn't actually needed.
Basically, I answered my own question on a different topic a couple of days ago without realising that the actual code of the sample project is C#:
Obtaining Current Network Rate
Can anybody give me a firm clarification to whether I understand this correctly (that I can use C# code in a C++ project with no issues, apart from the /clr switch being required)?
I'm not sure if SO is the right place to ask a question like this, so please tell me if it isn't instead of downvoting with no explanation.
Thanks.
Edit
Forgot to mention that this is a C++/CLI gameserver application DLL on Windows. If it matters, it is used on only Windows Server 2008 R2 and Windows 7.
If you use C++/CLI, then you can add references to other .net assemblies, but you do not mix C++ and C# code in the same project. What you would do is to create a new project (or add an existing one) in your solution using C#, and then add a reference to it in the C++/CLI project.
The drawback is that you need to marshal between C++ and .Net types (std::string vs System::String^), and you also need to learn the additional syntax used by C++/CLI (^, gcnew, etc.).
Further reading: Pure C++: Hello, C++/CLI
It means, that with the /clr switch, you can access assemblies (DLL or EXE) written for the .Net framework. Because such assemblies can be created with any .net language (including C#), you can use "code" written in C# from within C++, but it doesn't mean that you would be able to write C# in C++ project. Unfortunately - you can't.
You need to make a C++/CLI project in visual studio.
C++/CLI is a bit different to C++, because there is the managed stack, so memory is managed and you can avoid memory leaks (garbage collection).
Simply create a C++/CLI project and click on propietys and add a reference to your C# project, it should work :D
http://www.codeproject.com/KB/cs/ManagedCOM.aspx
I have written a library in C that I want to be able to be used in C#, C++, and Python (other languages would be nice, but not necessary for now). The library was developed in Linux and I have been able to compile it on Windows via MinGW. The problem I am having is when trying to add the .dll as a reference in a Visual Studio 2010 solution. The error I get is:
A reference to 'C:\path\to\libmylibrary.dll' could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component.
I have spent some time trying to see if I am compiling the DLL incorrectly, but don't see anything glaring out at me. Some references I've used are this, this, and this.
If more information is needed I can provide some compilable example source code.
There are 2 ways to add a "DLL" to a C# project.
If the DLL is a CLR Assembly, meaning it is managed code that adheres to the CLR, then you can add it as a "reference".
If the DLL is NOT a CLR Assembly, you can load the code manually using the P/Invoke structure. There's a lot of online documentation on P/Invoke. It is messy, but it works. You need to declare each DLL entry function using the [DllImport] attribute, and load the DLL manually. Search SO for p/invoke.
Based on your error message, you are trying to load a plain DLL as an CLR DLL. That means you'll have to figure out how to use P/Invoke.
I'm going to ask a newbie question here. I need to use a control I found online in my code, but it was written in the wrong language. It is in C# and I am using VB.net for work. I have all of the code for the control which works beautifully in C#, but I had issues with it not working very well in VB.
The source code and a demo project are available here, but I am not sure exactly which files I should use to build the dll for VB.
It seems a lot to ask, but would someone mind taking a look and pointing me in the right direction?
Cheers
Add the project to your solution, or just build it using your version of Visual Studio. Then add a reference, either to the project in the first case, or the .dll in the second.
If the Control doesn't work well, it's not because it's written in C# as opposed to VB.Net.
You should be able to use/reference the DLL as-is from your visual basic solution. It doesn't matter whether it was written in C#, VB.NET or any other .net language.
One you compile a dll from the c# source it will work exactly the same as if it were written in vb. You simply need to create a reference to the dll in visual studio.