471,627 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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

Jan 9 '06 #1
2 1192

"Victor" <bi******@chefmail.de> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
| 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.

Jan 9 '06 #2
Thank you, Willy.

Your advices are really helpful for me.

Victor

Jan 9 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by 20041210.20.eclipse | last post: by
2 posts views Thread by Rob | last post: by
reply views Thread by Tony Johansson | last post: by
reply views Thread by leo001 | last post: by

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.