Unmanaged DLL project and WinForms managed application in one solution

I am using Visual Studio 2008 Professional and I need to create a solution with two projects. One project is managed by the WinForms C # project, the second by an unmanaged C ++ DLL project.

I created them in VS2008, and in an unmanaged DLL project, I exported a simple function that returns some int. In a managed WinForms project, I imported the DLL in the usual way and tried to print the return value in the label:

[DllImport("DllProj.dll", EntryPoint = "GetSomeInt", SetLastError = true)] private static extern int GetSomeInt(); 

But when I create the solution and run, I get a DllNotFoundException . I also tried to add the existing element ( DllProj.dll ) to the WinForms project, but only copied this DLL from the Debug folder to the WinForms project folder, but not to the Debug subfolder where the compiled project is located. Therefore, I still get a DllNotFoundException .

I also tried adding it as a reference, but VS2008 complains that my DLL is not a COM or a managed entity. Is there any way to configure the solution in such a way that I do not need to manually copy the compiled DLL to the Debug subfolder of the WinForms project after each build?

+4
source share
3 answers

You should add a copy of the dll to publish the assembly event.

See Project properties>Build events>Post build event command line .

+6
source
  • Copy the compiled dll to the WinForms project after each build
  • Right click on the dll (which is in the WinForms project), click on properties
  • Set "Copy to output directory" to "Copy if new"
+2
source

DllImport is the right way to use an unmanaged dll, since a C ++ project is unmanageable, you cannot add it as a reference in a C # windowsforms application, and you need to copy manually or with the postbuild C ++ dll event used by DllImport to bin your managed application.

+2
source

Source: https://habr.com/ru/post/1341995/


All Articles