普通はx64 x86フォルダ固定だけど、どうしても違うフォルダにしたい場合の設定方法
コード(vb)
Imports System.Runtime.InteropServices
....
' --- Win32 API 宣言 ---
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function SetDllDirectory(lpPathName As String) As Boolean
End Function
' 対応OSなら安全な検索順序へ(Windows 8+)
<DllImport("kernel32.dll", SetLastError:=True)>
Private Shared Function SetDefaultDllDirectories(dwDirectoryFlags As UInteger) As Boolean
End Function
<DllImport("kernel32.dll", CharSet:=CharSet.Unicode, SetLastError:=True)>
Private Shared Function AddDllDirectory(NewDirectory As String) As IntPtr
End Function
Private Const LOAD_LIBRARY_SEARCH_DEFAULT_DIRS As UInteger = &H1000UI
Private Const LOAD_LIBRARY_SEARCH_USER_DIRS As UInteger = &H400UI
...
Sub Main()
' --- 1) SQLite.Interop.dll の検索パスを最初に通す ---
Dim baseDir = AppDomain.CurrentDomain.BaseDirectory
Dim archFolder = If(IntPtr.Size = 8, "OrgFolder(x64)", "OrgFolder(x86)")
Dim nativeDir = Path.Combine(baseDir, archFolder)
Try
Dim okDefault As Boolean = False
Try
okDefault = SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS Or LOAD_LIBRARY_SEARCH_USER_DIRS)
Catch
' 旧OS等で未実装なら握りつぶし
End Try
If Directory.Exists(nativeDir) Then
If okDefault Then
' Win8+:ユーザーディレクトリに追加入れ
Dim p = AddDllDirectory(nativeDir)
If p = IntPtr.Zero Then
' 念のためフォールバック
SetDllDirectory(nativeDir)
End If
Else
' 旧OS:従来APIにフォールバック
SetDllDirectory(nativeDir)
End If
Else
MessageBox.Show($"ネイティブDLLフォルダが見つかりません: {nativeDir}",
"起動警告", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End If
Catch ex As Exception
MessageBox.Show("DLL検索パス設定でエラー: " & ex.Message,
"エラー", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
...vbproj
...
<Target Name="RelocateSQLiteInterop" AfterTargets="CopySQLiteInteropFiles">
<PropertyGroup>
<SQLiteInteropX86Source>$(TargetDir)x86\SQLite.Interop.dll</SQLiteInteropX86Source>
<SQLiteInteropX64Source>$(TargetDir)x64\SQLite.Interop.dll</SQLiteInteropX64Source>
<SQLiteInteropX86Destination>$(TargetDir)OrgFolder(x86)\SQLite.Interop.dll</SQLiteInteropX86Destination>
<SQLiteInteropX64Destination>$(TargetDir)OrgFolder(x64)\SQLite.Interop.dll</SQLiteInteropX64Destination>
</PropertyGroup>
<MakeDir Directories="$(TargetDir)OrgFolder(x86)" Condition="Exists('$(SQLiteInteropX86Source)')" />
<MakeDir Directories="$(TargetDir)OrgFolder(x64)" Condition="Exists('$(SQLiteInteropX64Source)')" />
<Copy SourceFiles="$(SQLiteInteropX86Source)" DestinationFiles="$(SQLiteInteropX86Destination)" SkipUnchangedFiles="true" Condition="Exists('$(SQLiteInteropX86Source)')" />
<Copy SourceFiles="$(SQLiteInteropX64Source)" DestinationFiles="$(SQLiteInteropX64Destination)" SkipUnchangedFiles="true" Condition="Exists('$(SQLiteInteropX64Source)')" />
<Delete Files="$(SQLiteInteropX86Source)" Condition="Exists('$(SQLiteInteropX86Source)')" />
<Delete Files="$(SQLiteInteropX64Source)" Condition="Exists('$(SQLiteInteropX64Source)')" />
<RemoveDir Directories="$(TargetDir)x86" Condition="Exists('$(TargetDir)x86')" />
<RemoveDir Directories="$(TargetDir)x64" Condition="Exists('$(TargetDir)x64')" />
</Target>
...