External DLL Reference with Visual Studio

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 624 Views - Last Post: 08 December 2017 - 10:32 AM Rate Topic: -----

#1 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

External DLL Reference with Visual Studio

Posted 06 December 2017 - 04:45 PM

Hi

I've looked through several tutorials to reference my external DLL for an application so it can make use of its methods however I'm getting the error 'cmd.exe exited with error 1' on build.

Relative to my test application vcxproj, ../../MyDLL/Debug contains MyDll.dll and MyDll.lib.
../../MyDLL/MyDLL contains all of the class headers.

In the test application's properties I set:

Additional Include Directories: ../../MyDLL/MyDLL/
Additional Library Directories: ../../MyDLL/Debug

I then set the Configuration to All Configurations and the Platform to All Platforms.
I then added the .lib in Additional Dependencies.

Then I created a file called MyDLL.ref, set its type to Custom Build Tool and set the Platform to x86.

Then I set:

Command Line: COPY "$(ProjectDir)..\References\%(Filename)\%(Filename).dll" "$(TargetDir)" > NUL
Description: Copying %(Filename) binaries to output directory...
Outputs: $(TargetDir)%(Filename).dll
Additional Dependencies: $(ProjectDir)..\References\%(Filename)\%(Filename).dll

If you could help me to solve this issue I would greatly appreciate it.

This post has been edited by Rixterz: 06 December 2017 - 04:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: External DLL Reference with Visual Studio

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14096
  • View blog
  • Posts: 56,495
  • Joined: 12-June 08

Re: External DLL Reference with Visual Studio

Posted 06 December 2017 - 04:53 PM

Why wouldn't you just use Visual Studios to right click on the project -> add -> reference and find the DLL from there?
Was This Post Helpful? 0
  • +
  • -

#3 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: External DLL Reference with Visual Studio

Posted 06 December 2017 - 04:54 PM

There's nothing in that window. I also need to make sure the referenced DLL is updated when I modify its code and rebuild it.

This post has been edited by Rixterz: 06 December 2017 - 04:57 PM

Was This Post Helpful? 0
  • +
  • -

#4 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 06 December 2017 - 06:12 PM

'cmd.exe exited with error 1' usually means you have an incorrect command line in your Project....

I would check all paths are correct and you are using the correct commands in your Build Events.

Read this article please.

https://msdn.microso...y/ms235636.aspx
Was This Post Helpful? 0
  • +
  • -

#5 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: External DLL Reference with Visual Studio

Posted 06 December 2017 - 06:42 PM

Ok I managed to fix that issue by playing around with relative paths but now I'm having another issue.

I need to copy all .h files, the .dll and .lib into a directory. I've done that, but I want to omit stdafx.h and targetver.h.

To do this, I copied all .h files using copy "*.h" "$(TargetDir)..\INCLUDE" > NUL, then copied the dll and lib, that's 3 .ref files. I created another two, each deleting one of stdafx.h and targetver.h. The issue is that these are executed before the others, meaning nothing gets deleted, then all of the files are copied; I need to copy the files first then delete the specific .h files.

I know there's an Execute After/Before option but it's greyed out; how can I set the order?
Was This Post Helpful? 0
  • +
  • -

#6 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 06 December 2017 - 11:06 PM

just create an empty win32 project that way stdafx.h .... etc .... wont be added.

just create an empty win32 project that way stdafx.h .... etc .... wont be added.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6220
  • View blog
  • Posts: 21,469
  • Joined: 05-May 12

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 12:25 AM

For what you are doing, what you should really do is create a single solution, but have to C++ projects within the solution. You can then have a project reference the other project not dll. Visual Studio will take care of building things in the right order. As I recall, it will also take care of gathering all the .LIB and .DLL files and putting into a common set of directories so that you don't have to manually copy those files. So the only thing that you are left to do is figure out how to reference any shared header files.

Instead of copying things around, one way to structure your files looks something like this:
solution\
    solution.sln
    docs\
    tools\
    common\
        inc\
            public_component1.h
            public_component2.h
            :
        lib\
            component1.lib
            component2.lib
            :
    mainexe\
        mainexe.vcxproj
        stdafx.h
        stdafx.cpp
        private_main.h
        main.cpp
        support_for_main.h
        support_for_main.cpp
        :
    component1\
        component1.vcxproj
        stdafx.h
        stdafx.cpp
        private_component1.h
        component1.cpp
        support_for_component1.h
        support_for_component1.cpp
        :
    component2\
        component2.vcxproj
        stdafx.h
        stdafx.cpp
        private_component1.h
        component1.cpp
        support_for_component1.h
        support_for_component1.cpp
        :
    :



By planning ahead and putting your publicly shared headers in common\inc\, and changing the project build destinations to put the DLL export libraries into common\lib\, you minimize the copying files around. Your various .vcxproj simply have to include files form common\inc\ and try to link libraries from common\lib\
Was This Post Helpful? 0
  • +
  • -

#8 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 06:42 AM

So I've tried to use the methods in the DLL but it's outputting weird characters.

I included the .h in my test application, the .h, .dll and .lib are in the same directory as the test application .exe.

When I do the following, the MessageBox contains weird characters.

#include "..\INCLUDE\ClientConnection.h"

...

ClientConnection* tClass = new ClientConnection(5);

char buff[100];
sprintf_s(buff, "ClientConnection: ", tClass->getSomething().c_str());
MessageBox(0, (LPCWSTR)buff, L"Debug Message", MB_OK);

delete tClass;



The definition of getSomething() in the referenced .h is:

std::string ClientConnection::getSomething()
{
	return "Wow, something!";
}



This issue still happens even if I pass the buffer size as the second parameter of sprintf_s.

Side note- am I managing the memory correctly by deleting tClass after it's used? Will I need to do this for buff as well?

This post has been edited by Rixterz: 07 December 2017 - 06:45 AM

Was This Post Helpful? 0
  • +
  • -

#9 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 07:25 AM

This line

sprintf_s(buff, "ClientConnection: ", tClass->getSomething().c_str());



should be

sprintf_s(buff, 100, "ClientConnection: %s", tClass->getSomething().c_str());


note the %s.

also you dont need to use the microsoft specific versions of sprintf just

#define _CRT_SECURE_NO_WARNINGS


At the top of your code before any #includes

buff does not have to be deleted as there is no 'new' involved.

This post has been edited by snoopy11: 07 December 2017 - 07:30 AM
Reason for edit:: extending answer

Was This Post Helpful? 1
  • +
  • -

#10 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 08:11 AM

I'm only targeting Windows so I'd rather use the Microsoft specific one and not have to suppress the warning.

Screenshot of issue:
here
Was This Post Helpful? 0
  • +
  • -

#11 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 08:17 AM

Those are Unicode characters either use swprintf_s or in your project properties instead of using Unicode use Multi-Byte.
Was This Post Helpful? 1
  • +
  • -

#12 Rixterz   User is offline

  • D.I.C Head

Reputation: -7
  • View blog
  • Posts: 163
  • Joined: 26-August 12

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 08:25 AM

Excellent, changing the DLL project and test project to use Multi-Byte worked. Are there any downfalls to doing that?
Was This Post Helpful? 0
  • +
  • -

#13 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 08:36 AM

No apart from if you want to support multiple languages.. there are no downfalls.
Was This Post Helpful? 1
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6220
  • View blog
  • Posts: 21,469
  • Joined: 05-May 12

Re: External DLL Reference with Visual Studio

Posted 07 December 2017 - 11:59 PM

Actually, there if you are using MultiByte instead of Unicode you are paying a thunking tax everytime you make an OS call because the OS is natively Unicode, but you will be calling the multibyte versions of the APIs. The OS will have to convert any strings from multibyte to Unicode and then turn around and call the Unicode version. If any strings are returned, then the OS will have to convert from Unicode back to multibyte so that your multibyte program will have data in the form that it expects.

Since in another thread you mentioned that you are compiling for x86 (e.g. 32-bits) then you are now paying a double penalty where the OS not only has to thunk from multibyte to Unicode, and then it has to thunk from 32-bit to 64-bit each time you make an OS call.
Was This Post Helpful? 0
  • +
  • -

#15 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1467
  • View blog
  • Posts: 4,726
  • Joined: 20-March 10

Re: External DLL Reference with Visual Studio

Posted 08 December 2017 - 12:14 AM

God never heard of 'thunking' before,

You come up with some right crackers so you do... :)
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2