dllimport

Parameterising DllImport for use in a C# application.

We have a supplier who provides a library for access to their hardware. Unfortunately, if you have multiple devices, you need to import their library multiple times, with different dll names. As a consequence, we have a metric ton of duplicated code, and I'm worried that it will soon become be a maintenance nightmare. What we have at th...

Read a Registry Key

I have a web application which is importing DLLs from the bin folder. const string dllpath = "Utility.dll"; [DllImport(dllpath)] Now what i want to do is first import the DLLs from a folder not in the current project but at some different location. The path of that folder is stored in a registry key. How should i do this? Than...

Does "fixed" really guarantee anything when passing pointers (ie int[]) to DLLs?

I tried searching for this but haven't found anything, however when passing an int[] into a native DLL function as a pointer, isn't there still the danger that the DLL could maintain a reference to the pointer, and then try to access it again after the "fixed" block has terminated? Wouldn't this cause memory access errors if the GC has ...

Catch DllNotFoundException from P/Invoke

Found post with a solution: http://stackoverflow.com/questions/970017/how-do-i-handle-a-failed-dllimport I'm writing an app that checks the OS version to do different things depending on whether the host is using a Vista-series or NT-series version of Windows. If Vista-series, it loads some DLLs (using DllImport), but doesn't use these ...

Access violation when calling external function (C++) from Delphi application

I've an external DLL written in C++. The piece below declares a struct type and a function, which, being given a pointer, fills a variable of this type: enum LimitType { NoLimit, PotLimit, FixedLimit }; struct SScraperState { char title[512]; unsigned int card_common[5]; unsigned int card_player[10][2]; unsigned int ca...

C# DllImport MFC Extension DLL & Name Mangling

I have a MFC extension DLL which I want to use in a C# application. The functions I'm exposing are C functions, i.e. I'm exporting them like this extern "C" { __declspec(dllexport) bool Initialize(); } The functions internally uses MFC classes, so what do I have to do to use the DLL in C# using P/Invoke. Secondly, I want to use func...

how to import/export dll and use its function

I am developing a project. I want to include a particular function from 7zip software to decompress data. Its a kinda exporting a particular function from 7zip software to my project. Should I do using DLL import and export or is there any other way? ...

SQL CLR Stored Procedure - Relative path for DllImport?

I have a C# project consisting of stored procedures that reference a win32 dll from another project in the solution. Currently, dllimport is using absolute paths to reference it. If I use a relative path instead, where is that path relative to once the clr stored procs assembly is loaded in sql server? ...

C++ from C#: C++ function (in a DLL) returning false, but C# thinks it's true!

Hi everyone, I'm writing a little C# app that calls a few functions in a C++ API. I have the C++ code building into a DLL, and the C# code calls the API using DllImport. (I am using a .DEF file for the C++ DLL so I don't need extern "C".) So far the API has one function, which currently does absolutely nothing: bool Foo() { return f...

gdi32.dll + unsafe code on 32+64Bit Windows

Which target cpu should I use to compile a .NET 3.5 app (developed on a 32 Bit System) that contains an assembly which uses [DllImport("gdi32.dll")] static extern IntPtr CopyEnhMetaFile(IntPtr hemfSrc, IntPtr hNULL); and which also has the option checked: "Allow unsafe code" because of the following code: ((byte*) dst.Scan0.ToPoin...

HELP linking library in DLL project

Hi there I have a DLL project in VS08 and it references some code from lib32eay.dll(openssl). When I build the dLL it works fine on my machine since I have openssl installed. However my clients dont have openssl installed so they will get error running my app as it references the openssl dll. How do I build a DLL project so it can includ...

Free unmanaged memory allocation from managed code

Hi! A .NET application calls C dll. The C code allocates memory for a char array and returns this array as result. The .NET applications gets this result as a string. The C code: extern "C" __declspec(dllexport) char* __cdecl Run() { char* result = (char*)malloc(100 * sizeof(char)); // fill the array with data return resul...

Installing msvcr90.dll easy way! (without C++ Redistributable Package)

My program is a converted python file to exe file. The problem with this exe file is that it does not run without python installed and it only needs mscvr90.dll! I don't want to install C++ Redistributable Package just for this dll file! That big fat package! If I copy this msvcr90.dll to my application folder it just won't work! The f...

Weird exception when trying to DllImport from kernel32

Hello! I have been using this particular function for months now, however today it stopped working. I can't imagine why, and I'm ruling nothing out, so if you have any ideas please do tell! I am loading function in such manner: [DllImport("kernel32")] private static extern int GetPrivateProfilestring(string section, string key, string...

Creating C++ Dll, and call it from C#

In my project I got a device which comes with C++ Sample codes. The codes are ok and the device is working as expected. But I need it to talk with my C# interface because all other devices are currently using C# interface. So I am planning to create a DLL Wrapper for the driver. I will create a C++ Library of my own (from source code w...

Specifying codepage for PInvoke string marshalling using C#

I am calling a DLL using PInvoke. The DLL's function returns a C string in codepage 437. Is there a way to have the .Net marshaling convert the string to unicode, or could someone suggest which parameters I should give to DllImport() and MarshalAs() and a conversion function to use in order to get an output in unicode? For reference, t...

Converting dllimport from vb6 to c# 3.5

Hi, I have some code in VB6 which imports a function from a dll, it uses the byVal and byRef keywords, I want to convert that code into C# 3.5. Is there going to be a problem with unicode encoding of the strings? Do I declare the varibles that are "byRef" in vb6 into "ref" varibles in the C# code? It seams that return value is entered...

Using vb.net dll in c++ - class is abstract

I created a vb.net dll which I am using in an unmanaged c++ project. When I try to create an object of the class, I am getting an error: cannot instantiate abstract class Why would my class be abstract? How can I modify it so that it won't be abstract? ...

MissingmethodException in C#SmartDevice project Can't find Pinvoke DLL

Hi, I am getting this error. when i am importing a dll from c++ appliaction. in my C# code i am importing this dll like this [DllImport("Assemble.dll", EntryPoint = "Settpdu")] static extern void Settpdu(byte[] VALUE); in my C++ code i am exporting this function like this extern "C" __declspec(dllexport) void Settpdu(BYTE * VAL...

DLLImport crash in Mono (linux): icall_wrapper_mono_marshal_free

I'm stucked with a problem with Mono Interop in Linux. I have a native shared library (made in Lazarus) that I need to use in a C# assembly. The shared library is used by a Mono C# assemblie on Windows and on Linux. The assembly loads the shared library via DllImport at runtime, and calls an exported function that generates a file and r...