Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, March 24, 2019 5:47 PM
Hi, when crating a new .NET project, the folders \bin and \degub are created. From the Solution Explorer of Visual Studio, selecting a project and the right-click->Add->Existing Item (an unmanaged DLL in this case), then VS will copy the selected file in the main folder of the project. To import the unmanaged DLL in a C# project i use the [DllImport("file.dll")] attribute. When run/debug the code, it will throws an exception because cannot find the DLL since the system is looking for the file inside \bin or \debug.
I ask if the DLL must always be copied in both \debug and \bin or if there is a more elegant way to handle this problem, maybe from some VS settings, or using another approach.
Thank you.
Paolo
All replies (11)
Sunday, March 24, 2019 6:35 PM | 1 vote
In order to copy the file automatically on each Build, select the file in Solution Explorer, right-click, go to Properties (or press <F4>). Then set the “Copy to Output Directory” option.
It will be copied to “bin\Debug” or “bin\Release”. It does not have to be in “bin”.
Sunday, March 24, 2019 8:30 PM
The first thing to understand is that there are many types of DLLs. Programmers often say DLL without specifying what type.
The type of DLL that we use DllImport with is often called a Native DLL. We cannot use C# to create a native DLL. See my Native Windows Dynamic Link Libraries (DLLs) for more about that.
The type of DLL that .Net creates is called a Class Library. My guess is that that is what you want. Create a Class Library project for your DLL. Then it will all be much easier.
Sam Hobbs
SimpleSamples.Info
Monday, March 25, 2019 10:37 AM
Thanks Sam, in fact i am willing to properly work with Native DLL. I have a bunch of scientific libraries that were compiled from FORTRAN code. I was able to address them but i was sure that to properly use them there is a more elegant way then manually copying them in \bin \release folders. Dylan has gave a good answer, but if you can give more hints i will appreciate a lot.
Thank you.
Paolo
Monday, March 25, 2019 6:26 PM
Hi, when crating a new .NET project, the folders \bin and \degub are created. From the Solution Explorer of Visual Studio, selecting a project and the right-click->Add->Existing Item (an unmanaged DLL in this case), then VS will copy the selected file in the main folder of the project.
I am confused. Is the FORTRAN library already compiled as a DLL or as DLLs? If so then I see no need to copy them to the project, you can use them wherever they are at. And I don't see any value in adding the unmanaged DLL to the project. I assume there is not a separate debug version of the library.
If the FORTRAN library is already compiled then I do not see a need to do anything, at least nothing more than what is necessary to use the library outside of development purposes. The typical solution is to add the library's directory to the path but there is another way to use libraries that most developers are not aware of. However updating the path would work if I understand the problem.
Sam Hobbs
SimpleSamples.Info
Tuesday, March 26, 2019 5:33 AM
Hi Sam, thanks for answering and sorry if i am not able to be very clear in my statements.
I am reading your DLL article and is very enlightening.
Answering to your comments:
"Is the FORTRAN library already compiled as a DLL or as DLLs?"
It is one single DLL distributed from program vendor. It was compiled from FORTRAN source code and it is unmanaged, eg "native DLL".
"I don't see any value in adding the unmanaged DLL to the project"
I guess you refer to the fact that importing (and so copying) a file that was already present somewhere is bad practice and so it is better pointing to it instead of creating a copy...Is what you mean?
"The typical solution is to add the library's directory to the path"
May i ask where this path should be set exactely? I tryed to "Add Reference" but since the DLL is unmanaged and does not respect a COM "interface", VS says that "cannot be added. Please make sure that the file is accessible, and that it is a valid assembly or COM component". By the way, i can use the DLL if i use [DllImport] attribute.
" but there is another way to use libraries that most developers are not aware of"
I would really know which methods you refer about.
Tuesday, March 26, 2019 6:56 AM | 1 vote
Hi Paolo,
Also you can use xcopy which is a windows command to copy your library files to your output directory.
Please right-click your project >> click Properties >> select Build Events,then you can enter command line in post-build event command line window
The command line "xcopy D:\a D:\b /y" means that you can copy all files in D:\a to D:\b and overwrite the existing file. Please check here to learn more.
Hope it can help you.
Best Regards,
Dylan
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]
Tuesday, March 26, 2019 7:07 AM | 1 vote
The path environment variable has been around since DOS days. It is something that people that have been doing Windows (even non-programmers) are familiar with. I am not implying anything about that, I can understand that a young person might not have encountered such a thing.
If you go to System in the Control Panel and click on Advanced system settings in the left menu then you will get a System Properties dialog/form. Near the bottom is Environment Variables. There you can see and modify the environment variables specified for your account and for the global system.
The FORTRAN DLL I assume has been installed and that involves:
- Copying it to someplace
- Updating the path to include the directory the DLL has been copied to
I assume those two things have been done already. They should be done by a setup program. If not then you can do that yourself and then the DLL can be used in that system for both development and production. Do you know if the DLL has been installed like that?
In the Environment Variables dialog click on Path then click on "Edit...". You will see a list of directories. Be sure to click Cancel unless you want changes to be saved. You can add another directory such as the directory where the DLL is. Then you need just one copy of the DLL. It is best to use just one copy for situations such as this.
I have a directory called Software that I put exe and DLL files in that I need to install by just copying.
Sam Hobbs
SimpleSamples.Info
Tuesday, March 26, 2019 7:25 AM
That's great! I was not aware about that.
Paolo
Tuesday, March 26, 2019 7:28 AM
Thanks, i will test all your advices.
Paolo
Wednesday, March 27, 2019 7:00 AM
Hi Paolo,
Sorry for bothering you.
If the reply is helpful for you, please mark or propose it as answer. It will be very beneficial for other community members who have similar questions.
Best Regards,
Dylan
MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected]
Wednesday, March 27, 2019 7:14 AM
Also you can use xcopy which is a windows command to copy your library files to your output directory.
That might be confusing Paolo since we have determined that there is only one DLL file.
Please right-click your project >> click Properties >> select Build Events,then you can enter command line in post-build event command line window
That also might confuse Paolo since the DLL is built and there is no source for it.
Sam Hobbs
SimpleSamples.Info