Problem Overview
When developing a C# WPF application that entegrates with Teledyne DALSA Sapera LT cameras, you may encounter a runtime error stating that the DALSA.SaperaLT.SapClassBasic DLL cannot be loaded, often accompanied by the message "attempting to load an incorrect format program." This guide provides sollutions to resolve this common issue.
- Select the Correct DLL for Your Target Framework
The Sapera LT SDK provides different DLLs for different .NET frameworks. Using the wrong one will result in a loading failure.
- .NET Framework (e.g., .NET 4.8): Use
DALSA.SaperaLT.SapClassBasic.dll - .NET Core / .NET 5+ (e.g., .NET 6.0): Use
DALSA.SaperaLT.SapClassBasic.Core.dll
Ensure the correct DLL is referenced for your project's target framework.
- Configuring Multi-targeting in a Single Project
If your project needs to support multiple frameworks (e.g., both .NET Framework and .NET 6), you can configure the .csproj file to conditionally reference the appropriate DLL.
<PropertyGroup>
<OutputType>Library</OutputType>
<TargetFrameworks>net48;net6.0-windows</TargetFrameworks>
<UseWPF>true</UseWPF>
<Platforms>AnyCPU;x64</Platforms>
</PropertyGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net48' ">
<Reference Include="DALSA.SaperaLT.SapClassBasic">
<HintPath>C:\Path\To\SDK\Bin\DALSA.SaperaLT.SapClassBasic.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net6.0-windows' ">
<Reference Include="DALSA.SaperaLT.SapClassBasic.Core">
<HintPath>C:\Path\To\SDK\Bin\DALSA.SaperaLT.SapClassBasic.Core.dll</HintPath>
</Reference>
</ItemGroup>
- Preventing DLL Embedding by Fody/Costura
Tools like Fody's Costura can embed referenced DLLs into your main executable. This can cause issues with native dependencies. Ensure these specific assemblies are excluded from embedding.
<Costura IncludeRuntimeReferences='false'>
<ExcludeAssemblies>
DALSA.SaperaLT.SapClassBasic
DALSA.SaperaLT.SapClassBasic.Core
</ExcludeAssemblies>
</Costura>
- Ensure SDK Version Consistency
The application may run correctly on your development machine but fail on another due to a mismatch in the installed Sapera LT SDK version. Verify that the target machine has the same version of the Teledyne DALSA Sapera LT SDK installed as your development environment.
- Setting System Paths
If the application still fails to find the DLL, you can make it available to the system.
- Set the environment variable
PATHto include the directory containing the required DLL. - Alternatively, copy the appropriate DLL to the system folders:
DALSA.SaperaLT.SapClassBasic.dlltoC:\Windows\System32DALSA.SaperaLT.SapClassBasic.Core.dlltoC:\Windows\SysWOW64