|
I have a strange problem that has been driving me up the wall. I created a SSCCE that shows my problem. Compile this with cl /EHsc /clr <filename> :
#using <mscorlib.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
#include <iostream>
int main()
{
using System::Windows::Forms::DialogResult;
using System::Windows::Forms::FolderBrowserDialog;
using System::Windows::Forms::MessageBox;
double d;
std::cin >> d;
FolderBrowserDialog* fb = new FolderBrowserDialog;
if (fb->ShowDialog() == DialogResult::OK) {
MessageBox::Show(fb->SelectedPath);
}
}
When I run this program as is, after entering a number on the command line, the FolderBrowserDialog comes up blank. However, if I comment out the std::cin >> d; line, then the FolderBrowserDialog works fine. Does this happen for anybody else? Or am I misusing the mixture of managed and unmanaged code? I am using VS .NET 2003 on Windows XP SP2, with the .NET 1.1 Framework.
--
Marcus Kwok
|
|
|
|
|
OK, I found a solution that seems to work. In the article Mix and Match: Integrate Windows Forms Into Your MFC Applications Through C++ Interop (http://msdn.microsoft.com/msdnmag/issues/06/05/MixAndMatch/[^]), I found this piece of advice:If you compile with the /clr option, your code implicitly depends on the multithreaded DLL version of the C runtime libraries (CRT). This means that all files in your project—even those compiled without /clr—must use the multithreaded DLL version of the CRT. Adding /MD to the compile command seems to fix it. Incidentally, this also seems to fix my earlier problems (here and here).
--
Marcus Kwok
|
|
|
|
|
Hi,
I've got a problem with my wrapper class and my wrapped native object:
How can the wrapper be notified if the native object inside is modified?
Background: I want to wrap a library where an observer pattern is implemented. There the native observer observes a native subject where it was registered. If the subject notifies the registered observers about a change my observer-wrapper was not notified.
What should I do to make this work? I thought about something like function pointers and delegates but I think I'm on the wrong track.
Or is it possible to pass a managed object as a parameter to a native object's function?
Example:
class AbstractSubject
{
public:
virtual ~AbstractSubject() {};
virtual void Attach(IObserver*);
virtual void Detach(IObserver*);
virtual void Notify();
protected:
AbstractSubject() {};
private:
std::vector<IObserver*> observers;
};
class IObserver
{
public:
virtual ~IObserver() {};
virtual void Update(AbstractSubject* theChangedSubject) = 0;
protected:
IObserver() {};
};
As you can see the IObserver is a interface class. This interface class is wrapped by a managed c++ interface-class. The native observer object registeres itself on the Attach(IObserver*) -method and is added to the observers -vector.
If the Nofify() -method of the subject is called the subject iterates over the observers-vector and calls Update(this) (observerobject->Update(this)) on the observer object to notify the observer that something changed.
But how can I add now my managed class or my managed object to the subject to be notified? Please correct me when I'm wrong but I think I cannot pass the managed-observer-handle to the native method to notify the managed observer.
BTW: I don't have to source code for the native classes. I only have got a DLL-file, a LIB-file and some header-files to access the classes in the native lib.
Any help would be appreciated.
Thanks a lot for you help!
Best regards,
Ralf
|
|
|
|
|
I am a new C++ video game design student. I'm really not loving this C++ class cause my teacher is from tiawan and only knows like 100 words of english and when he puts them together in a sentence they are in the wrong order anyways. So i'm NOT understanding what it is he is telling us or asking of us. But i need help, or i need the code written for me or something. but we are on vectors and he wants us to make a list using 'iterators' that we can input our favorite video games. list them, and erase them. then he wants a quit fucntion to end the program.
can someone please help me.
|
|
|
|
|
You should also learn to read sticky notes in forums. This forum is for C++/CLI questions only, your question is off topic here. It's generally rude to crosspost, in any case.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
|
|
|
|
|
You are not clear, but it sounds like you want the Standard Template Library (STL)
It a simple and easy to use library. here is a simple example for you, straight from the help files which you should probably read first before posting a question:
#include "stdafx.h"
#include <vector>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<int> myVector; // an empty string vector
vector<int>::iterator myIter; // an iterator for the string vector
myVector.push_back( 0 );
myVector.push_back( 1 );
myVector.push_back( 2 );
myVector.push_back( 3 );
myVector.push_back( 4 );
myVector.push_back( 5 );
for ( myIter = myVector.begin(); myIter != myVector.end(); myIter++ )
{
cout << " " << *myIter << endl;
}
cout << endl;
return 0;
}
OrcBighter2
|
|
|
|
|
I have a problem setting a class constant in my C# assembly to the value of a constant in a managed C++ class. The compiler does not seem to recognise that the managed C++ class constant is constant. The error message is shown below:
#error = "The expression being assigned to 'MyAssembly.SMyFooBar.bahName' must be constant
I am writing in Visual Studio 2003 on WinXP SP2 with .Net Framework V2.0.50727
What Have I done wrong here?
The reason for this attempt is that the majority of our code is unmanaged c++, which uses a glabal header file to set values through a number of projects.
I wish to include this header in our various new C# projects, and thus avoid double maintenance. If the header file changes, all that is needed is a code recompile, at least in theory.
To do this I wrapped the header file inside a managed c++ class to create a dll. I then included this dll as a reference in my C# project so that I could use it to initialise some c# constants, thus:
-- unmanaged C++ header
// CPPHeader.h
#ifndef CPPHeader_defined_hpp
#define CPPHeader_defined_hpp
#define MAX_FOOBARS 3
#define FOOBAR_NAME "Foo Bar"
#endif // CPPHeader_defined_hpp
---------------------
-- managed C++ class
//MyManagedClass.h compiled to a DLL
#pragma once
#include "CPPheader.h"
using namespace System;
public __gc class SMyManagedClass
{
public:
static const int maxFooBars = MAX_FOOBARS;
static const String* FooBarName = FOOBAR_NAME;
};
--------------------------
-- C# assembly
//MyFooBarAsembly.cs
using system;
using SMyManagedClass;
namespace MyAssembly
{
public class SMyFooBar
{
public const int maxBahs = SMyManagedClass.SMyManagedClass.maxFooBars;
public const string bahName = SMyManagedClass.SMyManagedClass.FooBarName;
}
}
OrcBighter2
|
|
|
|
|
No way, your mc++ declaration says the constans are static. static are not considered as constants. Thats why teh error.
static const int maxFooBars = MAX_FOOBARS;
static const String* FooBarName = FOOBAR_NAME;
So to overcome this, you have to remove taht static. but then you cant use the values without creating the instance. so the alternative is to declare an enum for that insted of a class.
public enum class SMyManagedClass<br />
{<br />
maxFooBars = MAX_FOOBARS<br />
}
But here inside enum u cant use strings. So for string items u can create a managed class and inside a hash table to host all tthe strings. then an index enum can puul the corresponding string,...
cheers..Milton Kb
|
|
|
|
|
Thanks for your reply.
Sorry, but I don't quite see what you are getting at.
The const in mc must be static in order to initialise it with a value.
For me, the end result must be that a string constant defined in the C# assembly must be able to be initialised to the value of a constant from the managed C++ class.
The code I provided works fine for integer constants, however, the C# does not regard the C++ string constant as a constant, and thus will not allow me to initialise the C# constant to that value.
|
|
|
|
|
What is the best way to integrate an MFC-component in a .Net application?
The MFC-component I want to use resides in an mfc-dll and consists of many c++ classes with big usage of the MFC-classes. I tried so far to use IJW with MC++ wrapper classes but the application crashes all the time in some new and I don't want to use PInvoke because then I loose the object oriented design. How about converting the MFC-component into a COM object? Can I use the MFC-component this way in a .Net application?
All suggestions are welcome.
Cheers
gunag
|
|
|
|
|
U should be able to make it working through IJW. whats the erro u are getting? Though converting to COM is a possible alternative, as far as workload is concerned IJW is best. Performancewise also IJW is better than Com interop according to msdn.
cheers..Milton KB.
|
|
|
|
|
Hi Milton KB,
I get lots of crashes related to new operators. Each time I allocated with new a CDC object, it might crash. I have the feeling that during the new operation there might be a conflict between the managed and unmanaged heap. Do you have any idea what i could do in this case?
|
|
|
|
|
There shouldn't be any crash normally. can u b more specific by posting some code which causes error..
Milton KB
|
|
|
|
|
Im trying to create an AsyncCall back instance but i get a weired error :
Code:
Static void function()
{
...
AsyncCallback* callback = new AsyncCallback(TestMethod, &TestMethod);
...
}
the error is:
error C3363: 'void ClientRequestReceived(System::IAsyncResult __gc *)' : cannot create a delegate handler for 'System::AsyncCallback' from a non-member function or a member of an unmanaged class
how do i fix this error?
Thanx in advance
Upon my end shall i begin?
Forsaking all i've fallen for..
i rise to meet the END!!
|
|
|
|
|
In C/C++ can any "macro" take variable number of arguments? Is it possible any way?
Pradosh
|
|
|
|
|
depends of the compiler. with VC6, i'm sure you can't, but on later versions, i don't remember how you can do such macros though
|
|
|
|
|
|
It's working in VC++ 8. I could not check it in any other compiler.
Pradosh
|
|
|
|
|
% is for tracking references to CLI object
& is for passing native heap objects
If I use % and pass a native object, what is the compiled behavior? In other words given the following trivial example, does the code for x and y end up being the same, or does x box the passed value and then unbox it?
void Test(int% x, int& y)
{
x++;
y++;
}
void SomeFunction()
{
int x = 0;
int y = 1;
Test(x, y);
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke
|
|
|
|
|
Seems like x would automatically get boxed. You can run ildasm on the compiled binary and see if this is the case.
I could've sworn Nish had an article on & and % but I can't find it right now.
--Mike--
Visual C++ MVP
LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
|
|
|
|
|
hi friends,
i am infront of you with another doubt. We are using cin and cout in C++. Whether these are class or object.
if they are object how can we use them without declaring that it belongs to this class. Thanks for u in advance.
Shamnar
shamnar
|
|
|
|
|
cin is a class...
well how do you percept a difference between class and object?
|
|
|
|
|
NrmMyth wrote: cin is a class
no, cin is an instance of a class (std::ofstream class).
the difference between a class and an object is that a class defines a type, while an object is an instance of that class (an instance of the type of the class)
|
|
|
|
|
my bad, weren't thinking
also, i never used word "object" for "instance", personal opinion
|
|
|
|
|
well, i not only don't understand what you mean, but for the second time, i tell you that you ask about UNMANAGED C++, in a Managed C++ forum... this is the wrong place !
BTW, see my reply to the other guy to get some infos. the point is that cout and cin are instanciated at the beginning of your program (implicitely)
|
|
|
|