|
akira32 wrote: How to divide a class into multi-files(like c# partial)?
C++ or C++/CLI already supports this. One header file and several source files is possible. You can have a MainFormUI.cpp which includes MainForm.h and have methods like InitializeComponent . Another source file say MainForm.cpp which has all other method implementations.
Unfortunately, VS designer won't support this kind of separation and it will always write the auto-generated code to MainForm.h.
|
|
|
|
|
A C# Partial Class is not equivalent to the C++ & C++/CLI header/source model. The following cannot not be done with C++ or C++/CLI in different (or in the same) files:
public partial class Employee
{
public void DoWork()
{
}
}
public partial class Employee
{
public void GoToLunch()
{
}
}
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
George L. Jackson wrote: A C# Partial Class is not equivalent to the C++ & C++/CLI header/source model.
I never said they are equal.
George L. Jackson wrote: The following cannot not be done with C++ or C++/CLI in different (or in the same) files:
Correct because it needs all the functions to be declared before using.
The question here is How to divide a class into multi-files(like c# partial)? and I believe C++'s header and multiple source files model is the answer. Please correct me if you feel it is wrong.
|
|
|
|
|
It depends on the point of view, just having multiple physical files or having both multiple physical files and the method of class construction. Yes, you can use multiple files (header/source) in C++ as you do in C#. However, the C# partial class is constructed more like a C++ class-namespace hybrid where you can define methods and data members in multiple source files that somehow come together in the scope of one class.
"We make a living by what we get, we make a life by what we give." --Winston Churchill
|
|
|
|
|
ok i'm anoob in cli and i'm sure this quesiton has a simple answer ...but i'm new to managed code so...
what i wanna do is:
void mod_str(String ^bb) {
bb="bau";
}
void action() {
String ^str="ciao";
mod_str(str);
this->button1->Text=str;
}
what i wanna is that this button1 text became "bau" but with this code it remains "ciao"...
in c++ it wolud be something like...
void mod_str(string &str) {
str="bau";
}
void action() {
string str="ciao";
mod_str(str);
....
}
|
|
|
|
|
instead of
void mod_str(String ^bb) {
bb="bau";
}
use
void mod_str(String ^%bb) {
bb="bau";
}
EDIT:
This is a pretty good place to learn the keywords and operators specific to C++/CLI
http://msdn.microsoft.com/en-us/library/xey702bw.aspx[^]
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
Thank you for this simple but important reply
|
|
|
|
|
Is it possible to create a DLL that exposes both a managed interface and a native one so that it can be used by both managed and native clients?
For instance, can it contain a managed class that wraps exported "C"-linkage functions, so that a managed client can see the managed class, and a native client can see the C-type functions?
|
|
|
|
|
Hi,
I'm not aware of any official solution, and I haven't tried it yet, however I got this[^] answer more than 2 years ago on a similar question of mine.
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Thanks Luc. I think that it might be possible because I'm not trying to export any native classes, only native API's.
Thanks for digging up that old post, I appreciate it.
|
|
|
|
|
No problem.
If you have any results, please let us know.
TIA
Luc Pattyn
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
This is possible using conditional compilation. You need to create managed and unmanaged methods in the header files separated with _MANAGED preprocessor symbol.
Consider the following code.
#ifdef _MANAGED
using System;
using System::Runtime::InteropServices;
#endif
#include <string>
class MixedAPI
{
public:
#ifdef _MANAGED
void TakeAString(gcroot<String^>); #else
void TakeAString(std::string); #endif
};
void MixedAPI::TakeAString(gcroot<String^> str)
{
}
void MixedAPI::TakeAString(std::string str)
{
}
|
|
|
|
|
So the library must be recompiled for managed or native?
|
|
|
|
|
Nope. Library should be compiled only once and it should be a mixed mode DLL (compiled using /clr ). Trick is with header files. When a native user uses your library, he will see only the methods defined for native. Other methods are not seen as those are guarded with _MANAGED preprocessor directive.
1 - You create the library with exported functions and managed wrapper functions. This should be compiled with /clr switch.
2 - Supply the library along with header file. Since header file has preprocessor statements, managed method definitions won't be seen by a native compiler. They still can work with native methods.
Since your DLL is mixed mode, all users require .NET framework to be installed of course.
|
|
|
|
|
- using visual studio 2008 -
what i have done so far is this:
a) i selected the file .lib and the .h and imported in my project -- done
b) i set in the project properties the linker to point to the lib file --done
c) i set in the project properties the c++ to look for the .h file --done
d) i set in the general project properties to compile using clr
e) my portion of code where i inlude the .h:
...
#pragma unmanaged
#include "database.h"
#pragma managed
...
f) i put a button in my form and onclick i initialize a class in the unmanaged class
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
try {
db::database db2;
}
catch (System::Exception ^exception) {
}
}
g) after that i compile and everything goes fine (i thinked "maybe too simple")
h) i run the application and ....tada error...
An unhandled exception of type 'System.TypeInitializationException' occurred in Unknown Module.
Additional information: The type initializer for '<Module>' threw an exception.
i googled around and i see is a common problem but i can't find an answer on what i have a todo...a magic code like #pragma something or some compiler settings or somethings....i neither find a good "howTo" on how to do this... can someone help me out or point me to a tutorial - how to ?? thx
modified on Thursday, October 1, 2009 8:02 AM
|
|
|
|
|
maybe the question was to general...i make a link where you can download my sample file with the problem. Th warnings are only because of my lib was for 'release' only. Hope someone can help me.
link
ps you need to correct the path of the linker to where you have the project to get the lib
ps2 maybe you need mysql do be installed on your pc with developer option to use because it use a mysql dll to run
or you have to put this dll in the project folder and you'll get it--download--
modified on Thursday, October 1, 2009 8:24 AM
|
|
|
|
|
carlo.andreoli wrote: you can download my sample file with the problem
This looks like a fairly sizeable project, the zip file is nearly 5Mb. I think you are going to have to do some more work in narrowing down exactly where the error is occurring, at least to the line of code in your source.
|
|
|
|
|
ok if you open the program you'll see the real program are something like 100 line of real code. the size is beacuse i include all the vc++ project including the 2 lib that's like ~10MB of code...but the lib already work. The problem is that i compile it and everything goes right. When i run it gives me the error, the problem is in including the unmanaged class and trying to use it. It give me the error before i could initialize the first unmanaged class. Pls give it a try it's only 100 lines of code
|
|
|
|
|
carlo.andreoli wrote: Pls give it a try it's only 100 lines of code
Well I guess it's not too big a task for you then. You have all the same tools that I have to debug this; also you understand your code, I don't.
|
|
|
|
|
I have taken a look at your project, and in the button click event I see
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
try {
db::database db2;
}
catch (System::Exception ^exception) {
}
This does not agree with your original description. I also found that the project will not link under VC++ Express Edition, as the library appears to have been built with VS 2008, which I do not possess.
|
|
|
|
|
i'm currently working with visual studio 2008 express edition. Why the coode doesn't agree with my original description ?(maybe i used different variabiles name but that was just an example, i'm going to correct it). pls help me point out. Now i give you all the things i have done:
1) we want to develop an application that can speak with database
2) i am making the visual side of the project using visual studio 2008 express
3) my friend is developing the 'wrapper' to connect to mysql but it's developing not in vs2008 express edition
4) now i want to integrate his code with mine
5) i take his code and if i make a program (without using clr) it works fine
6) i take his code and put it on a form, using clr option to compile,it compiles well but then it crash giving me the error you see in the starter thread
any ideas...do i have to be more specific?
thx man i really want to understand where is the problem and you are trying to help me
ps you need to correct the path of the linker to where you have the project to get the lib
ps2 maybe you need mysql do be installed on your pc with developer option to use because it use a mysql dll to run
or --> you have to put this dll in the project folder and you'll get it--download--
modified on Thursday, October 1, 2009 8:24 AM
|
|
|
|
|
carlo.andreoli wrote: 6) i take his code and put it on a form, using clr option to compile,it compiles well but then it crash giving me the error you see in the starter thread
Well, as I stated before, you have all the tools to track down where it is failing. Use the debugger, step through the program to the fail point and then look at the variables and stack trace to see what is going on. If the problem lies in your friend's library then get him to fix it.
|
|
|
|
|
i'll give it a try...but i'm new to c++ programming..i have only developed thing through application by now so i have to skill a lot. Now i'll try to look at the stack frame to see if i can understand something
|
|
|
|
|
carlo.andreoli wrote: i'll give it a try...but i'm new to c++ programming
Maybe you should have started with a simpler project! Learning C++ is not the easiest task, even for programmers coming from C or Pascal background. You made it even harder by going for the managed C++/CLI form which adds a whole extra level of complexity. Buona fortuna!
|
|
|
|
|
Hello Everyone, I am struggling with a problem for two days and could not figure out what to do.
Here what I am trying to do.
I have cli<array> which is two dimentional. I get data in that array but is not in order. I want to copy that data in a temp array [same type] in order. I want change the location of the column and put it indore to the temp array.
example,original array A[0,0]
A[0,1]
A[0,2]
A[0,3]
I want to copy column in a different location of the new array
B[0,0]=A[0,2]
B[0,1]=A[0,3]
B[0,2]=A[0,0]
B[0,3]= A[0,0];
If anybody can please give me a hint would be very helpful.
if you did not understand the question please ask me I really need it soon.
Thanks in advance.
|
|
|
|