Using DLL's and other files on an architecture basis

0

I'm currently making an OpenGL application in Visual Studio 2015, and have successfully linked and included all my things for GLFW, GLEW, etc.

However when I run my application, I need to include glew32.dll, no problem at all. I just go and grab the x64 dll and add it to the project folder. However now when I run my program in 32-bit mode, it breaks, and vice-versa if I was to use the 32-bit dll on a 64-bit program. The only cheap fix to this is to include the architecture specific dll's to the build folders.

Is there a way I can include the dll's on an architecture-specific basis, because I want to house my resulting program in a form such as:

Program Directory

  • game.exe
  • game_x64.exe
  • x64 (folder)
    • glew32.dll
  • x32 (folder)
    • glew32.dll

If something like this isn't possible, I'm more than happy to have a glew32.dll and glew32_x64.dll housed in the one folder instead, but that'll probably never happen due to the library not looking for the new dll...

user613581

Posted 2016-09-21T08:22:37.467

Reputation:

My guess is this should be over at stackoverflow. If you're already doing multiple builds (one for x32 and one for x64) shouldn't it be possible to include the linkage of that DLL in either build configuration?

– Seth – 2016-09-21T08:30:15.663

@Seth I was thinking of SO too, but realised that it isn't really a programming question (unless it is :O) but rather a question on how to get VS to do it for me. As for the linkage, I can't find any options. I link the include headers, then the .lib locations, and the .lib names; thats about it. No .dll specification. :O – None – 2016-09-21T08:33:51.553

What language are you using? Either of those links might help if it is C#: Link1 Link2 This KB Might help to understand who it's looking for DLLs and how to adjust the code?

– Seth – 2016-09-21T08:53:17.593

@Seth Sorry Seth, I'm using C++. – None – 2016-09-21T08:56:54.937

1It’s x86, by the way. x32 is only used for Linux’ x32 ABI. – Daniel B – 2016-09-21T10:22:45.320

Answers

0

The article on Dynamic-Link Library Search Order actually has something on how to change the way an application looks for a DLL as well. Namely it's referencing SetDllDirectory and LoadLibraryEx and even some more.

Seth

Posted 2016-09-21T08:22:37.467

Reputation: 7 657

0

There are multiple ways to solve the problem.

Build System

MSBuild has many features that cannot be controlled from the Visual Studio GUI. You can use variables almost everywhere, sometimes conditions, too.

You can declare conditional blocks in your .vcxproj file (which is just XML), like this:

<Choose>
  <When Condition="'$(Platform)' == 'Win32'">
    <ItemGroup>
      <Reference Include="SomeProject">
        <HintPath>..\Libraries\x86\SomeProject.dll</HintPath>
      </Reference>
    </ItemGroup>
  </When>
  <Otherwise>
    <ItemGroup>
      <Reference Include="SomeProject">
        <HintPath>..\Libraries\x64\SomeProject.dll</HintPath>
      </Reference>
    </ItemGroup>
  </Otherwise>
</Choose>

There are other solutions, like this one:

<Content Include="..\..\MyContentFiles\**\*.*">
  <Link>%(RecursiveDir)%(Filename)%(Extension)</Link>
  <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>

It doesn’t directly solve your problem, but gives additional insight into MSBuild’s capabilities.

I once had a working solution for a very similar problem (referencing native libraries from .NET with debug/release builds), but it remained at my previous employer.

If you feel MSBuild is too restricted, you can always create post-build tasks.

This solution can also be part of the dual-architecture separate directory solution mentioned below because it helps better automate the build process.

DLL Preloading

Call LoadLibrary or LoadLibraryEx and load the correct DLL manually. This is only possible if you have control before the DLL is automatically loaded by the OS loader.

Separate Directories

Put a launcher in the top level directory. Then, put the x86 and x64 builds into separate directories:

.\Launcher.exe
.\x64\Game.exe
.\x64\glew32.dll
.\x86\Game.exe
.\x86\glew32.dll

Search Path

IMHO this shouldn’t ever be necessary in a completely controlled environment.

Daniel B

Posted 2016-09-21T08:22:37.467

Reputation: 40 502

<When Condition="'$(Platform)' == 'Win32'"> What about when I want to use it on x64 and x86 macOS as well? – None – 2016-09-22T05:47:04.663

Hmm... The XML editing doesn't seem to work... – None – 2016-09-22T05:57:22.153

Visual Studio does not compile for macOS. You need a cross-platform build system for that. Also, "does not work" isn't a helpful problem description. – Daniel B – 2016-09-22T07:33:33.447

Well, I just had a brain malfunction on terms of macOS. :/ As for "doesn't seem to work", simple. *It doesn't do anything*. Nothing has changed. – None – 2016-09-22T07:38:26.000

So you're saying it doesn't copy the correct DLL to the output directory, right? Could you perhaps provide your .vcxproj file? Consider using a pastebin site if it's larger. Unfortunately, I don't have access to Visual Studio until next week. – Daniel B – 2016-09-22T09:36:15.617