add dll reference

Hi
I’m trying to add a DLL reference to my project, actually I want to communicate with microchip called MCP2210 that is supported with dll file used in Visual Studio projects.
The problem that after adding that dll file to my project through MonoDevelop then I should add “using MCP2210” (based on their instructions in Microchip website) I always get the

the following error: error CS0246: The type or namespace name `MCP2210’ could not be found. Are you missing a using directive or an assembly reference?.

I searched on the internet on how to add VS dll file to unity then I found that the solution is to build a class library project in VS where I can add the proposed dll file plus UnityEngine.dll file then create class in that project and call all the needed methods from that dll file at the end I can add the new dll file which is created by VS studio to unity, so when I did all of that, the error gone in debugging but, when I built unity game I have got another errors saying that:

1- Error building Player: Extracting referenced dlls failed.

2-rgumentException: The Assembly MCP2210DLL-M is referenced by USBDLLCLASS. But the dll is not allowed to be included or could not be found.
UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List1 alreadyFoundAssemblies, System.String[] allAssemblyPaths, System.String[] foldersToSearch, System.Collections.Generic.Dictionary2 cache, BuildTarget target) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/AssemblyHelper.cs:137)
UnityEditor.AssemblyHelper.AddReferencedAssembliesRecurse (System.String assemblyPath, System.Collections.Generic.List1 alreadyFoundAssemblies, System.String[] allAssemblyPaths, System.String[] foldersToSearch, System.Collections.Generic.Dictionary2 cache, BuildTarget target) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/AssemblyHelper.cs:140)
UnityEditor.AssemblyHelper.FindAssembliesReferencedBy (System.String paths, System.String foldersToSearch, BuildTarget target) (at C:/BuildAgent/work/d63dfc6385190b60/Editor/Mono/AssemblyHelper.cs:172)
UnityEditor.BuildPlayerWindow:BuildPlayerAndRun().

(by the way “USBDLLCLASS” is the name of the new dll file created by vs, and “MCP2210DLL-M” is the name of the dll file that supported by microchip for vs projects.(Iam using csharp in unity)).

knowing that I re-added the both dll references to Mono-Develop and I pasted the dll references in new folder inside the assets folder called Plugins!!
can anyone help me with this?.

I know that I didn’t add any script with the question cause I think it is not related to that!!
Thanks in Advance

Some steps to getting a C++ dll into Unity on Windows:

Make the Visual Studio project

  • Make a visual studio DLL project. This is the version of projects where no .exe is generated.
  • Write your code. Get it to build and get it debugged.

**Build it correctly **

  • Unity has 2 version, x86 (32 bit) and x64 (64 bit) you need to build the correct version
  • In the editor a debug build will work. On your end deployment machine, such as a gamer’s PC they may only have the Visual studio runtime, so debug will not work. So in the end you MUST have a release version of the DLL.

Deploying

  • You must put the dll file in the correct place. The best is in Plugins directory under Assets.

  • Add the dll to you Assets folder in Unity Editor. This way you can set any special settings, and it bets bundled up when you do a release build.

  • Make sure the visual studio runtime gets installed. If your game is on Steam then you need to include the VS runtime as one of the required parts in the application setup screens in Steam. I usualy include both x86 and x64 runtimes but that may not be needed depeding on what your C++ code is doing.

Make the C# bridge from the C++

  • Make a C# class that is ALL static public functions.

  • Bind all the methods in your C++ dll to declarations in C#

  • Make any other helper code that is also static. Remember, static functions are in effect global singletons.

The declaration of a function in C# from the C++ dll might look like this…

public class CustomSerialBridge  {

    public const string LibFile = "CustomSerial";

    [DllImport(LibFile)]
    public static extern void VS_InitVoidSerial();

... etc...
}

About the DLL name

It appears that the “.dll” does not need to be on the end but can be.

Debugging

So you dll won’t load. Possible problems…

1.) Not in a findable place. Maybe not in the Plugins directory? You can also put the dll in the same directory as the .exe for your game, but that is harder to deploy correctly.
2.) Typo in the dll name?
3.) Not correctly 32 bit or 64 bit to match Unity?
4.) Your dll is Debug and should be Release for releases to outside gamers? If it works fine on your machine in the debugger, but not on people’s machies that bought your game, this might be why.
5.) Not included in Unity so it gets deployed. This would show up as the dll not being in the Plugins directory in your deployment.
6.) Missing other dependant dlls? Maybe your DLL depends on some other dll that is not getting included. Since Unity does not give meaningful error messages you need to get better error messages. One good way to do this is to make a completely separate C++ project, console application that loads your dll and calls one method in it. Then when it fails you get much better indication as to why. Run the resulting .exe in the same folder as you .dll and see what happens. If the missing DLL is VCRUNTIME140D.dll or some such with that ‘d’ just before the ‘.dll’ then it means your dll is Debug instead of Release.

Sample code…

#include "stdafx.h"
#include <stdio.h>
#include <Windows.h>

int main()
{
	printf("Custom Serial test");
	HMODULE libHandle;

	if ((libHandle = LoadLibrary(TEXT("CustomSerial.dll"))) == NULL)
	{
		printf("load failed: CustomSerial.dll

“);
printf(”%d
", GetLastError());
getchar();
return 1;
}
printf(“Load DLL ok”);

	if (GetProcAddress(libHandle, "VS_InitVoidSerial") == NULL)
	{
		printf("VS_InitVoidSerial failed

“);
printf(”%d
", GetLastError());
getchar();
return 1;
}
printf(“Done.”);
getchar();
return 0;
}

Blah, crappy code formatting in this forum application. You need to include Window.h stdio.h stdafx.h

7.) As last resort go get the depends progam off the net and run it on your dll to see what might be missing.

I solved the problem!!
I added the DLL file to the root of unity if this directory C:\Program Files (x86)\Unity\Editor,
after building the game I added the same file to the folder called mono …I didn’t get what i need till this moment but at least that error doesn’t appear anymore