|
jobin007007 wrote: I actually made all the object variable definitions in the header file to another class I already had in the header file and declared them static.Is there any disadvantage in moving them to the class versus declaring them as global??????
That's fine and ends up being pretty much the same thing, except as
static objects of a class, only one instance will exist. You still need to
define the static instances in ONE cpp file, or you'll get a linker unresolved
external error.
jobin007007 wrote: QUESTION 1)So as seen above I am creating an pointer object of type UNMANAGEDNAMESPACE.Data So i guess the larger the size of UNMANAGEDNAMESPACE.Data class, the larger the memory used will be when i define pObj = new UNMANAGEDNAMESPACE.Data().
I suppose it's true, but not an issue. No matter what heap an object is created on,
it's still going to take some memory.
jobin007007 wrote: QUESTION 2)Is there any problems in the above approach. Is there a better approach for reducing memory of the program?
The approach is fine, and a good way to make use of C++/CLI - to interoperate
between managed and unmanaged code. Again, memory is not an issue here...use the
memory you need and free it when you're done with it, just like any programming
Since you're ManagedCLR class holds unmanaged resources (an UNMANAGEDNAMESPACE.Data
object), you may want to implement the ManagedCLR as disposable, so you can free
the unmanaged resource(s) deterministically. (See Destructors and Finalizers in Visual C++[^])
It would look something like this:
public ref class ManagedCLR
{
private:
UNMANAGEDNAMESPACE.Data *pObj;
public:
ManagedCLR()
{
pObj = new UNMANAGEDNAMESPACE.Data();
}
~ManagedCLR()
{
this->!ManagedCLR();
}
!ManagedCLR()
{
delete pObj;
pObj = 0;
}
void EntryPoint(void)
{
pObj->EntryPointUnmanged();
}
};
When used from C# code, you can call Dsipose() to deterministically free unmanaged resources of
a ManagedCLR object. Whwn used from C++/CLI code, just use "delete".
Mark
Mark Salsbery
Microsoft MVP - Visual C++
modified on Sunday, May 31, 2009 4:48 PM
|
|
|
|
|
Thank you.
I wish there were more examples on all this. I had to spend 2 days looking up examples on the net. and then when i got nowhere I got a couple of books to help me out. Even with the books, small errors here and there would always deter me. It seems like only some people are using CLI INTEROPERABILITY and most people are using the P/Invoke approach. Since performance is critical on my program, this suits me.
|
|
|
|
|
jobin007007 wrote: most people are using the P/Invoke approach.
I suppose those people aren't able to use C++/CLI, like people who
only know C#, or don't have access to source code to do a C++/CLI
approach.
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I suppose those people aren't able to use C++/CLI, like people who
only know C#, or don't have access to source code to do a C++/CLI
approach.
Yes. True. I am working with performance critical video hardware and software and at the same time I need to be able to develop GUI's at a fast pace. So I need to use with C++ with C#. I don't have any options!!
|
|
|
|
|
jobin007007 wrote: I am working with performance critical video hardware and software and at the same time I need to be able to develop GUI's at a fast pace. So I need to use with C++ with C#.
Same here.. WPF/C# for the GUI and general housekeeping stuff, and
C++/CLI to talk to my unmanaged C++ code...works sweet!
Cheers,
Mark
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Next, move all object/variable definitions from Unmanaged.h to
Unmanaged.cpp (Unmanaged.h is #included in more than one cpp file,
so the compiler is make more than one copy of the variables - the linker
doesn't like that, and it's ambiguous anyway).
Actually is there a way to access these global/variables that are declared in the header file that are not a part of any class but just a part of the namespace. If I move them to the cpp file my Managed.cpp will not even have access to them?
Lets say the Managed.cpp has access to these variables. How would I provide access to these variables from the C# file?
So Unmanaged.h is like this-
************************Unmanaged.h***********
namespace UNMANAGED{
class ABC{
};
}
Unmanaged.cpp is like this-
************************Unmanaged.cpp***********
namespace UNMANAGED{
//ABC definitions
int s; //I need access to these variables
int k; //I need access to these variables
}
***********Managed.cpp************
How would I get access to the variables I want?
|
|
|
|
|
jobin007007 wrote: How would I provide access to these variables from the C# file?
Provide an interface through your managed wrapper class.
Properties would work great ...
public ref class ManagedCLR
{
private:
UNMANAGED.Data *pObj;
public:
ManagedCLR()
{
pObj = new UNMANAGED.Data();
}
~ManagedCLR()
{
this->!ManagedCLR();
}
!ManagedCLR()
{
delete pObj;
pObj = 0;
}
void EntryPoint(void)
{
pObj->EntryPointUnmanged();
}
property int s
{
int get()
{
return UNMANAGED::s;
}
void set(int value)
{
UNMANAGED::s = value;
}
}
property int k
{
int get()
{
return UNMANAGED::k;
}
void set(int value)
{
UNMANAGED::k = value;
}
}
};
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
I did everything that is mentioned in these posts.I created a start button in a c# Windows Forms GUI. Now when I try to call the ManagedC++ Function from C# by clicking a start button, I get this error:
Attempt to load an unverifiable executable with fixups (IAT with more than 2 sections or a TLS section.) (Exception from HRESULT: 0x80131019)
and the program crashes.
Heres my start button code:------
private void button_Start(object sender, EventArgs e)
{
BlackMagicManaged.BlackMagicCLR Result=new BlackMagicCLR();
Result.StartCapture(); //Accessing the Managed CLR class.
}
|
|
|
|
|
BlackMagicManaged.BlackMagicCLR should be in a library (DLL), not an EXE.
Issue reported to Microsft[^]...AFAIK status is still unresolved
four years later
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
Thanks. Microsoft doesnt like CLI period. Sucks.
Geez..I have been creating so many workarounds.
|
|
|
|
|
Unable to access variable. Suppose I wanted to access a integer array a[12] stored in my Unmanaged class.
I can only declare pointers inside the ref class. So how do I access the integer array from C#. C# only allows pointers as unsafe. I need to check if each a[i] has a value <0. It seems very hard to do this? Any help?
*******************Managed.cpp*********************************
namespace Managed
{
public ref class ManagedCLR
{
public:
ManagedCLR (void)
{
pObj = new UNMANAGEDNAMESPACE.Data();
}
~ManagedCLR(void)
{
delete pObj;
}
void EntryPoint(void)
{
//pass through to the unmanaged object
pObj->EntryPointUnmanged(); //Here the managed program accesses my unmanaged program.
}
private:
UNMANAGEDNAMESPACE.Data *pObj; //Here my Unmanaged Class is referenced through a pointer.
};
|
|
|
|
|
|
Actually, how would I use properties with arrays???????????
|
|
|
|
|
Found a solution.
I did this in the managed.cpp file
namespace Managed
{
public ref class ManagedCLR
{
public:
ManagedCLR (void)
{
pObj = new UNMANAGEDNAMESPACE.Data();
}
~ManagedCLR(void)
{
delete pObj;
}
void EntryPoint(void)
{
//pass through to the unmanaged object
pObj->EntryPointUnmanged(); //Here the managed program accesses my unmanaged program.
}
int ArrayResult(int i)
{
return Unamanaged::Array[i];
}
private:
UNMANAGEDNAMESPACE.Data *pObj; //Here my Unmanaged Class is referenced through a pointer.
};
|
|
|
|
|
|
The horror!! The horror!!
Boris Jabes (MSFT) has just revealed in a VC++ blog post (in the comments no less; I wonder when we'd have found this out if Sunil Joshi had not asked about it) that Visual Studio 2010 Intellisense, you know, that incredibly fast and reliable rearchitecture of Intellisense that we've all been waiting for, will not support C++/CLI!
http://blogs.msdn.com/vcblog/archive/2009/05/27/rebuilding-intellisense.aspx
This is stunning to me. Boris's statement is effectively that "we don't think you spend all that much of your time in C++/CLI anyway!" Puh!
If you are at all invested in C++/CLI, please head over to that blog post and add a comment indicating your displeasure with this disturbing turn of events.
Thanks,
Eric
|
|
|
|
|
Wow, I thought it was just an issue in the beta. No wonder intellisense is faster! it's doing half the work. This really blows (to put it lightly).
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
Yeah, that really blows.
A main reason I dropped C++/CLI for new development a while back.
Still use it as little as possible - for interop, but that's it.
Mark Salsbery
Microsoft MVP - Visual C++
modified on Thursday, May 28, 2009 1:55 PM
|
|
|
|
|
Intellisense is for sissys
Real programmers use vi.
BTW today is Friday for me, have a great weekend
|
|
|
|
|
|
BTW, did you see the followup just below about VC10 intellisense. You are getting hooked up baby!!!
|
|
|
|
|
Heh yeah....but like you said, Intellisense is for sissys.
Intellisense won't make it a first=class .net language. I want to see C++/CLI usable
everywhere C# is, with all the same VS tools!
I spend so much more time with C# now, that when I go back to do some C++/CLI,
it feels like I can't do anything....weird
Mark Salsbery
Microsoft MVP - Visual C++
|
|
|
|
|
*** UPDATE ***
Boris Jabes has now posted a clarification in the comments of the referenced blog post stating that the lack of C++/CLI Intellisense support in Visual Studio 2010 is temporary and will be remedied as soon as possible after Visual Studio 2010 ships. This is certainly an improvement compared to the original statement, which did not say anything about the lack of C++/CLI support being temporary.
http://blogs.msdn.com/vcblog/archive/2009/05/27/rebuilding-intellisense.aspx
|
|
|
|
|
That's a great news. I remember Herb Sutter saying future versions of VS will have good support for C++/CLI. But Boris really surprised me.
|
|
|
|
|
As with all other combinations of C++, C# and different versions of Visual Studio, I suspect I'll be trusting Visual Assist X[^] to provide me with decent Intellisense
They don't yet have VS2010 support, but judging by their track record (I've been using the product for 8/9 years....), they'll have support pretty soon after VS2010 is released.
Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
|
|
|
|