DLL Hijack/Preloading issue in WPF application

44 views Asked by At

I have created wpf application using visual studio 2019(.Net Framework 4.7.2). Builded it. In bin/debug folder I have placed one dll(cryptbase.dll) which is having a simple message box. When ever I am running my exe of wpf application from debug folder first its executing cryptbase.dll and showing message box and main application is not launching at all.

This is a simple demonstration of my problem. I have tried mutiple ways to fix this dll hijacking issue. Can some please suggest some approach so that it should run my application first.

I have tried to change dll search order also, but that code is not getting hit and dll is getting executing before reaching there.

public partial class App : Application
{

    private const int LOAD_LIBRARY_SEARCH_DEFAULT_DIRS = 0x00000800;


    [DllImport("kernel32.dll", SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool SetDefaultDllDirectories(int directoryFlags);

    public App()
    {
        if (!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_DEFAULT_DIRS))
        {
            Console.WriteLine($"Failed to exclude application directory from DLL directories, error code: {Marshal.GetLastWin32Error()}");
        }
    }
}

Can someone please let me know if there is any fix for this?

0

There are 0 answers