I want to use a C++ library for my asp.net website. I don't know how to make a .dll of the library and make it work. I tried making a dll and import it to asp.net. Do I need a to do something in the c++ code for the dll to work?
Source code: http://warp.povusers.org/FunctionParser/fparser.html
You could use:
[DllImport("dllname.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double FunctionParser();
Related
As the title says, I am using a managed dll (myManaged.dll) in my c# project (myProject). But myManaged.dll makes use of an unmanaged (c++) dll (myUnmanaged.dll).
My question is: Do I need to explicitly load myUnmanaged.dll in myProject along with the method declaration?
And if yes, then should it be like this:
[dllImport "myUnmanaged.dll", EntryPoint = "myMethod"]
public extern IntPtr myMethod(int myParam);
Thanks
No you don't need, as long as you are not going to use myUnmanaged.dll directly in myProject
I've created a C# application. Within this application I would like to use/run a C++ API from another project(the API is written in macro coding). I tried import the dll of that C++ project and tried to call a functions which belongs to that API. The problem is that it throws "unable to find method" error.
How can I run a C++ project in a C# project?
You can't add a native DLL as a reference to a managed project. You have 3 main options:
Make the native functions available with p/invoke.
Expose the native code through COM.
Compile the native code as a managed C++ assembly.
For any serious amount of code, option 3 is the most productive and effective approach.
If by "running", you mean a separate process:
Use the class System.Diagnostics.Process available in .NET:
myProcess.StartInfo.FileName = "notepad.exe";
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
Else, if you mean using a dll developed in C++, you can use Platform Invoke Services:
using System;
using System.Runtime.InteropServices;
class PlatformInvokeTest
{
//First param is of course either in your PATH, or an absolute path:
[DllImport("msvcrt.dll", EntryPoint="puts", CallingConvention=CallingConvention.Cdecl)]
public static extern int PutString(string c);
[DllImport("msvcrt.dll", CallingConvention=CallingConvention.Cdecl)]
internal static extern int _flushall();
public static void Main()
{
PutString("Test");
_flushall();
}
}
I have a old DLL (Borland Builder 2006 C++) which I want to use in .Net C# Visual Studio 2010. When I try to import the functions in VS always I get a StackOverflowException from Visual Studio. I've already read a lot stuff and the import seems to be easy. But I fail and don't see my error.
In the Borland DLL the Functions are exported as:
__declspec(dllexport) void TestFunc1() or
extern "C" __declspec(dllexport) void __stdcall TestFunc2()
The decorated names are (*.DEF file created with impdef and proved with dependency walker):
#TestFunc1$qqsv
TestFunc2
In Visual Studio I import in this way:
[DllImport("MyDllName.dll", EntryPoint = "#TestFunc1$qqsv", CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern void TestFunc1();
[DllImport("MyDllName.dll",CallingConvention = System.Runtime.InteropServices.CallingConvention.StdCall, CharSet = CharSet.Auto, SetLastError = true)]
public static extern void TestFunc2();
In booth cases a get a StackOverflowException from Visual studio, when I call:
MyImport_Unmanaged.TestFunc1() or
MyImport_Unmanaged.TestFunc2()
What is wrong ? Can anybody help me ?
Interesting when I import a old dll created with Visual Studio C++ the decorated name of the function is: _TestFunc1#0. The name is quite different to the Borland names but is works.
the correct syntax is:
extern "C" void __stdcall __declspec(dllexport) TestFunc1()
I had the same problem. After a lot of experiments I realized that the problem was not a syntax one. It was the fact that the C++ Builder DLL was using VCL Forms. I removed the forms and everything worked.
Yes, Panos seems to be right. I tried to P/Invoke an old BCB5 DLL with VCL and at the
first look it worked quite OK. But it silently has corrupted the C# program which manifests in strange exceptions a little bit later.
Not using the VCL in the Borland DLL was the only way to get it work. For us this means we have to convert our code to VisualStuidio which is on out todo-list anyway.
I got a DLL that I want to add to my C# project and I have some problems. First, my DLL is coded in C++ and I got an interface of one function to export it.
extern "C" __declspec(dllexport) char* sniff()
{
return ps.Sniff();
}
I have an instance "ps" that initialise a socket when the DLL is attached. The point is that I need to have this instance initialise when I call my exported function. My problem is when I import it in my C# project, my DLL is detached for no apparent reason and I can no longer call my exported function.
I use this syntax in my C# projet :
[DllImport(#"C:\Documents and Settings\Pat\Bureau\sniffoporn\Release\sniff.dll", EntryPoint = "sniff", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr sniff();
Some important things that I tryed :
Import the DLL in the reference of the project : Can't because it's not a COM component.
Manually import the DLL using LoadLibrary, FreeLibrary and GetProcAddress : Same result
Call my exported function in loop : Same thing but the DLL is attached for a little longer before detaching.
I would want to know why my DLL is detaching and how can I keep it attached for the rest of the process life.
Thanks a lot
Maybe an exception occurs in your DLL itself. Making it detach in Visual Studio to prevent your application from crashing. After all C# is managed code.
Try your same procedures with a different (dummy) dll with very simple functionality. If it still occurs then something with your method is wrong, otherwise with the DLL you are attempting to use.
I'm working on an ASP.NET project which generates screen shots. I want to use gdi32.dll in my project. How can I import it?
You need to use P/Invoke.
You could use a C++/CLI wrapper, depending on your requirements. It makes certain things easier.
If the dll is a system dll, you have to add programatically like this:
[DllImport("gdi32.dll", CharSet = CharSet.Ansi,
BestFitMapping = true, ThrowOnUnmappableChar = true)]
And make sure that the project (Solution Platform) is set to 32 or x86 and not to Any CPU. You can find more about it with a simple Google search. Happy
coding