jump to navigation

Using C++ DLL in ASP.NET (C#) February 27, 2008

Posted by Sabs in Technology.
trackback

In my places in our program we have to use DLLImport, but it only works if the DLL is loaded. In windows application you can simply overcome this by placing the DLL in a designated folder, but in case of an ASP.NET application the code is compiled in an location which can’t be determined previously. It’s usually accessed from system32 ASP.NET folder and from a file created dynamically having some random naming. In .NET 1.1 the dynamic loading of dll using relflection was an available option, which we can also follow in ASP.NET 2.0 (see here) but we now also have another. We can load the DLL by calling Loadlibrary and then dll will be available to us in the memory and we can use it as we want.

Declaration of LoadLibrary in C#

[DllImport(“kernel32.dll”)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport(“kernel32.dll”)]
public static extern IntPtr FreeLibrary(IntPtr library);
// This is a sample Dll, whose function Function1 is required for us in the application.

[DllImport(“Sample.dll”, EntryPoint = “Function1”)]
public static extern void Function1(Byte[] inStr, Byte[] outStr, long in_Len);

The use of LoadLibrary in C#

// The physical path of the DLL

string DLLPath = HttpContext.Current.Request.PhysicalApplicationPath + “\References\Sample.dll”;
//Call LoadLibrary
IntPtr lib = LoadLibrary(DLLPath); // If the lib is zero, the code has failed

// Use the declared function, if required or any other stuff
Function1(System.Text.ASCIIEncoding.Unicode.GetBytes(Password.Text),
pwd, Password.Text.Length*2);
Don’t forget to free it

FreeLibrary(lib); // Check HRESULT, if that bothers you

Please do send me any enhancements, updates or suggestions regarding the above mentioned post.

Advertisements

Comments»

1. Keerthi Raju - September 8, 2008

could you please give me a small example of how to import a dll in C#.net web application.

also could you give me information of how to create a dll file. i want to make a dll file for some c++ programs.

2. Sabs - September 9, 2008

For Importing a dll in C#, you just have to add that dll in your application by using the “Add reference” option provided in the object explorer of Visual Studio. You can use that dll further in your code by simply adding the namespace in the page where you are using that dll.

Moreover, a dll is created for every project of visual studio when its output type is specified as class library.

Let me know, if I was able to answer your queries or not.

-Sabah

3. Keerthi Raju - September 9, 2008

yes you have answered my question. thank you so much.

4. Keerthi Raju - September 11, 2008

do you think i have to create a dll or create a class library for my c++ programs and then call these dll or class library from my web application,
or directly run c++ from my web application.

please help me.

5. Sabs - September 11, 2008

Dll would be a good option if you don’t want to disclose your code and its source. It also portable and easy to use. I think using a dll would be a logical option.

6. Keerthi Raju - September 11, 2008

thank you for suggesting me. could you please give me some links regarding the creation of dll file.

7. Keerthi Raju - September 11, 2008

also i have many classes needed in my program. so could i create the dll file which has many classes

8. Sabs - September 12, 2008
9. Keerthi Raju - September 16, 2008

I have created a small web application using c#.net that calls a function from a dll file created in c++

the following are the files i have created..

//CPPDLL.cpp file

#include “stdafx.h”

#include “CPPDLL.h”
#include

using namespace std;

namespace CPPDLL
{
double Class1::Add(double a, double b)
{
return a + b;
}
}

//CPPDLL.h file

#pragma once

using namespace System;

namespace CPPDLL {

public class Class1
{
// TODO: Add your methods for this class here.
public:
static __declspec(dllexport) double Add(double a, double b);
};
}

Now my c#.net code is as follows

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CPPDLL;
using System.Runtime.InteropServices;

public partial class _Default : System.Web.UI.Page
{
[DllImport(“kernel32.dll”)]
static extern IntPtr LoadLibrary(string lpFileName);

[DllImport(“kernel32.dll”)]
public static extern IntPtr FreeLibrary(IntPtr library);

[DllImport(“CPPDLL.dll”, EntryPoint=”Add”)]
private static extern double Add(double a, double b);

protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
IntPtr lib = LoadLibrary(“C:\\Data\\COMP7980\\LirbraryHelpFiles\\CPPDLL\\debug\\CPPDLL.dll”);
double sum = Add(12, 13);
FreeLibrary(lib);
}
}

After running the application when i press the button the following error is displayed…..

Unable to find an entry point named ‘Add’ in DLL ‘CPPDLL.dll’.

help me please in finding the problem…

10. Sabs - September 18, 2008

Hi,

Did you add your Dll from the “Add reference” option in your associated VS solution, before proceeding to the above defined step of yours?

-Sabah

11. palApanvamiBak - September 22, 2008

thanks much, dude

12. shalini - February 11, 2009

Hi,
I have added reference for my project .But still i am getting as cannot find entry point in the dll
Here is my code
[DllImport(“FaceDetection.dll”, EntryPoint = “WrapDetectFaces”)]
public static extern int WrapDetectFaces(Bitmap bmp, string path);
[DllImport(“FaceDetection.dll”, EntryPoint = “WrapGetFaceCordinates”)]
unsafe public static extern void WrapGetFaceCordinates(int index, int* lx, int* ly, int* rx, int* ry);

13. LITSSTYMN - April 22, 2009

hm… strange

14. recommended reading - June 2, 2013

This is really attention-grabbing, You’re an excessively professional blogger. I have joined your rss feed and sit up for seeking extra of your magnificent post. Additionally, I’ve
shared your web site in my social networks


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: