简体   繁体   中英

Unable to Import a Visual Studio made C++ DLL into a Visual Studio C# Project on Windows

I have been trying to monitor video memory for a highly performance intensive program.

So I used the NVIDIA CUDA libraries in a C++ Visual Studio Application to collect the data I need.

Unfortunately I need a package that can be used with Ruby, C# and the Software Testing tools my company has.

So I decided to compile a C++ DLL and import it into a C# Visual Studio Project for testing.

I was able to compile the DLL but importing it into my Visual C# Program has been an issue.

I right Click on References -> Add Reference. Then I click on the Browse tab and browse to the location of my DLL; "TestProgram.dll" and I click "OK"

Then I get the following Error message "A reference to C:.... could not be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component."

What I have tried so far: 1. Importing the DLL from several different locations on my system. 2. Moving the DLL into my Solution Explorer and importing it 3. Using the "Invoke" method: [DllImport("PathToMyDll")] 4. Using the "Invoke" method with just my dll name. 5. Typing 'using' path to my DLL 6. Exporting the dll as a .lib file instead

What am I doing wrong? I thought that if I exported a DLL using Visual Studio 2010 then surely that same DLL could be Added into a Visual Studio 2010 project.

Well, is it a valid Assembly or COM component? Just because it's a C++ DLL doesn't mean it can be used with just a reference, in fact, most can't. It needs to be a managed DLL or a COM object. If it's none of those, then you have to invoke unmanaged code from a managed assembly. That means defining each function in a stub.

More info here: how to call a C++ dll exported function from c#

Be aware, you also have to deal with 64/32 bit issues, since if the DLL is compiled for 32 bit, you can't use it in a 64 bit application.

I found the solution to this problem. CUDA creates a Windows Console Application project. So the solution was to just use that instead of a DLL.

This worked well because Ruby, Test Complete and Visual C# can read the output from a basic .exe console program (with some work).

The compiled exe is simple. All I need to do is pass in an integer as an argument when I launch the program and the program will get the data I need from my NVIDIA card.

The challenge now will be finding a good way to interface with the .exe.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM