using dll built in another version of visual studio - c#

I have to use a third-party dll in my C++ project. All I know about this dll is that it was built in Visual Studio 2013. I use VS2010 and can not switch to 2013 because reasons.
Said dll exports some functions that return std::string. Unsurprisingly, calling those functions produces an access violation in some string deallocation code.
As far as I understand the problem, that's nothing to be surprised of, also nothing that could be done about other than switching to VS2013, which I can't.
Here is the thing though. This third-party dll comes with a c# example project that imports it and works just fine!
So my questions are:
(purely theoretical one) Why? Is it .NET string marshaling magic at work?
Suppose I wrap the offending dll in a managed dll of my own, then use that managed wrapper in my unmanaged code. Will it work?
If the answer to question 2 is yes, then how? I know literally nothing about c# so i wouldn't mind a few pointers.
EDIT:
So out of pure desperation I tweaked the imported function signature in my project - it was std::string (__cdecl *TFunc)();, changed it to BSTR (__cdecl *TFunc)(); - and it works! even though both library manual and the dependency walker tool insist std::string is correct. Posting this as an edit and not an answer because I still don't know what's going on here and it is scary.

Related

Adding Swedll32.dll to VisualStudio

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.

C# Code in C++ Project

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

Adding MinGW compiled DLL to Visual Studio fails

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.

Wrapper C++ in C++/CLI clr:safe for C# COM Interop

is there any way to make a wrapper clr:safe for a project in C++ unmanaged?
My little story started this way,
"Boy, you have to do a project 'C# COM Interop' but this one have to use a 'C++ library' and must result in only 'one' (dll COM)."
Ok, after a few days searching, I realized that is possible to use C++ library in two ways: adding in Resources and calling with PInvoke or creating a wrapper C++/CLI. With PInvoke I can not have only one dll(right?). So I opted for the second option "wrapper C++/CLI". Seemed easy at the beginning, I recompile the library Visual Studio 2005 to 2010, create a CLR project (with keypair.snk and re-signed) added the library. Works! \0/ Now I will use the ILMerge, ohhhoo what is this? Only clr:safe? Why? OK, I try to recompile C++/CLI to clr:safe but erros appears...
How can I fix this?
Thanks in advanced,
ILMerge isn't the right tool for this.
Instead, you compile the C# to a .netmodule, and pass that to the C++/CLI linker along with all the C++/CLI object files.

How to convert C# to DLL to hide the source code?

How can I convert my C# code to DLL file in a way that the user of DLL can’t view my source code?
When I make DLL in the way I always do by making a class library project, importing my classes and compiling it, the source code can still be viewed.
I believe you are looking for an obfuscator. This is a tool that will take a compiled DLL and rewrite the code with the intent of it not being meaningfully decompiled by another user. Visual Studio comes with a free Dotfuscator
Note, this will not actually prevent people from looking at your code. They will instead be looking at a very weird translation of your code. There is no way to prevent people from looking at decompiled versions of your code in C# or any other .Net language for that matter.
This is not something that is unique to C#. It is fact a flaw of every language in existence. It's perfectly possible to decompile C code. The difference though is it's much easier to maintain a lot of the original code structure when decompiling managed languages (.Net and Java for instance) because the metadata maintains the original structure.
obfuscation is what you want to search for.
There is a free one (that is limited) in visual studio called Dotfuscator.
Uses fancy methods to rename your code and alter flowpaths to obscure it.
Consider using an obfuscator.
If you are developing desktop applications converting your code to Dll will not hide the it( there are many tools to decompile the dll or exe files).
but if you are using Asp.Net, then you can compile your site to Dll, and the code will not be visible in the aspx pages, it will be compiled to Dll, you can do that by right click on your project on solution explorer, then choose Publish website
But in all cases .Net Exe files and DLL will be easy to decompile and extract the source code again, unless you use tool to obfuscator your code.
If you mean, the end-user can view your source code by decompiling it, you can protect yourself using an obfuscator.
There is standard obfuscator build in into Visual Studio. In the menu choose Tools / Dotfuscator community edition.
I think my reply to a similar question about JavaScript obfuscation applies here as well: in short, why bother? Your question has already been answered here ("use an obfuscator"), but I thought it wouldn't hurt to find out what your motivations are. Generally, code that you give to people is "in the hands of the enemy" -- if somebody wants to use it/figure out how it works badly enough, they will.

Categories

Resources