Questions tagged [dllimport]

Use this tag for questions about importing functions, data or objects from DLLs (Dynamic-link libraries).

Because DLL export table does not contain type information, this information must be provided, to make it possible to use functions, data or objects kept inside DLLs. There are several ways to achieve this.

  • __declspec( dllimport ) is a C and C++ compiler directive. When used in the header file provided by the library, it explicitly defines DLL interface to the client.

  • The System.Runtime.InteropServices.DllImportAttribute class allows managed .NET code to reference a Windows DLL through the magic of Platform Invocation (P/Invoke for short). The System.Runtime.InteropServices namespace contains a wealth of tools to allow .NET to efficiently and effectively use legacy DLLs.

2285 questions
175
votes
10 answers

How can I specify a [DllImport] path at runtime?

In fact, I got a C++ (working) DLL that I want to import into my C# project to call it's functions. It does work when I specify the full path to the DLL, like this : string str =…
Jsncrdnl
  • 3,005
  • 5
  • 28
  • 43
98
votes
4 answers

Is there any native DLL export functions viewer?

Is there any free native Windows DLL export functions viewer, which shows the function name, and a list of their parameters?
Alon Gubkin
  • 56,458
  • 54
  • 195
  • 288
92
votes
5 answers

Embedding unmanaged dll into a managed C# dll

I have a managed C# dll that uses an unmanaged C++ dll using DLLImport. All is working great. However, I want to embed that unmanaged DLL inside my managed DLL as explain by Microsoft…
Laurent
80
votes
3 answers

What is [DllImport("QCall")]?

Many methods in the .Net library are implemented in native code. Those that come from the framework itself are marked with [MethodImpl(MethodImplOptions.InternalCall)]. Those that come from some unmanaged DLL are marked with [DllImport] (e.g.…
svick
  • 236,525
  • 50
  • 385
  • 514
77
votes
10 answers

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

Here is the situation, I'm using a C based dll in my dot.net application. There are 2 dlls, one is 32bit called MyDll32.dll and the other is a 64bit version called MyDll64.dll. There is a static variable holding the DLL file name: string…
Gilad
  • 2,876
  • 5
  • 29
  • 40
58
votes
4 answers

Specify the search path for DllImport in .NET

Is there a way to specify the paths to be searched for a given assembly that is imported with DllImport? [DllImport("MyDll.dll")] static extern void Func(); This will search for the dll in the app dir and in the PATH environment variable. But at…
Stefan
  • 4,166
  • 3
  • 33
  • 47
58
votes
2 answers

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

I found a lot of questions about it, but no one explains how I can use this. I have this: using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Windows.Forms; using…
ThomasFey
  • 736
  • 1
  • 5
  • 11
45
votes
2 answers

Why/when is __declspec( dllimport ) not needed?

In a project using a server.dll and a client.exe, I have dllexported a server symbol from the server dll, and not dllimported it into the client exe. Still, the application links, and starts, without any problem. Is dllimport not needed,…
xtofl
  • 40,723
  • 12
  • 105
  • 192
43
votes
7 answers

Calling functions in a DLL from C++

I have a solution in VS 2008 with 2 projects in it. One is a DLL written in C++ and the other is a simple C++ console application created from a blank project. I would like know how to call the functions in the DLL from the application. Assume I am…
QueueHammer
  • 10,515
  • 12
  • 67
  • 91
40
votes
12 answers

BadImageFormatException when loading 32 bit DLL, target is x86

I have a DLL (FreeType) which is certainly 32-bit (header: IMAGE_FILE_MACHINE_I386). I want to use it from C# code, using DllImport. Target of my application is x86, IntPtr.Size is 4, process is 32-bit. But I get BadImageFormatException (Exception…
Coder
  • 483
  • 1
  • 4
  • 7
40
votes
5 answers

PInvokeStackImbalance C# call to unmanaged C++ function

After switching to VS2010, the managed debug assistant is displaying an error about an unbalanced stack from a call to an unmanaged C++ function from a C# application. The usuals suspects don't seem to be causing the issue. Is there something else I…
user287498
  • 401
  • 1
  • 4
  • 4
39
votes
5 answers

Call function from DLL?

I'm new to C# and I'm trying to learn to usage of DLLs. I'm trying to wrap my objects in a DLL, and then use it in my program. public class Foo // its in the DLL { public void Bar() { SomeMethodInMyProgram(); } } So I try to pack…
Dominik Antal
  • 3,281
  • 4
  • 34
  • 50
39
votes
28 answers

DLL load failed when importing PyQt5

I have installed PyQt5 on windows platform and and getting an importError: DLL load failed. I have installed pyqt5 using the command pip3 install pyqt5 Successfully installed pyqt5-5.8.1 My Python version is as follows: Python 3.5.2 |Anaconda…
gdRow
  • 497
  • 1
  • 5
  • 10
38
votes
5 answers

Unload a DLL loaded using DllImport

How do I unload a DLL which has been loaded using DllImport in C#?
subbu
  • 3,229
  • 13
  • 49
  • 70
30
votes
5 answers

How to use in VB.NET?

How should I DLLImport things in VB.NET? An example would be: _ Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer)…
MilMike
  • 12,571
  • 15
  • 65
  • 82
1
2 3
99 100