Unload a DLL loaded using DllImport

The most reliable way to unload an unmanaged DLL from a process that got loaded by a [DllImport] pinvoke declaration is to load it yourself, again, by pinvoking LoadLibrary(). That gives you a reliable handle to the DLL and works correctly even if the module name of the DLL is ambiguous. It doesn’t have any … Read more

DLL load failed when importing PyQt5

It is because of missing Python3.dll (stub dll, that re-exports Python3x.dll functions, so that one version of extension can work for multiple versions of python). If your Python distro doesn’t bundle python3.dll, you can try one from WinPython (https://winpython.github.io/). At least the 2017/04/01 versions should have it. 1) Download WinPython (‘Zero’ version suffices; must be … Read more

Call function from DLL?

Depends on what type of DLL. Is this built in .NET ? if it is unmanaged code then here is an example otherwise the Answer from Rob will work. Unmanaged C++ dll example: using System; using System.Runtime.InteropServices; You may need to use DllImport [DllImport(@”C:\Cadence\SPB_16.5\tools\bin\mpsc.dll”)] static extern void mpscExit(); or [DllImport(“user32.dll”, CharSet = CharSet.Unicode)] public static … Read more

BadImageFormatException when loading 32 bit DLL, target is x86

From what I understand, an assembly specifically built for x86 and running in a 64-bit operating system can only load libraries built for x86 or a BadImageFormatException will be thrown. In a 64-bit OS, an assembly built for Any CPU or x64 will throw the same exception when trying to load an x86 library. So, … Read more

Why/when is __declspec( dllimport ) not needed?

__declspec(dllimport) is a client-side MSVC attribute that can be specified for imported code and data. It isn’t required for code. It is an optimization; a client-side compiler hint that a function call isn’t direct but imported. The imported function pointer for a function named foo() will be __imp_foo. Without the hint, a thunk is created … Read more

How to use [DllImport(“”)] in C#?

You can’t declare an extern local method inside of a method, or any other method with an attribute. Move your DLL import into the class: using System.Runtime.InteropServices; public class WindowHandling { [DllImport(“User32.dll”)] public static extern int SetForegroundWindow(IntPtr point); public void ActivateTargetApplication(string processName, List<string> barcodesList) { Process p = Process.Start(“notepad++.exe”); p.WaitForInputIdle(); IntPtr h = p.MainWindowHandle; SetForegroundWindow(h); … Read more

Specify the search path for DllImport in .NET

Call SetDllDirectory with your additional DLL paths before you call into the imported function for the first time. P/Invoke signature: [DllImport(“kernel32.dll”, SetLastError = true)] static extern bool SetDllDirectory(string lpPathName); To set more than one additional DLL search path, modify the PATH environment variable, e.g.: static void AddEnvironmentPaths(string[] paths) { string path = Environment.GetEnvironmentVariable(“PATH”) ?? string.Empty; … Read more

Using a 32bit or 64bit dll in C# DllImport

I’ve found the simplest way to do this is to import the two methods with different names, and calling the right one. The DLL won’t be loaded until the call is made so it’s fine: [DllImport(“MyDll32.dll”, EntryPoint = “Func1”, CallingConvention = CallingConvention.Cdecl)] private static extern int Func1_32(int var1, int var2); [DllImport(“MyDll64.dll”, EntryPoint = “Func1”, CallingConvention … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)