473,976 Members | 26,259 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(dlle xport)
#else
#define HERHAIR_API __declspec(dlli mport)
#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("HerH air.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 1278

"Victor" <bi******@chefm ail.de> wrote in message
news:11******** **************@ g43g2000cwa.goo glegroups.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(dlle xport)
| #else
| #define HERHAIR_API __declspec(dlli mport)
| #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("HerH air.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2368
by: Ann Morris | last post by:
INTRODUCTION One of the most powerful aspects of .NET and Windows Forms is data binding. Data binding is the process of associating user interface (UI) elements with a data source to generate a visual representation of data. Two types of data binding are available for Windows Forms: Simple Data Binding and Complex Data Binding. Simple data binding allows you to bind one data element to a control. In many situations you want to display...
4
8340
by: Mountain Bikn' Guy | last post by:
I am having serious problems with the following IDE bug: Could not write to output file 'x.dll' -- 'The process cannot access the file because it is being used by another process. ' and BUG: "Could Not Copy Temporary Files to the Output Directory" Error Message When You Build a Solution That Contains Multiple Projects I have tried all the solutions in Microsoft Knowledge Base Article - 313512.
0
1130
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET projects; its generated assembly is inside MyAssembly.dll When used inside a VC++.NET project it appears correctly over forms at design time but when trying to compile the project we get the following error:
0
1463
by: Severino | last post by:
Hi all, we have developed a .NET component for use inside Windows Forms: this component has been written using VC++.NET (2003) and is working perfectly when inserted inside VC#.NET or VB.NET projects; its generated assembly is inside MyAssembly.dll When used inside a VC++.NET project it appears correctly over forms at design time but when trying to compile the project we get the following error:
3
1437
by: 20041210.20.eclipse | last post by:
Hi, I'm using MS Visual Studio .NET 2003 Standard on WinXP Home on a DELL Notebook. It is not possible to create a project that contains windows forms. When trying to create a Windows Forms application I get the error "The specified module could not be found". When trying to add Forms manually to a project I get the same error message. Previousely I used the Standard edition on XP Professional and was able to add Forms to my projects.
7
2489
by: Mike Bulava | last post by:
I have created a base form that I plan to use throughout my application let call the form form1. I have Built the project then add another form that inherits from form1, I add a few panel controls each with a couple of controls in them I then rebuilt my project and my new panels and all controls they contained are gone... I've looked through the Auto generated code but don't see anything that looks wrong Any body have any idea why this...
2
1988
by: Rob | last post by:
I was working on a project and everything was going fine, then all of a sudden the form set as my startup object stopped loading. I tried setting some others as the startup object, and some of my forms will load and others won't. Those that won't load at startup don't load with the Show or ShowDialog command either. Can anyone give me a hint as to what went wrong?
4
1583
by: Steven Cummings | last post by:
Hello, I'm currently developing on a large project where several assemblies build into DLLs that support the final .NET EXE. The original plan was to implement the project in VB.NET, so the EXE project is a VB WinForms App. Later on we got approval to not stick to that requirement so I started writing library code in C# (which I much prefer). Of interest in my problem are two C# DLL projects aside from the main EXE project. The first,...
0
1607
by: Tony Johansson | last post by:
Hello! I have a very specific question and that is about how to inherit a visual control for example the control System.Windows.Forms.TextBox without causing the environment to delete the control when there are some compile errors. It's the same problem with any visual control that you inherit. The control is deleted as soon as you use the View Designer when there is compile error. It's very easy to reproduce my problem. You can do...
0
10353
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11818
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11571
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
8460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7604
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6415
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6557
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.