473,407 Members | 2,598 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,407 software developers and data experts.

A C# class library in a C++ project

Hi everybody !

I have a beautiful class library (DLL) designed under C#. Now I need to
use a class from that library in a VC++ project (VS 2005).

However, I desperately fail to find a possibility to make my VC++ code
see the declaration of the class. There are no header files under C#,
no LIB to link to the current project - solely DLL. I can load this DLL
in my project, can unload it, but I do not know how to declare a class
instance...

'using namespace' does not help - compiler states "..no such
namespace"...

Any directing hint would be highly appreciated.

Victor

Jan 16 '06 #1
17 2443
Hi Victor!
I have a beautiful class library (DLL) designed under C#. Now I need to
use a class from that library in a VC++ project (VS 2005).


See: Walkthrough: Creating and Using a Managed Assembly
http://msdn2.microsoft.com/en-us/library/ms235638.aspx

See: How to: Add or Remove References in Visual Studio
http://msdn2.microsoft.com/en-us/library/wkze6zky.aspx

(Project|References...)

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jan 16 '06 #2
Jochen,

I must apologize, but I do not find how the stuff from the links you
provided pertains my problem... You know, I am quite new to .NET, so
would you give me some more comments, if you please...

I have a Class Library completely developed in C#, containig a complex
definition of a class referring many other sources. On compiling this
project I get only a DLL, respectively debug or release version.

Further, I have a rather advanced project in VC++, using MFC and doing
already many different things. In this project I need a class, defined
in the mentioned C# DLL. The question of mine is : HOW to teach my VC++
about existence of the class definition in the DLL created in C# ?

I can brilliantly use this class in another C# project. I can use other
class definitions specified in DLL's that have been developed in C++,
because I have their header files.

And all the links you pointed me to - as far as I understand - explain
how to do things WITHIN ONE LANGUAGE. I do not seem to have problems
like this...

Maybe, I simply do not understand anything a proper way ?

Will you help me any furhter, please ?

Victor

Jan 16 '06 #3
Hi Victor!
On compiling this
project I get only a DLL, respectively debug or release version.
Yes.
HOW to teach my VC++
about existence of the class definition in the DLL created in C# ?
1st: You need to enabled "C++/CLI" via the "/clr" compiler switch
(Project|Settings|General|Common Language Runtime Support: /clr)
And all the links you pointed me to - as far as I understand - explain
how to do things WITHIN ONE LANGUAGE. I do not seem to have problems
like this...


In .NET you can use the DLLs (assemblies) from any .NET enabled language
(VB, C++/CLI, C#, ...)
If you have enabled the "/clr" switch, then you can add a reference to
your DLL!
(Project|Common Properties|References).

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jan 16 '06 #4
THANK YOU JOCHEN !!!

Now I have my class !!

Though there appeared some other problems now. But they are of
different nature. Will try to figure them out first myself...

Victor

Jan 16 '06 #5
Unfortunately, I am failing to tackle this different problem alone...

So I have to appeal about help again.

Now the problem is like this:

In the C# DLL there is a class defined :

class CSM : Stream, IDisposable {
private string wFile;
public CSM () : base() {}
public CSM (string file) : this() { Filename = file; }
public CSM (CSM src) {}
public string Filename {
get { return wFile; }
set { wFile = value; }
}
}

Then I try to declare an instance of the class in the C++ project. As
long as I am doing it this way :
CSM smFile;
- no problems arise.
But I need to declare it using another ctor :
CSM smFile("Some value");

The compiler laments : C3673 - 'CSM' : class does not have a
copy-constructor

So what is about the line
public CSM (CSM src) {} ???

Furthermore, when the CSM has been declared without parameters, I get
the following compiler error on trying to access this instance :
wcscpy(fileW,smFile.Filename); // fileW is a buffer declared
earlier

Error C2228: left of '.Filename' must have class/struct/union

What is/can be wrong here?

Any assistance would be highly appreciated.

Victor

Jan 18 '06 #6
Hi Victor!
Then I try to declare an instance of the class in the C++ project. As
long as I am doing it this way :
CSM smFile;
- no problems arise.
But I need to declare it using another ctor :
CSM smFile("Some value");
CSM ^smFile;
smFile = gcnew CSM("Some value");

Furthermore, when the CSM has been declared without parameters, I get
the following compiler error on trying to access this instance :
wcscpy(fileW,smFile.Filename); // fileW is a buffer declared


I think there is no default conversion from System::String to LPCTSTR...
You need to use PtrToStringChars

See: How to convert from System::String* to Char* in Visual C++ 2005 or
in Visual C++ .NET
http://support.microsoft.com/kb/311259/
--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jan 18 '06 #7
Thanks Jochen,

you know, this far I have riched myself as well :

CSM^ smFile;
smFile = gcnew CSM("Some value");

- with the same resulting compiler error

C3673 - 'CSM' : class does not have a
copy-constructor

..................

As for conversion from System::String, you may have right, but I
believe that the problem must appear somewhere still before conversion;
the compiler does not find this element at all - if I understand all
this stuff correctly :
Error C2228: left of '.Filename' must have class/struct/union

It means that the Filename could not be accessed at all - or not ?

Victor

Jan 18 '06 #8
BTW : What is the FieldGetter ?

In the MSDN I do not find any reference to this term---

Victor

Jan 18 '06 #9
I am sure that it's not that line which is causing the compiler error.
Copy/paste the entire compiler error and the entire code block where the
error occurs.

--
Regards,
Nish [VC++ MVP]
"Victor" <bi******@chefmail.de> wrote in message
news:11*********************@g44g2000cwa.googlegro ups.com...
Thanks Jochen,

you know, this far I have riched myself as well :

CSM^ smFile;
smFile = gcnew CSM("Some value");

- with the same resulting compiler error

C3673 - 'CSM' : class does not have a
copy-constructor

.................

As for conversion from System::String, you may have right, but I
believe that the problem must appear somewhere still before conversion;
the compiler does not find this element at all - if I understand all
this stuff correctly :
Error C2228: left of '.Filename' must have class/struct/union

It means that the Filename could not be accessed at all - or not ?

Victor

Jan 18 '06 #10
The whole trouble snippet cosists of two lines:

(1) using namespace CSMSharp;

and then the line (2) in two variants:

This variant causes the below error :

CSM^ smFile = gcnew CSM(fileWav);

Error 6 error C3673: 'CSMSharp::CSM' : class does not have a
copy-constructor
If declared this way :

CSM^ smFile = gcnew CSM();

- no compiler error occurs...
Victor

Jan 18 '06 #11
I tested out your code.

I creates a C# DLL project :-

******************************
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace CSMSharp

{
public class CSM
{
private string wFile;
public CSM() : base() { }
public CSM(string file) : this() { Filename = file; }
public CSM(CSM src) { }
public string Filename
{
get { return wFile; }
set { wFile = value; }
}
}
}
******************************

And a Cpp caller :-

******************************
using namespace System;
using namespace CSMSharp;

int main(array<System::String ^> ^args)
{
CSM^ smFile = gcnew CSM("fileWav");
return 0;
}
******************************

Code compiles - no problems at all. I assumed fileWav is a string.

--
Regards,
Nish [VC++ MVP]
"Victor" <bi******@chefmail.de> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
The whole trouble snippet cosists of two lines:

(1) using namespace CSMSharp;

and then the line (2) in two variants:

This variant causes the below error :

CSM^ smFile = gcnew CSM(fileWav);

Error 6 error C3673: 'CSMSharp::CSM' : class does not have a
copy-constructor
If declared this way :

CSM^ smFile = gcnew CSM();

- no compiler error occurs...
Victor

Jan 18 '06 #12
Nishant,

I tried to follow your approach : I had the same success - everything
compiles O.K.

But my working project is not this simple. And I do get the described
error C3673...
Regrettably, I do not have a lot of experience under .NET - so I have
only inflicted some bad headache on myself without any tangible chance
to find a way out...

What could cause this behaviour in my project ?

Additional info : I develop a MFC project that must support
multithreading.

Regards
Victor

Jan 18 '06 #13
Try and create a minimal project that reproduces the error. Then copy/paste
the code here and someone can figure out what you are doing wrong.

--
Regards,
Nish [VC++ MVP]
"Victor" <bi******@chefmail.de> wrote in message
news:11*********************@g47g2000cwa.googlegro ups.com...
Nishant,

I tried to follow your approach : I had the same success - everything
compiles O.K.

But my working project is not this simple. And I do get the described
error C3673...
Regrettably, I do not have a lot of experience under .NET - so I have
only inflicted some bad headache on myself without any tangible chance
to find a way out...

What could cause this behaviour in my project ?

Additional info : I develop a MFC project that must support
multithreading.

Regards
Victor

Jan 18 '06 #14
Your advice gives at least some hope---

Thank you Nishant

Jan 18 '06 #15
Well, it did not take too long to reproduce the error in an almost
virgin project.

I created a new C++ MDI project using MFC in a shared DLL - I did not
change anything in the wizard settings for a new project. The only
thing : in the Project Properties/General/CLR Support I activated the
switch /CLR. Surely, I also introduced a reference to my C# DLL.
Then I added a new menu entree and a handler for it, resulting in a
following line in the app header ResTesA.h :

public:
afx_msg void OnViewZoomIn();

In the ResTesA.cpp there appeared the function body :
void CResTesAApp::OnViewZoomIn()
{
using namespace SMWaveFileSharp;
CSMWaveFile^ smWaveFile = gcnew CSMWaveFile("fileWav");
}

-- this was enough for reproducing the error

Error C3673: 'SMWaveFileSharp::CSMWaveFile' : class does not have a
copy-constructor

Any comments are welcome

Victor

Jan 18 '06 #16
Hey Victor,

You also need to copy/paste the CSMWaveFile code listing. Otherwise, people
cannot reproduce the error.

--
Regards,
Nish [VC++ MVP]
"Victor" <bi******@chefmail.de> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
Well, it did not take too long to reproduce the error in an almost
virgin project.

I created a new C++ MDI project using MFC in a shared DLL - I did not
change anything in the wizard settings for a new project. The only
thing : in the Project Properties/General/CLR Support I activated the
switch /CLR. Surely, I also introduced a reference to my C# DLL.
Then I added a new menu entree and a handler for it, resulting in a
following line in the app header ResTesA.h :

public:
afx_msg void OnViewZoomIn();

In the ResTesA.cpp there appeared the function body :
void CResTesAApp::OnViewZoomIn()
{
using namespace SMWaveFileSharp;
CSMWaveFile^ smWaveFile = gcnew CSMWaveFile("fileWav");
}

-- this was enough for reproducing the error

Error C3673: 'SMWaveFileSharp::CSMWaveFile' : class does not have a
copy-constructor

Any comments are welcome

Victor

Jan 19 '06 #17
Sorry guys,

I am so much ashame -- but probably we all make sometimes stupid
mistakes...

It was completely my personal fault and nothing more -- I referred in
the VC++ project an old version of the DLL with the same name....

Sorry for this bothering and thank you very much

Nishant and Jochen

for your engagemental support !!!

Happy coding ;-()

Victor

Jan 19 '06 #18

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

Similar topics

2
by: Andrew Walsh | last post by:
Is it possible to create a Class Library Project using the personal edition of Visual Studio? Trying to change the Output Type on a project already created only shows Windows App and Console...
1
by: Russell Mangel | last post by:
After creating a new .NET Class Library project, I then add the header file: #include <iostream> (I tried adding in my main .cpp file and also tried in stdafx.h file.) Doesn't work, and I...
1
by: Ron | last post by:
Hello, I need to create/instantiate a global class library project so that 2 EXE's can write to the same class library form in the same instance of the class libary. I am thinking something...
2
by: sathya | last post by:
Hi, I am creating a class libarary application, when i add a Oulook 11.0 Com Object as reference to my project , i am getting error during project compilation as: Refferenced Interop.Oulook...
10
by: ajtaylor | last post by:
Hello, I have a load of native C++ code that I want to use in a CLR class library. My "logic" being that I create a C++/CLI managed class that acts as an interface to the unmanged code. I...
1
by: Zoe Hart | last post by:
Prior to VS2005 I configured my applications via the appSettings section of the app.config file. When I developed a class library, I used ConfigurationManager.AppSettings(settingName) syntax and...
3
by: Gonza | last post by:
Hi group, is there anyway to catch unhandled exceptions on a class library project?? thanks in advance.
3
by: Phaitour | last post by:
Hi there, I'm working on developing a large Class Library project that is slowly becoming a shared "framework" library amongst multiple applications. As this shared library grows, I need to...
4
by: =?Utf-8?B?cm9kY2hhcg==?= | last post by:
hey all, i compiled my Class Library in Release mode but whenever i add a reference to this dll it adds that extra .pdb file. how do i keep it from adding that ..pdb file? thanks, rodchar
2
by: Ronald S. Cook | last post by:
In my solution, I have a client (Windows app) and a class library. The class library project has a connection string setting that I would like to be able to change (from the client project...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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
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...
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,...
0
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...

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.