Referencing a C++ DLL from within a C# ASP.NET project using Visual Studio Community 2015 [closed]

This question might be a little odd, but is there any specific way to import a C++ reference into an ASP.NET C# project?

The problem I am having is that VS is giving me the following error:


Error CS0246 The type or namespace name ‘LicenseAdminApi’ could not be found >(are you missing a using directive or an assembly reference?)…


This error only shows up within my Views and not in my Controller or any .cs code, The code within my views where I reference fields defined within the DLL also work without any issues. And the project compiles/builds and runs as expected without any issues.

LisenseAdminApi is a DLL also written by our company, just in a different department and it was developed in C++. It requires a Visual Studio 2015 C++(“Microsoft Visual C++ 2015 Redistributable (x64) – 14.0.23918”) runtime to run.
I also had to change my project to run in x64 and use the x64 version of IIS. Without installing it I get the following error when I try and run the project:


Server Error in ‘/’ Application.

Could not load file or assembly ‘LicenseAdminApi.DLL’ or one of its >dependencies. The specified module could not be found.
Description: An unhandled exception occurred during the execution of the >current web request. Please review the stack trace for more information about >the error and where it originated in the code.

Exception Details: System.IO.FileNotFoundException: Could not load file or >assembly ‘LicenseAdminApi.DLL’ or one of its dependencies. The specified module >could not be found.


Similar to the error VS is giving me within the error list, which makes me think that it’s not using the runtime within VS to read the DLL until the project is compiled. It’s quite odd because like I said the project builds fine and runs without any issues once the runtime is installed, but the error in the error list is not going away and I need to sort it out as company policy is not to commit any code with errors.

Edit
If I were to add the “@using LicenseAdminApi” to the view, it also gives me the same error in the error list. Not really sure what solution to try if any as I don’t really know what’s wrong to begin with.

The usage of the DLL is not the issue, I can call the methods I need and they work. The problem is that VS is giving me an error that seems to not exist when I build and run my project. Apart from the standard way of adding a reference which I assume everyone knows, is there anything I need to enable for VS to read the C++ DLL before compiling?

Edit 2

Within my Views/Web.config I have included my namespace like the following:

<system.web.webPages.razor>
<host factoryType="System.Web.Mvc.MvcWebRazorHostFactory, System.Web.Mvc, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" />
<pages pageBaseType="System.Web.Mvc.WebViewPage">
  <namespaces>
    ...
    <add namespace="LicenseAdminApi" />
  </namespaces>
</pages>

Which according to me should make the namespace available in my views, but it doesn’t.

Once again the code is running fine and my project is building, it is only when I am not debugging that VS is telling me that my type/namespace could not be found. This also only occurs within my views, within my controllers I can reference the namespace without getting an error. Even though I do get the error in VS the code and my views all run perfectly when I debug

Please let me know if anyone needs more details, I have tried many different solutions including all the answers provided here

Thanks in advance

The Visual Studio designer is a 32-bit application and therefore cannot load the 64 bit DLL. The reason it runs fine is that you are running as 64bit I.e. design and runtime are different architectures.

Generally when importing a native dll you write a wrapper class for the P/Invoke and then everything references that wrapper class (likely a static class). That would keep you from getting the designer errors.

You might have design time did in that class but you could always use #if statements to hide the 64 bit specific code and write place holder dummy 32bit items.

1

LisenseAdminApi is a DLL also written by our company, just in a different department and it was developed in C++. It requires a Visual Studio 2015 C++(“Microsoft Visual C++ 2015 Redistributable (x64) – 14.0.23918”) runtime to run.

How about you go and ask the Developers who created the DLL how it’s supposed to be used?

Or, (Shock; Horror) read the documentation that they provided with the DLL on how it should be used?

The use of the ’14 runtime suggests it’s a pretty recent build, so there’s a good chance those Developers are still around …

1

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *