A C++ DLL used in a C# Forms project

V

Victor

Hi everybody !

I would like to consult a guru for the obstacle I cannot tackle alone.

My environment is Visual Studio 2005 under Windows XP.

I have two projects :

(A) A DLL - HerHair.dll - created in the VC++ by the Wizard. Its header
file HerHair.h has the following content :

**************

#ifdef HERHAIR_EXPORTS
#define HERHAIR_API __declspec(dllexport)
#else
#define HERHAIR_API __declspec(dllimport)
#endif

// This class is exported from the HerHair.dll
class HERHAIR_API CHerHair {
public:
CHerHair(void);
// TODO: add your methods here.
};

extern HERHAIR_API int nHerHair;

HERHAIR_API int fnHerHair(void);

**************

This project compiles perfectly.

(B) An EXE, which should use the HerHair.dll. This project is developed
in C# and is of WindowsForms type. I have made an entree in its
reference pointing to the directory where the DLL created in the
project (A) resides.

In the declaration
public class Form1 : System.Windows.Forms.Form {

there must appear an instance of the class exported from the DLL
CHerHair class1;

This project cannot compile. I get the following error :

The type or namespace name 'CHerHair' could not be found (are you
missing a using directive or an assembly reference?)


Well, I tried to put the following before the declaration of the
CHerHair instance :
[DllImport("HerHair.dll")]

- no effect.

I tried to put the declaration of the class CHerHair in the project (A)
within a namespace directive like this :

namespace HerHair {
// This class is exported from the HerHair.dll
class HERHAIR_API CHerHair {
public:
CHerHair(void);
// TODO: add your methods here.
};

extern HERHAIR_API int nHerHair;

HERHAIR_API int fnHerHair(void);
}

and then added to the project (B) this line before the declaration of
the class Form1 :

using HerHair;

The result was only an additional error on this line :

The type or namespace name 'HerHair' could not be found (are you
missing a using directive or an assembly reference?)


So, what could be wrong in all this stuff ?


Many thanks in advance.

Victor
 
W

Willy Denoyette [MVP]

| Hi everybody !
|
| I would like to consult a guru for the obstacle I cannot tackle alone.
|
| My environment is Visual Studio 2005 under Windows XP.
|
| I have two projects :
|
| (A) A DLL - HerHair.dll - created in the VC++ by the Wizard. Its header
| file HerHair.h has the following content :
|
| **************
|
| #ifdef HERHAIR_EXPORTS
| #define HERHAIR_API __declspec(dllexport)
| #else
| #define HERHAIR_API __declspec(dllimport)
| #endif
|
| // This class is exported from the HerHair.dll
| class HERHAIR_API CHerHair {
| public:
| CHerHair(void);
| // TODO: add your methods here.
| };
|
| extern HERHAIR_API int nHerHair;
|
| HERHAIR_API int fnHerHair(void);
|
| **************
|
| This project compiles perfectly.
|
| (B) An EXE, which should use the HerHair.dll. This project is developed
| in C# and is of WindowsForms type. I have made an entree in its
| reference pointing to the directory where the DLL created in the
| project (A) resides.
|
| In the declaration
| public class Form1 : System.Windows.Forms.Form {
|
| there must appear an instance of the class exported from the DLL
| CHerHair class1;
|
| This project cannot compile. I get the following error :
|
| The type or namespace name 'CHerHair' could not be found (are you
| missing a using directive or an assembly reference?)
|
|
| Well, I tried to put the following before the declaration of the
| CHerHair instance :
| [DllImport("HerHair.dll")]
|
| - no effect.
|
| I tried to put the declaration of the class CHerHair in the project (A)
| within a namespace directive like this :
|
| namespace HerHair {
| // This class is exported from the HerHair.dll
| class HERHAIR_API CHerHair {
| public:
| CHerHair(void);
| // TODO: add your methods here.
| };
|
| extern HERHAIR_API int nHerHair;
|
| HERHAIR_API int fnHerHair(void);
| }
|
| and then added to the project (B) this line before the declaration of
| the class Form1 :
|
| using HerHair;
|
| The result was only an additional error on this line :
|
| The type or namespace name 'HerHair' could not be found (are you
| missing a using directive or an assembly reference?)
|
|
| So, what could be wrong in all this stuff ?
|


First, you cannot create instances of native C++ classes from C#, only
managed classes can be created from managed code like C#. Second you cannot
call instance methods from C# using PInvoke, only non-member functions can
be called (C style functions).
You have several options to solve this:
- Use managed instead of native classes in your C++ code and compile the
code using the clr extenstions(see project options).
- Write a 'managed wrapper' class (using the above method), that wraps your
native class.
- Expose your native classes as COM interfaces (f.i using ATL or MFC).

Check MSDN docs (C++ interop) for more details on managed/unmanaged interop.

Willy.
 

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