1

I'm trying to load my 32/64 bit native dll's using the same DllImport call.

Directory structure:

root:

  • application.exe
  • /win64/
    • stb_image.dll
  • /win32/
    • stb_image.dll

I tried using this solution but as you can see no succes.

For example this function call:

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

But it doesn't work as I get an DllNotFoundException.

The way I'm using 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);
}

It's the first call in my program so it should've set the correct path. It also returns true.

But if I put the (in my case) 64 bit native dll in the exe's directory then it works even tho I set the DllDirectory to a different path.

Any help?

Community
  • 1
  • 1
Matthiee
  • 431
  • 4
  • 14
  • did you set the reference path? – Jacobr365 Mar 22 '16 at 13:18
  • @Jacobr365 I'm not sure why I should try that as I'm having trouble loading native library at runtime. Anyways I tried adding the folders but they still don't work. I edited my post so now you can see the directory structure. – Matthiee Mar 23 '16 at 22:36
  • Assuming Visual Studio, try sticking a copy of the dll into the project folder somewhere. Then right click on the project in solution explorer, choose Add->Reference, then browse and select the stb_image and hit ok. – Jacobr365 Mar 24 '16 at 13:10
  • It's a native dll I can't reference it, I have to call it using DllImport. – Matthiee Mar 25 '16 at 14:03

2 Answers2

0

Something went wrong with kernel32 development. But there is a workaround via setting PATH environment variable for the current process session.

string dllFolder = "<somewhere>";

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

After registering the folder in PATH variable, LoadLibrary works flawlessly and loads Dll(s) from the path specified.

Alfishe
  • 3,430
  • 1
  • 25
  • 19
-1

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

good luck

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 22 '22 at 00:40