simple c++ dll caling from c# dll

K

Koliber (js)

does anybody could tell me how can i do a 'simple c++ dll I compiled'
- calling
from my c# code (when I try adding this dll to references' it ays that
it should be com) ?

T I A
jS
 
N

Nicholas Paldino [.NET/C# MVP]

Koliber,

If you want to call classes in a C++ dll, you have one of two options.
The first is to expose the classes through COM, and use COM interop. The
other is to create a managed wrapper for the classes in the dll, and then
add a reference to the managed wrapper.

Regardless, you can't just add a reference to a C++ dll and use the
classes.
 
G

Guest

Koliber said:
does anybody could tell me how can i do a 'simple c++ dll I compiled'
- calling
from my c# code (when I try adding this dll to references' it ays that
it should be com) ?

If the DLL is a normal Win32 DLL and not a COM/ActiveX DLL, then
you need to DllImport the functions you want to use.

Note that I said functions not classes and methods.

Arne
 
K

Koliber (js)

If the DLL is a normal Win32 DLL and not a COM/ActiveX DLL, then
you need to DllImport the functions you want to use.

Note that I said functions not classes and methods.
Arne


I tried some like this in cpp dll

#define DllExport __declspec( dllexport )

/DllExport extern void test();
DllExport extern int a();
DllExport extern int b();

/
*****************************************************************************/

void test()
{
}


int a()
{
return 6;
}


int b()
{
return -6; //676k6
}


and in c# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplicationTest
{
class Program
{
[DllImport("dng_validate.dll")]
public extern static void test();
[DllImport("dng_validate.dll")]
public extern static int a();
[DllImport("dng_validate.dll")]
public extern static int b();

static void Main(string[] args)
{
test();
b();
a();
}
}
}

but in runtime there is an error that in my dll there is no entry
point test
(also a b tried) There is if i check it with total commander plugin
exported functions are:

int __cdecl a(void)
int __cdecl b(void)
int __cdecl test(void)

:( ?

T I A
JS
 
B

Ben Voigt [C++ MVP]

You need to be very specific about name mangling and calling convention
options. On the C++ side, you probably want:

#define FOREXPORT __declspec( dllexport ) __stdcall extern "C"

Use Dependency Walker (www.dependencywalker.com) to view the export table,
then you have the option of viewing the raw names or the decoded C++ names.

You will probably also want a linker definition file (.def) to avoid name
mangling completely.


If the DLL is a normal Win32 DLL and not a COM/ActiveX DLL, then
you need to DllImport the functions you want to use.

Note that I said functions not classes and methods.
Arne


I tried some like this in cpp dll

#define DllExport __declspec( dllexport )

/DllExport extern void test();
DllExport extern int a();
DllExport extern int b();

/
*****************************************************************************/

void test()
{
}


int a()
{
return 6;
}


int b()
{
return -6; //676k6
}


and in c# code:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;


namespace ConsoleApplicationTest
{
class Program
{
[DllImport("dng_validate.dll")]
public extern static void test();
[DllImport("dng_validate.dll")]
public extern static int a();
[DllImport("dng_validate.dll")]
public extern static int b();

static void Main(string[] args)
{
test();
b();
a();
}
}
}

but in runtime there is an error that in my dll there is no entry
point test
(also a b tried) There is if i check it with total commander plugin
exported functions are:

int __cdecl a(void)
int __cdecl b(void)
int __cdecl test(void)

:( ?

T I A
JS
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top