2016-03-22 17 views
1

同じDllImport呼び出しを使用して32/64ビットのネイティブdllをロードしようとしています。SetDllDirectoryが機能しない(DllNotFoundException)

ディレクトリ構造:

ルート:

  • application.exe
  • /Win64の/
    • stb_image.dll
  • /win32の/
    • stb_image.dll

あなたは何も大成功を見ることができないように私はthis solutionを使用してみましたけど。例えば

この関数呼び出し:

[DllImport("stb_image.dll")] 
private static extern IntPtr stbi_load(string filename, ref int x, ref int y, ref int n, int req_comp); 

しかし、私はDllNotFoundExceptionを取得するとして、それは動作しません。

私はSetDllDirectoryを使用しています方法:

static class Program 
{ 
    /// <summary> 
    /// The main entry point for the application. 
    /// </summary> 
    [STAThread] 
    static void Main() 
    { 
     SetUnmanagedDllDirectory(); 

     GameConfiguration config = new GameConfiguration(); 
     config.FPSTarget = 60; 
     config.FixedFPS = true; 
     config.Resizable = false; 

     TestGame game = new TestGame(config); 
     game.Run(); 
    } 

    public static void SetUnmanagedDllDirectory() 
    { 
     string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); 
     path = Path.Combine(path, IntPtr.Size == 8 ? "win64 " : "win32"); 
     if (!SetDllDirectory(path)) throw new System.ComponentModel.Win32Exception(); 
    } 

    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)] 
    private static extern bool SetDllDirectory(string path); 
} 

それは正しいパスを設定している必要がありますので、それは私のプログラムの最初の呼び出しです。 また、trueを返します。

しかし、(私の場合)exeのディレクトリに64ビットのネイティブdllを置くと、それは別のパスにDllDirectoryを設定してもthoで動作します。

助けが必要ですか?

+0

参照パスを設定しましたか? – Jacobr365

+0

@ Jacobr365なぜ私は実行時にネイティブライブラリを読み込むのに問題があるのですか?とにかく私はフォルダを追加しようとしましたが、まだ動作しません。 私は自分の投稿を編集したので、今度はディレクトリ構造を見ることができます。 – Matthiee

+0

Visual Studioを想定して、dllのコピーをどこかのプロジェクトフォルダに貼り付けてみてください。次に、ソリューションエクスプローラでプロジェクトを右クリックして、「Add-> Reference」を選択し、stb_imageを参照して選択してokを押します。 – Jacobr365

答えて

0

kernel32開発で何か問題が発生しました。しかし、現在のプロセスセッションのPATH環境変数を設定することで回避策があります。

string dllFolder = "<somewhere>"; 

string path = Environment.GetEnvironmentVariable("PATH"); 
Environment.SetEnvironmentVariable("PATH", dllFolder + ";" + path); 

PATH変数にフォルダを登録すると、LoadLibraryは完璧に動作し、指定されたパスからDllを読み込みます。

関連する問題