|
As far as I know, safe_cast is more like dynamic_cast (which you can also use on managed types). safe_cast and dynamic_cast are for casting between handles to polymorphic types within a class hierarchy (much as you would do in C++ with pointers). The difference is that safe_cast throws an exception when the cast is not possible rather than returning nullptr. This means you don't have to write code that checks to see whether the cast succeded, just handle the exception, meaning potentially less cluttered code.
|
|
|
|
|
Just wondering how and if you can pre-select multiple files as the dialog opens? Do you place a list of them in the Initial Filename area, as I've tried this already the answer seems to be, No.
Am I missing something?
Basically I'd like a user to be able to select multiple files, I then do some checking of the filenames and if one of these checks comes back false, I want to reload this dialog with the files that the user had previously selected.
Any help would be greatly appreciated
Hayden
CFileDialog dlg(FALSE,NULL,"",OFN_ENABLESIZING|OFN_EXPLORER|OFN_FILEMUSTEXIST|OFN_ALLOWMULTISELECT,"All Files(*.*)|*.*||");
const int nBufferSize = 128*1024;
TCHAR *szNewBuffer = new TCHAR[nBufferSize];
memset(szNewBuffer, 0, sizeof(TCHAR) * nBufferSize);
dlg.m_ofn.lpstrFile = szNewBuffer;
dlg.m_ofn.nMaxFile = nBufferSize - 1;
if (dlg.DoModal() == IDOK)
{
POSITION pos = dlg.GetStartPosition();
CString fullpath;
CString filenametemp;
CString filename;
int nLastSlashPos;
filePaths = "";
fileNameList = "";
while (pos)
{
fullpath = dlg.GetNextPathName(pos);
nLastSlashPos = fullpath.ReverseFind('\\');
if(nLastSlashPos >= 0)
{
filename = fullpath.Mid(nLastSlashPos+1);
}
if (filePaths != "")
{
filePaths = filePaths + "|";
fileNameList = fileNameList + " ";
}
filePaths = filePaths + fullpath;
fileNameList = fileNameList + '"' + filename + '"';
}
}
|
|
|
|
|
Hi!
I've a file called test.ini. The contents of this file is:
PreviousCheck =true
LID =ratha
PWD =rtrrules
I've to read the file and assign each value after the equal(=) sign to three System::String^ variables. I don't want the text preceding the equal sign(i.e PreviousCheck =,LID = and PWD = all I don't want). How to do this in C++/CLI?
|
|
|
|
|
Firstly, what you are doing is better suited to the app.config key/value approach. But to answer the question investigate the 'Split' method on the String^ class in C++/CLI to achieve what you want.
Ger
|
|
|
|
|
Hi!
I used the following code to get a line from the file(and then I will split it into parts).
System::IO::StreamReader reader("test.ini", System::IO::FileMode::Open);
System::String^ line = reader.ReadLine();
But I got the following error:
error C2664: 'System::IO::StreamReader::StreamReader(System::IO::Stream ^,bool)' : cannot convert parameter 1 from 'const char [17]' to 'System::IO::Stream ^'
Reason: cannot convert from 'const char *' to 'System::IO::Stream ^'
No user-defined-conversion operator available, or
Cannot convert an unmanaged type to a managed type
How to get rid of this error?
|
|
|
|
|
I suggest putting "Test.INI" in a variable defined as String^ e.g.
String^ FileName;
FileName = String::Format("Test.INI");
Finally when you are splitting your input strings define the index character as wchar_t
Ger
|
|
|
|
|
T.RATHA KRISHNAN wrote: How to get rid of this error?
I think this is a subtle one; you have provided a char* in parameter 1 of the constructor, but no such constructor exists. You need to use a String object as the path parameter for the compiler to select the correct constructor. You can find all the gory details here[^] on MSDN.
I must get a clever new signature for 2011.
|
|
|
|
|
This seems to work:
#include "stdafx.h"
using namespace System;
int main(array<String ^> ^args)
{
array<String^>^ lines = System::IO::File::ReadAllLines("C:\\temp\\test.ini", System::Text::Encoding::UTF8);
Console::WriteLine("Key\t\tValue");
for each (String^ line in lines)
{
if (String::IsNullOrWhiteSpace(line))
continue;
int offset = line->LastIndexOf("=");
if (offset >= 0)
{
String^ key = line->Substring(0, offset)->Trim();
String^ value = line->Substring(offset+1)->Trim();
Console::WriteLine(key + "\t\t" + value);
}
}
return 0;
}
John
|
|
|
|
|
Hi all!
I have big experience in the unmanaged C++. And have almost no experience in all these NET things. So, I started a project using managed C++ yesterday. Looks, like it will put me to the grave...
For the context. I'm writing a plugin for ClearCanvas application. My plugin already have some classes that are inherited from the classes in the ClearCanvas SDK. Plugin is loading and working well.
Now imagine the situation.
I define a new class that inherits the simple interface. The inheritance model is:
IDisposable->IRenderingSurface->MyClass
IRenderingSurface is just a simple interface that declares 4 properties without implementation of them.
So I implement my class like this:
ref class VolumeRenderingSurface :
public ClearCanvas::ImageViewer::Rendering::IRenderingSurface
{
public:
VolumeRenderingSurface(System::IntPtr windowID, int width, int height);
~VolumeRenderingSurface();
property System::IntPtr WindowID
{
virtual System::IntPtr get() {return m_windowID;}
virtual void set(System::IntPtr id) {m_windowID = id;}
}
property System::IntPtr ContextID
{
virtual System::IntPtr get() {return m_contextID;}
virtual void set(System::IntPtr id) {m_contextID = id;}
}
property System::Drawing::Rectangle ClientRectangle
{
virtual System::Drawing::Rectangle get() {return m_clientRectangle;}
virtual void set(const System::Drawing::Rectangle rect) {m_clientRectangle = rect;}
}
property System::Drawing::Rectangle ClipRectangle
{
virtual System::Drawing::Rectangle get() {return m_clipRectangle;}
virtual void set(const System::Drawing::Rectangle rect) {m_clipRectangle = rect;}
}
private:
System::IntPtr m_windowID;
System::IntPtr m_contextID;
System::Drawing::Rectangle m_clientRectangle;
System::Drawing::Rectangle m_clipRectangle;
};
The constructor and destructor are defined but empty. I even DONT use this class anywhere, just define it.
And I try it. Everything is compiled without warnings, but when ClearCanvas attempts to load plugin - it fails.
"first chance exception of type 'System.BadImageFormatException' occurred in mscorlib.dll"
Well, next point. If I comment the inheritance from interface it works!
In my project, I have few classes that are inherited exactly the same (I mean IDisposable->ISomeSimpleInterface->SomeClass) and they work!
What a hell!?
Here is the code for IRenderingSurface interface, it's in C#. Nothing interesting:
public interface IRenderingSurface : IDisposable
{
IntPtr WindowID
{
get;
set;
}
IntPtr ContextID
{
get;
set;
}
Rectangle ClientRectangle
{
get;
set;
}
Rectangle ClipRectangle
{
get;
set;
}
}
Thank you.
|
|
|
|
|
Wow, I found a problem. Although this is my (no big) mistake, the result is just unexpected.
virtual void set(const System::Drawing::Rectangle rect) {m_clientRectangle = rect;}
Should be replaced with:
virtual void set(System::Drawing::Rectangle rect) {m_clientRectangle = rect;}
No const! Well I know const doesnt mean here anything. I've tried to use const reference from the very beginning. Then it failed to compile, I removed reference, but forgot to remove const.
As a result - 5 hours wasted because of const that actualy doesnt mean anything!
Fairy .NET 
|
|
|
|
|
When I add memnber functions in Visual Studio (C++) I always direct them to the .cpp file. However when I add form events, it insists on putting both the declaration and function body in the .h.
Now in VS 2008, when I move the function code to the .cpp file, the IDE looses track of it. How do I configure it to go there automatically?
Ger
|
|
|
|
|
Adding a member function in VC++ Express 2010 - Do I have to do this by hand? When I right click on the Class, in the browser, the Add option is not on the menu function.
Ger
|
|
|
|
|
Hi
Do we have ability to write inline assembly code in managed C++/CLI
Thanks in advance!
|
|
|
|
|
If you want to mix the asm into C++/CLI code you'd either need to use #pragma unmanaged or put the inline asm in a function within a separate file compiled without /clr.
A .cpp file built with /clr which includes inline assembly will get an error like this:
1> ..\myfile.cpp(205): error C2711: 'CMyApp::Test' : this functon cannot be compiled as managed, consider using #pragma unmanaged
1> Inline native assembly not supported in managed code
John
|
|
|
|
|
Hi guys
In C++/CLI what is the difference between using the concept of IJW (It Just Works) and using pragma directive as following:
#pragma managed(push,off)
UnmanagedFunc(){
…native code …
}
#pragma managed(pop)
Does IJW is enabled automatically (so just need to write our native code) or we need to do something to make it works..??
In other mean, what is the advantage of IJW over using pragma directive or the other way around??
|
|
|
|
|
IJW is just a term they used back in 2003/2004 to talk about how the managed-unmanaged transitions and marshaling were smoothly handled for you by the compiler. No one uses the term these days, and instead people just say C++ interop to refer to anything where mixed-mode code comes into play.
#pragma unmanaged is a pre-compiler directive that tells the compiler that any code in that block must be compiled natively (meaning no msil at all). It's mostly used to ensure that frequently used code is compiled for better performance (which usually implies native code generation).
|
|
|
|
|
Thanx Nish
If I use standard C function inside CPP/CLI code .. like for example:
ManagedFunction(){
..... Managed Code .....
..... Managed Code .....
printf("I am standard C function!\n");
..... Managed Code .....
..... Managed Code .....
}
Does MSIL do something with the printf() function! (Notice that i did NOT use #pragma umanaged directive!)
If MSIL will do nothing with printf() so (as i think) printf() will be compiled as native??So why I need to use #pragma umanaged directive.
|
|
|
|
|
The call to printf will be emitted via msil. The printf method itself will execute as native code (there will be a managed to unmanaged jump during execution).
Now instead of printf , assume you were calling your own global function Foo . Now if you did not put Foo 's definition in a #pragma unmanaged block, Foo itself will be compiled as msil. That's a scenario where you'd actually want to use #pragma unmanaged , particularly if you think Foo is a time-intensive function.
|
|
|
|
|
Great man... It is more clear now. Thank you.
|
|
|
|
|
What is Difference between Managed C++, C++/CLI and C++.Net?
Also give name of some good books for all of these.
I am MFC/Win32 programmer. In current project we are using WPF and code behind is Managed C++. But I am not sure it is C++.Net or what.
I know C#.Net as well.
Thanks in advance.
|
|
|
|
|
Managed C++ runs under the 'managed' environment which is .NET. C++/CLI and C++.NET are merely different names for the same thing. Take a look at MSDN for more information.
I must get a clever new signature for 2011.
|
|
|
|
|
Richard MacCutchan wrote: Managed C++ runs under the 'managed' environment which is .NET. C++/CLI and C++.NET are merely different names for the same thing. Take a look at MSDN for more information.
Actually it is said that Microsoft has deprecated Managed C++ for C++/CLI. I think there is difference.
Please refer this
http://en.wikipedia.org/wiki/Managed_Extensions_for_C%2B%2B[^]
Thanks for your answer.
|
|
|
|
|
So you knew the answer all along.
I must get a clever new signature for 2011.
|
|
|
|
|
Actually Richard, I know your answer was also same and correct, but problem was of my understanding. 
|
|
|
|
|
Managed C++ is the older, deprecated syntax for using .NET libraries via C++. It had lots of nasty __ prefixes.
C++/CLI has much nicer keywords for declaring and using managed classes.
The best book I've found is "C++/CLI in Action" by Nishant Sivakumar from Manning Press.
http://www.manning.com/sivakumar/
John
|
|
|
|