|
Hello everyone... Im just starting a new project using C++/CLI. Can some one please tell me why
this code does not work?
std::string nativeStr = "Hello World";
String^ managedStr = nativeStr.c_str();
but this code does work
std::string nativeStr = "Hello World";
String^ s6 = gcnew String(nativeStr.c_str());
howevet i want to be able to create my managedStr on the stack, and would expect this to work..
Can anybody tell me where i am going wrong
|
|
|
|
|
flippydeflippydebop wrote: i want to be able to create my managedStr on the stack, and would expect this to work
Then you don't understand how the .NET Platform works. Try reading some introductory programming articles on MSDN[^] Actually there might even be some here on CodeProject if you care to look.
led mike
|
|
|
|
|
flippydeflippydebop wrote: howevet i want to be able to create my managedStr on the stack, and would expect this to work..
LedMike has the correct answer.
To add...creating a managed object on the stack is a redundant/silly concept. It's all about
scope here. Creating it the way you've shown in the working sample gives it the same scope
as a stack variable.
Mark
"If you can dodge a wrench, you can dodge a ball."
|
|
|
|
|
well hang on then guys...
Why does this:
System::String^ myStr = "hello"
Work fine even though i do not use the gcnew operator to allocate the string 'myStr' on the heap?
What i am saying is that i know in the .net world everything is allocated on the heap and that the above statement, even though it appears to reside on the stack is probably being allocated on the heap..
Now if the compiler lets me write code like above, then why wont it let me write code like this:
String^ managedStr = nativeStr.c_str();
The above line fails.. Instead in this instance i have to explicitly allocate the variable managedStr on the heap like so:
String^ s6 = gcnew String(nativeStr.c_str()); // works!
|
|
|
|
|
When you do this
System::String^ myStr = "hello" myStr gets a reference to a pointer that already exists in the app's (assembly's) user strings
metadata section.
When you do this
String^ managedStr = nativeStr.c_str(); a System::String constructor is required. To use the constructor the object needs to be created
on the managed heap.
I tracked down a better description than I could possibly give:
Scroll to the second question/answer here[^]
Hope that helps!
Mark
"If you can dodge a wrench, you can dodge a ball."
|
|
|
|
|
Yes because "hello" is treated as the type (System.String) by the CLI compiler. Look at the MSIL code to see the difference. Also note that: System::String^ myStr = (const char*)"hello"; does not work.
See Stan Lipman's blog[^]
The insight to resolve this is to realize that the dual citizenship of a string literal applies to its fundamental type, not to its set of trivial conversions. In effect, under C++/CLI, the underlying type of a string literal such as "Pooh" is both const char[5] (its native inheritance) and System::String (its managed underlying unified type). Under C++/CLI, the string literal is an exact match to System::String and the trivial conversion to const char* is not considered. That is, under the revised C++/CLI language specification, the ambiguity has been resolved in favor of System::String
led mike
|
|
|
|
|
ahh-haaaa... i see (said the blind man). Well, guys it would seem i need to do some more reading of the subject material.
Thankyou both for taking the time to reply.
|
|
|
|
|
how to disable print screen using c++.i tried but i can't get answer.So please give if you know how to disable print screen using in windows operating system
srinivas
|
|
|
|
|
|
Hi guys
Can any one temme what does this mean
void AddtoNode(char*& Content, const char*& Buffer);
I am not getting this char*& part. Is it pointer to pointer or wt.
Thanks
|
|
|
|
|
I believe it is a "reference to a pointer."
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
but what does it mean....?
|
|
|
|
|
|
A reference for something means that it can be used anywhere the original can be used, AND any changes to the reference are reflected in the original.
So for instance:
void func( int a, int& b )
{
a ++;
b ++;
}
int foo = 5, bar = 5;
func( foo, bar );
In this example, foo will remain unchanged because it was passed by value to the function, however, bar will reflect the increment because it was passed by reference.
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
Hello,
It's similar to pointer to pointer (char**).
Regards,
|
|
|
|
|
I want to host a NotifyIcon control inside my MFC dialog box. The dialog box remains hidden, and the NotifyIcon will be the app's only UI.
The problem is that when I want to Show a context menu for the NotifyIcon, it requires me to pass in the handle to a Windows::Forms:Control.
Since my app does not have a Windows::Form, but rather an MFC dialog, how can I force it to show the context menu on demand?
I thought about just creating a temporary Form, but it won't work if the Form is invisible!
How can I force it to show the context menu inside my MFC dialog?
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
This question should be posted on the Visual C++ / MFC[^] board.
I'm confused ... you should be able to show a popup menu anywhere on the screen you want.
You say the dialog remains hidden but you want to "show the context menu inside my MFC dialog"??
By "NotifyIcon" you are referring to an icon in the "tray", right?
Mark
"If you can dodge a wrench, you can dodge a ball."
|
|
|
|
|
Thanks for your response.
This is a legitimate C++/CLI question because I am building a mixed-mode application.
Inside the MFC dialog, I am trying to host the .NET class "NotifyIcon".
But there is no method to tell the NotifyIcon to show its ContextMenu, so I hope someone knows how I can accomplish this.
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
Fair enough
I'm not sure how you'd accomplish this. In an MFC window (Windows OS window) you provide a
popup menu and display it typically in response to the user clicking the tray icon.
Seems to me (since ContextMenu.Show() requires a form) you need to maybe call the windows
TrackPopupMenu() API using the HMENU obtained from the ContextMenu (Menu.Handle property).
Pass the dialog's HWND - the dialog will receive the command messages.
Maybe?
Mark
"If you can dodge a wrench, you can dodge a ball."
|
|
|
|
|
Mark, I think you are right.
I think I won't be able to get MFC and .NET to "play nice" in this particular instance, and I will have to revert to my native implementation of the tray icon.
Thanks for your thoughts.
--------------------------------
"All that is necessary for the forces of evil to win in the world is for enough good men to do nothing" -- Edmund Burke
|
|
|
|
|
I had created class in one classes.h file. I included this file in Form1.h, created Class^ c = gcnew Class(); and call one of function. When I wanted to run my procjet I get error LNK2005: "public: void __clrcall class::Prepare(class System::String ^)" (?Prepare@class@@$$FQ$AAMXP$AAVString@System@@@Z) already defined in Myproject.obj
Where could I do wrong?
|
|
|
|
|
As error suggest its due to defining class::Prepare in more that one place.
If this definition is inline, make sure, your header is guarded by multiple inclusion.
i.e. We use #pragma once is native.
|
|
|
|
|
I added #pragma once in first line of classes.h but I still get that error
|
|
|
|
|
I dont know if this is the same linking error that I encountered once, but any way try to delete the debug folder from your project, you will find 2 debug folders inside your project, delete the inner one then rebuild your solution.
|
|
|
|
|
hi,
I am trying to develop a programme to let the user key in their name, then compare their name to the input file i have created using a notepad. Can anyone suggest me how to check the name key in by the user to the names in the input file? The programme which i am trying to develop is some sort like a programme to identify the user and hence tell them what class do they need to attend,when,where.
THANKS.
|
|
|
|