dllimport

How to dllimport in Microsoft Visual C++

五迷三道 提交于 2019-11-29 07:20:15
I have a DLL and I would like to use some of its functions. #include <iostream> using namespace std; extern "C" __declspec(dllimport) int Initialize(char* localPort, char* adminServerName, int rpcTimeout); int main() { int res = Initialize("7864", "6000@kabc", 10000); return 0; } I don't have the DLL's .lib file, so is there anyway I can link to it. One thing that comes to my mind is to use the LoadLibrary function and then use the GetProcAddress(). Is there any other way? When I compile the following code error LNK2019: unresolved external symbol _ imp _Initialize referenced in function _main

EntryPointNotFoundException when binding C++ dll in C#

为君一笑 提交于 2019-11-29 06:35:33
I try to bind a simple c++ dll shown in http://msdn.microsoft.com/en-us/library/ms235636.aspx in my c# console app, but I get a EntryPointNotFoundException for Add within dll at runtime. My test class is namespace BindingCppDllExample { public class BindingDllClass { [DllImport("MathFuncsDll.dll")] public static extern double Add(double a, double b); } public class Program { public static void Main(string[] args) { double a = 2.3; double b = 3.8; double c = BindingDllClass.Add(a, b); Console.WriteLine(string.Format("{0} + {1} = {2}", a, b, c)); } } } What is not correct? You could try

DllImport - PreserverSig and SetLastError attributes

那年仲夏 提交于 2019-11-29 04:09:28
On the MSDN I've found the following description for the two attributes: PreserveSig Set the PreserveSig field to true to directly translate unmanaged signatures with HRESULT or retval values; set it to false to automatically convert HRESULT or retval values to exceptions. By default, the PreserveSig field is true. SetLastError Enables the caller to use the Marshal.GetLastWin32Error API function to determine whether an error occurred while executing the method. In Visual Basic, the default is true (which adds some overhead); in C# and C++, the default is false. My question is: How these two

urlmon.dll FindMimeFromData() works perfectly on 64bit desktop/console but generates errors on ASP.NET

久未见 提交于 2019-11-29 04:04:24
I am creating a library of utilities to be used both in desktop environment in a web environment. It contains several features that I believe are often repeated in my applications, including utility to get the mime type of a file by its content (not the extension). The files that I'll have to check are the most common (jpg, png, pdf, txt) so I chose to use the external method FindMimeFromData (link above) Using .NET, how can you find the mime type of a file based on the file signature not the extension The method works well, except for the two incorrect mime type of JPG ( image/pjpg ) and PNG

Passing byte array between C++ and C# ByRef raises AccessViolationException

家住魔仙堡 提交于 2019-11-29 03:19:46
问题 I am trying to create a Win32 DLL exposes some functions which are called in C# as follows __declspec(dllexport) int GetData(unsigned char* *data, int* size) { try { int tlen = 3; unsigned char* tchr = new unsigned char[5]; tchr[0] = 'a'; tchr[1] = 'b'; tchr[2] = 'c'; *size = tlen; *data = tchr; return 1; } catch (char *p) { return 0; } } And on C# side [DllImport("MyDll.dll")] static extern int GetData(ref byte[] data, ref int size); static void Main() { try { int hr = 0; byte[] gData = null

How to use <DllImport> in VB.NET?

旧城冷巷雨未停 提交于 2019-11-29 02:57:37
How should I DLLImport things in VB.NET ? An example would be: <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function GetWindowText(ByVal hwnd As IntPtr, ByVal lpString As StringBuilder, ByVal cch As Integer) As Integer End Function If I put it inside a Class or somewhere else, I get "DLLimport is not defined" I am using Visual Studio 2008 Professional You have to add Imports System.Runtime.InteropServices to the top of your source file. Alternatively, you can fully qualify attribute name: <System.Runtime.InteropService.DllImport("user32.dll", _

Call function from DLL?

南楼画角 提交于 2019-11-29 01:04:58
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 int ID; public void Bar() { SomeMethodInMyProgram(); } } So I try to pack this to a DLL but I can't, because compiler doesn't know what the SomeMethodInMyProgram() is. I would like to use it like: class Program // my program, using DLL { static void Main(string[] args) { Foo test = new Foo(); test.Bar(); } } Add the DLL via the solution explorer - right click on references --> add reference then "Browse" to your DLL - then it

C# Marshalling double* from C++ DLL?

ⅰ亾dé卋堺 提交于 2019-11-28 23:36:58
I have a C++ DLL with an exported function: extern "C" __declspec(dllexport) double* fft(double* dataReal, double* dataImag) { [...] } The function calculates the FFT of the two double arrays (real and imaginary) an returns a single double array with the real an imaginary components interleaved: { Re, Im, Re, Im, ... } I'm not sure how to call this function in C#. What I'm doing is: [DllImport("fft.dll")] static extern double[] fft(double[] dataReal, double[] dataImag); and when I test it like this: double[] foo = fft(new double[] { 1, 2, 3, 4 }, new double[] { 0, 0, 0, 0 }); I get a

C# Struct No Parameterless Constructor? See what I need to accomplish

雨燕双飞 提交于 2019-11-28 23:26:49
I am using a struct to pass to an unmanaged DLL as so - [StructLayout(LayoutKind.Sequential)] public struct valTable { public byte type; public byte map; public byte spare1; public byte spare2; public int par; public int min; public byte[] name; public valTable() { name = new byte[24]; } } The code above will not compile because VS 2005 will complain that "Structs cannot contain explicit parameterless constructors". In order to pass this struct to my DLL, I have to pass an array of struct's like so valTable[] val = new valTable[281]; What I would like to do is when I say new , the constructor

DllImport vs Declare in VB.NET

末鹿安然 提交于 2019-11-28 20:40:51
I notice in the MSDN documentation that there are multiple ways to declare a reference to a function in an external DLL from within a VB.NET program. The confusing thing is that MSDN claims that you can only use the DllImportAttribute class with Shared Function prototypes " in rare cases ", but I couldn't find the explanation for this statement, while you can simply use the Declare keyword instead. Why are these different, and where would I appropriately use each case? JaredPar Declare is really an attempt to maintain a P/Invoke syntax which would be more familiar to Visual Basic 6.0 users