dllimport

Using C++ DLL in C# project

偶尔善良 提交于 2019-12-01 02:12:15
问题 I got a C++ dll which has to be integrated in a C# project. I think I found the correct way to do it, but calling the dll gives me this error: System.BadImageFormatException: An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B) This is the function in the dll: extern long FAR PASCAL convert (LPSTR filename); And this is the code I'm using in C# namespace Test{ public partial class Form1 : Form { [DllImport("convert.dll", SetLastError = true)]

inconsistent dll linkage & definition of dllimport static data member not allowed

人盡茶涼 提交于 2019-12-01 01:23:57
问题 Assuming I have these two files: Header.h class DLL ExportClass{ public: ExportClass(); static int test; }; Source.cpp #ifdef EXPORT #define DLL __declspec(dllexport) #else #define DLL __declspec(dllimport) #endif #include "Header.h" int ExportClass::test = 0; ExportClass::ExportClass(){ } And I won't define EXPORT (to import a already exported class with a static member), why do I get these warnings: 1>source.cpp(11): warning C4273: 'test' : inconsistent dll linkage 1> header.h(4) : see

A call to PInvoke function has unbalanced the stack when including a C DLL into C#

ε祈祈猫儿з 提交于 2019-11-30 23:42:35
I have written a C DLL and some C# code to test including this DLL and executing functions from it. I am not too familiar with this process, and am receiving a PInvokeStackImbalance exception whenever my DLL function is called from the C# source code. The code is as follows (I have commented most code out to isolate this problem): C# Inclusion code: using System; using System.Runtime.InteropServices; using System.IO; namespace TestConsoleGrids { class Program { [DllImport("LibNonthreaded.dll", EntryPoint = "process")] public unsafe static extern void process( long high, long low); static void

“The specified procedure could not be found” error with .NET 4

≯℡__Kan透↙ 提交于 2019-11-30 22:24:04
I am developing on a 64-bit Windows 7 box with Visual Studio 2012 (11.0.51106.01 Update 1). I have a Support project that compiles some C code into a (32-bit) DLL. In my header I have: #define RET_TYPE(type) _declspec(dllexport) type __stdcall RET_TYPE(int) test_dll_call(int invar); In my C file I have: RET_TYPE(int) test_dll_call(int invar) { int retVal = 4 * invar; return retVal; } I also have a (32-bit) WPF C# application that loads the DLL inside of a class as follows: [DllImport("MyDll.dll", CharSet = CharSet.Ansi, BestFitMapping = true, ThrowOnUnmappableChar = true)] public static extern

DllImport or LoadLibrary for best performance

自古美人都是妖i 提交于 2019-11-30 14:11:14
I have external .DLL file with fast assembler code inside. What is the best way to call functions in this .DLL file to get best performance? Your DLL might be in python or c++, whatever , do the same as follow. This is your DLL file in C++. header: extern "C" __declspec(dllexport) int MultiplyByTen(int numberToMultiply); Source code file #include "DynamicDLLToCall.h" int MultiplyByTen(int numberToMultiply) { int returnValue = numberToMultiply * 10; return returnValue; } Take a look at the following C# code: static class NativeMethods { [DllImport("kernel32.dll")] public static extern IntPtr

Setting dllimport programmatically in C#

冷暖自知 提交于 2019-11-30 13:21:24
问题 I am using DllImport in my solution. My problem is that I have two versions of the same DLL one built for 32 bit and another for 64 bit. They both expose the same functions with identical names and identical signatures. My problem is that I have to use two static methods which expose these and then at run time use IntPtr size to determine the correct one to invoke. private static class Ccf_32 { [DllImport(myDllName32)] public static extern int func1(); } private static class Ccf_64 {

Where does DLLImport look for unmanaged DLLs?

心已入冬 提交于 2019-11-30 12:47:11
A quick question: When declaring the DLLImport Attribute in .Net, where does the runtime look to resolve that DLL dependency? Would I have to dump the DLL and all of its dependencies in the bin folder? Martin I think it is working like LoadLibrary. Maybe Windows path searching in LoadLibrary with manifest will be helpfull. Edit Dynamic-Link Library Search Order is probably what you want. Generally, you'd expect it to look at whatever's defined in the user's Path. To find out what this contains, from a command prompt, type echo %PATH% [Enter] 来源: https://stackoverflow.com/questions/268627/where

Why use DllImport Attribute as apposed to adding a reference?

為{幸葍}努か 提交于 2019-11-30 12:14:11
I've seen a couple of examples such as this: [DllImport("user32.dll")] static extern bool TranslateMessage([In] ref Message lpMsg); [DllImport("user32.dll")] static extern IntPtr DispatchMessage([In] ref Message lpmsg); But, what I don't understand is why someone would do that as apposed to just referencing the DLL like they do other libraries? The MSDN states: "The DllImport attribute is very useful when reusing existing unmanaged code in a managed application. For instance, your managed application might need to make calls to the unmanaged WIN32 API." But, is that saying it is not useful to

How to get char** using C#? [duplicate]

孤者浪人 提交于 2019-11-30 12:06:58
This question already has an answer here: Marshal an array of strings from C# to C code using p/invoke 3 answers C# call C++ DLL passing pointer-to-pointer argument 3 answers I need to pass an argument to an unsafe DllImported function in the form of: [DllImport("third_party.dll")] private static extern unsafe int start(int argc, char** argv); I'm assuming it's an array of strings. However when I try to do the following, I get 'Cannot convert from string[] to char**' error. How do I go about getting this to work? Thanks. string[] argv = new string[] { }; start(0, argv); EDIT 1: The question

Generate C# DLLImport declarations from a native dll

送分小仙女□ 提交于 2019-11-30 11:11:29
Do you know a soft which automatically generates C# code (with [DllImport] attributes in .cs) from a native DLL in order to use this DLL in a C# code? Checkout the P/Invoke Interop Assistant : In marshalling, there are a bunch of attributes and rules. Understanding all those attributes and rules seem a bit daunting. In order to make developing work more efficient and easier on those attributes and the rules, P/Invoke Interop Assistant comes out. It is a toolkit that helps developers to efficiently convert from C to managed P/Invoke signatures or verse visa. This is conceptually similar to