dllimport

What is [DllImport(“QCall”)]?

六眼飞鱼酱① 提交于 2019-11-26 19:21:52
问题 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. [DllImport("kernel32.dll")] ). So far nothing unusual. But while writing answer for another question, I discovered there are many methods marked with [DllImport("QCall")] . They seem to be internal implementation of .Net (e.g. GC._Collect() ). My

Why/when is __declspec( dllimport ) not needed?

泄露秘密 提交于 2019-11-26 18:49:25
In a project using a server.dll and a client.exe, I have dllexport ed a server symbol from the server dll, and not dllimport ed it into the client exe. Still, the application links, and starts, without any problem. Is dllimport not needed, then??? Details: I have this 'server' dll: // server.h #ifdef SERVER_EXPORTS #define SERVER_API __declspec(dllexport) #else #define SERVER_API // =====> not using dllimport! #endif class SERVER_API CServer { static long s; public: CServer(); }; // server.cpp CServer::CServer(){} long CServer::s; and this client executable: #include <server.h> int main() {

CPU Architecture Independent P/Invoke: Can the DllName or path be “dynamic”?

大兔子大兔子 提交于 2019-11-26 18:24:15
问题 Is there a way to have the particular DLL referenced by a P/Invoke (DllImport) signature depend on the CPU architecture? I'm working on an application that loads a large number of method signatures from a native dll from a third party vendor, in this case the user-space interface DLL to a piece of hardware. That vendor has now started supplying both x86 and x64 versions of the DLL now, and I think my app would benefit from running as a 64bit process. Except for this one DLL, everything is

Calling functions in a DLL from C++

时间秒杀一切 提交于 2019-11-26 18:01:03
问题 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 starting with a blank C++ project and that I want to call a function called int IsolatedFunction(int someParam) How do I call it? 回答1: There are many ways to do this but I think one of the easiest options is to link the application to the DLL at

How to pass strings from C# to C++ (and from C++ to C#) using DLLImport?

纵饮孤独 提交于 2019-11-26 16:24:34
问题 I've been trying to send a string to/from C# to/from C++ for a long time but didn't manage to get it working yet ... So my question is simple : Does anyone know some way to send a string from C# to C++ and from C++ to C# ? (Some sample code would be helpful) 回答1: Passing string from C# to C++ should be straight forward. PInvoke will manage the conversion for you. Geting string from C++ to C# can be done using a StringBuilder. You need to get the length of the string in order to create a

BadImageFormatException when loading 32 bit DLL, target is x86

混江龙づ霸主 提交于 2019-11-26 15:30:48
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 from HRESULT: 0x8007000B). What can be wrong? Of course I use 64-bit Windows 7. 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

Linking error LNK2019 in MSVC, unresolved symbols with __imp__ prefix, but should be from static lib

跟風遠走 提交于 2019-11-26 14:15:55
问题 I'm running into linking problems in MSVC for a project that I wrote for g++. Here's the problem: I build libssh as a static library as part of my application, adding the target in cmake with add_library(ssh_static STATIC $libssh_SRCS) Libssh is in C, so I have 'extern "C" {...}' wrapping the includes in my c++ sources. I then link the ssh_static target to my executable, sshconnectiontest, with target_link_libraries(sshconnectiontest ... ssh_static ...) This all works fine in linux with gcc,

Specify the search path for DllImport in .NET

梦想与她 提交于 2019-11-26 12:16:22
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 times the dll will be placed elsewhere. Can this information be specified in app.config or manifest file to avoid dynamic loading and dynamic invocation? Chris Schmich 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)]

Unload a DLL loaded using DllImport

爱⌒轻易说出口 提交于 2019-11-26 12:14:00
问题 How do I unload a DLL which has been loaded using DllImport in C#? 回答1: 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 affect at runtime, other than the Windows loader increasing the internal reference count on the DLL from 1 to 2. You

Change C# DllImport target code depending on x64/x86

*爱你&永不变心* 提交于 2019-11-26 11:52:19
I have an external c++ dll to import using DLLImport. If my application is compiling in x64 I need to import the x64 version of this dll, if it is an x86 build, I need the x86 dll. What is the best way to achieve this? Ideally, I'd like some preprocessor directive, but I understand this doesn't work in c#? More info: the DLL is being imported by a project which is set to AnyCPU. A parent project is the one which determines whether the application compiles as x64 or x86. We compile both versions for different customers - and I want to share the child project in both versions. This is primarily