473,406 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Using c++ dll in c#

Hello!

I am trying to use a DLL that contains c++ functions in c# but I have
not been able to make it, this DLL is of a software called LOOX Maker,
which is a graphic editor that has many options reason why I want to
reuse that code so I not to have to program it again, also I have source
code of this program, I have tried several things but I have not
obtained it.
Somebody that has more experience in this type of things can help me?
I can send the project to you where I am trying to use it and also the
DLL and source code of LOOX Maker, I hope that somebody can help me.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #1
9 3117
Juan Martinez wrote:
Hello!

I am trying to use a DLL that contains c++ functions in c# but I have

<snip>

When you say "C++ functions", do you mean "exposes C++ classes" or do
you mean "exposes normal dll entry points (functions), and the dll is
written in C++" ?

The first I have no idea how to do (or even if its possible), the second
can be done using DllImport attributes.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #2
Juan Martinez wrote:
Hello!

I am trying to use a DLL that contains c++ functions in c# but I have

<snip>

When you say "C++ functions", do you mean "exposes C++ classes" or do
you mean "exposes normal dll entry points (functions), and the dll is
written in C++" ?

The first I have no idea how to do (or even if its possible), the second
can be done using DllImport attributes.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Nov 17 '05 #3
Hello
I am trying to use a DLL that contains c++ functions in c# but I have
not been able to make it,

What is wrong ? What made you unable to make it ?

If it's a COM dll, then you can add it as a reference to your project and
wraper classes will be generated. Or you can use COM interop by hands.

If it's a C dll, then you need to interop functions of your interest
something like this:
[DllImport("ntdll.dll")]
public static extern IntPtr memcpy(IntPtr dst, IntPtr src, int count);
--
With best regards,
Andrew
Nov 17 '05 #4
Hello
I am trying to use a DLL that contains c++ functions in c# but I have
not been able to make it,

What is wrong ? What made you unable to make it ?

If it's a COM dll, then you can add it as a reference to your project and
wraper classes will be generated. Or you can use COM interop by hands.

If it's a C dll, then you need to interop functions of your interest
something like this:
[DllImport("ntdll.dll")]
public static extern IntPtr memcpy(IntPtr dst, IntPtr src, int count);
--
With best regards,
Andrew
Nov 17 '05 #5
I am based on the example published in:

http://msdn.microsoft.com/library/de.../en-us/vcsampl
e/html/vcsammcppwrappersampledemonstrateswrappingcdllwith managedextensio
ns.asp

The original dll (LOOX Maker) is in Visual C++, so according with that
example i am making a dll in Visual C++ .net that use the original dll,
and it is the next code :

#include <graph.h>

#using <mscorlib.dll>

#include <line.h>

using namespace System;

public __gc class ScdGraph
{
protected:
LxGraph *lxGraph;
int a;

public :

char * StringToChar(String * s)
{
int length = s->Length;
char * in_string = new char[length+1];
for(unsigned short i = 0; i<length; i++)
{
in_string[i] = (char)s->Chars[i];
}

in_string[length] = '\0';

return in_string;
}
ScdGraph::ScdGraph(IntPtr w,int mode)
{
lxGraph= new LxGraph((Widget)w.ToPointer(),mode);

a=7;
}

//ScdGraph::ScdGraph(HWND__ *w,String * fileName,int mode)

ScdGraph::ScdGraph(IntPtr w,String * fileName,int mode)
{
char * name = this->StringToChar(fileName);

lxGraph = new LxGraph((Widget)w.ToPointer(),name,mode);

//lxGraph = new LxGraph((Widget)w,name,mode);

if(name!=NULL)
delete [] name;
a=9;
}

ScdGraph::~ScdGraph()
{
}

LxGraph *GetLxGraph()
{
return lxGraph;
}
int GetA()
{
return a;
}
};

//---------------------LINEA--------------------
public __gc class ScdLine
{
protected:
LxLine *l;

public:
ScdLine::ScdLine(String *s,ScdGraph *g,int x1,int y1,int x2,int y2)
{
char * name = g->StringToChar(s);
l = new LxLine(name,g->GetLxGraph(),x1,y1,x2,y2);
if(name!=NULL)
delete [] name;

}
};
graph.h and line.h they are files of which i want to reuse in c# .NET,
when i compile this code i generate my dll to use in c# .NET.

i made a project in c# .NET and there i use the dll and i call the
functions :

private void button1_Click(object sender, System.EventArgs e)
{
ScdGraph g = new ScdGraph(this.Handle,1);
//This code line must create an area to draw
ScdLine l = new ScdLine("Linea",g,10,10,200,400);
//This code line must draw a line into the drawing area

label1.Text=g.GetA().ToString();
//this code show the value of "a"
}

the problem is that this code dont make the drawing area neither draw
the line.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #6
I am based on the example published in:

http://msdn.microsoft.com/library/de.../en-us/vcsampl
e/html/vcsammcppwrappersampledemonstrateswrappingcdllwith managedextensio
ns.asp

The original dll (LOOX Maker) is in Visual C++, so according with that
example i am making a dll in Visual C++ .net that use the original dll,
and it is the next code :

#include <graph.h>

#using <mscorlib.dll>

#include <line.h>

using namespace System;

public __gc class ScdGraph
{
protected:
LxGraph *lxGraph;
int a;

public :

char * StringToChar(String * s)
{
int length = s->Length;
char * in_string = new char[length+1];
for(unsigned short i = 0; i<length; i++)
{
in_string[i] = (char)s->Chars[i];
}

in_string[length] = '\0';

return in_string;
}
ScdGraph::ScdGraph(IntPtr w,int mode)
{
lxGraph= new LxGraph((Widget)w.ToPointer(),mode);

a=7;
}

//ScdGraph::ScdGraph(HWND__ *w,String * fileName,int mode)

ScdGraph::ScdGraph(IntPtr w,String * fileName,int mode)
{
char * name = this->StringToChar(fileName);

lxGraph = new LxGraph((Widget)w.ToPointer(),name,mode);

//lxGraph = new LxGraph((Widget)w,name,mode);

if(name!=NULL)
delete [] name;
a=9;
}

ScdGraph::~ScdGraph()
{
}

LxGraph *GetLxGraph()
{
return lxGraph;
}
int GetA()
{
return a;
}
};

//---------------------LINEA--------------------
public __gc class ScdLine
{
protected:
LxLine *l;

public:
ScdLine::ScdLine(String *s,ScdGraph *g,int x1,int y1,int x2,int y2)
{
char * name = g->StringToChar(s);
l = new LxLine(name,g->GetLxGraph(),x1,y1,x2,y2);
if(name!=NULL)
delete [] name;

}
};
graph.h and line.h they are files of which i want to reuse in c# .NET,
when i compile this code i generate my dll to use in c# .NET.

i made a project in c# .NET and there i use the dll and i call the
functions :

private void button1_Click(object sender, System.EventArgs e)
{
ScdGraph g = new ScdGraph(this.Handle,1);
//This code line must create an area to draw
ScdLine l = new ScdLine("Linea",g,10,10,200,400);
//This code line must draw a line into the drawing area

label1.Text=g.GetA().ToString();
//this code show the value of "a"
}

the problem is that this code dont make the drawing area neither draw
the line.

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #7
Exposing a C++ class in a DLL is not as difficult as it might sound. The
easiest way I know of, if you have the source code (I've had to do it
several times myself), is to make the project a managed DLL and create a
wrapper class that implements the necessary public functions.

Lee Crabtree

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Juan Martinez wrote:
Hello! I am trying to use a DLL that contains c++ functions in c# but I
have

<snip>

When you say "C++ functions", do you mean "exposes C++ classes" or do you
mean "exposes normal dll entry points (functions), and the dll is written
in C++" ?

The first I have no idea how to do (or even if its possible), the second
can be done using DllImport attributes.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 17 '05 #8
Exposing a C++ class in a DLL is not as difficult as it might sound. The
easiest way I know of, if you have the source code (I've had to do it
several times myself), is to make the project a managed DLL and create a
wrapper class that implements the necessary public functions.

Lee Crabtree

"Lasse Vågsæther Karlsen" <la***@vkarlsen.no> wrote in message
news:uQ**************@tk2msftngp13.phx.gbl...
Juan Martinez wrote:
Hello! I am trying to use a DLL that contains c++ functions in c# but I
have

<snip>

When you say "C++ functions", do you mean "exposes C++ classes" or do you
mean "exposes normal dll entry points (functions), and the dll is written
in C++" ?

The first I have no idea how to do (or even if its possible), the second
can be done using DllImport attributes.

--
Lasse Vågsæther Karlsen
http://www.vkarlsen.no/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2

Nov 17 '05 #9
Check http://www.codeproject.com/csharp/Us...asp?print=true

"Juan Martinez" <ja*****@hotmail.com> wrote in message
news:u2**************@TK2MSFTNGP10.phx.gbl...
Hello!

I am trying to use a DLL that contains c++ functions in c# but I have
not been able to make it, this DLL is of a software called LOOX Maker,
which is a graphic editor that has many options reason why I want to
reuse that code so I not to have to program it again, also I have source
code of this program, I have tried several things but I have not
obtained it.
Somebody that has more experience in this type of things can help me?
I can send the project to you where I am trying to use it and also the
DLL and source code of LOOX Maker, I hope that somebody can help me.

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Enos Meroka | last post by:
Hallo, I am a student doing my project in the university.. I have been trying to compile the program using HP -UX aCC compiler, however I keep on getting the following errors. ...
3
by: Mike L | last post by:
Should the command call "using" be before or after my namespace? **AFTER** namespace DataGridBrowser { using System; using System.Drawing; using System.Drawing.Drawing2D; using...
3
by: xzzy | last post by:
I was wondering why we have to have using System.Data using System.Configuration using etc.... why are they not all lumped into one 'using'? In other words, is there a best way to use...
14
by: pmud | last post by:
Hi, I need to use an Excel Sheet in ASP.NET application so that the users can enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever the USER ENETRS needs to go to the...
8
by: acb | last post by:
Hi, I wrote a DLL Component (using Visual Studio 2005) and managed to include it into a C# Console application. I am now trying to include this component into a Web project. I copy the DLL...
0
by: Metal2You | last post by:
I'm working on an ASP.NET 2.0 application in Visual Studio 2005 that accesses a Sybase database back end. We're using Sybase SQL Anywhere 9.0.2.3228. I have installed and registered the Sybase...
10
by: mg | last post by:
I'm migrating from VB6 and have a question about using 'Using' and the best way to use it. Here is a example of a small bit of code: dbConx("open") Using CN Dim CMD As New OleDbCommand(sSQL,...
0
by: Eugene Anthony | last post by:
The problem with my coding is that despite removing the records stored in the array list, the rptPages repeater control is still visible. The rptPages repeater control displayes the navigation...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
6
by: =?Utf-8?B?U2hhd24gU2VzbmE=?= | last post by:
Greetings! I was researching AJAX to provide a solution to displaying status messages while a long process executed. I found several examples online and was able to use their code to get a quick...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.