|
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
|
|
|
|
|
Thank you very much for your answer.
What I understand is both are .Net via C++. Difference is syntax.
|
|
|
|
|
Exactly. Glad I could help.
John
|
|
|
|
|
<b>Hi
I need help...
I have an array of string private variable List<String^>^ CalculateGreeks_; having property for get and set List<String^>^ CalculateGreeks() in a class CArgsRequestCredential,
I want to put these variables
PV,Delta,Vega
in my local object
<b>ReqCredObj_ = gcnew Args::CArgsRequestCredential();</b>
first i need to split strings saving each string in a variable and then storing variables in the list..
my code is
List<String^>^ CalculateGreeks1 = gcnew List<String^>();
for(int i = 0 ; i <= LocalArray->GetUpperBound(0);i++)
{
//
//CalculateGreeks1->Add(ii);
String^ Test = Convert::ToString(LocalArray->GetValue(i,0));
array <String ^>^ strings = Test->Split(',');
//ii = gcnew List<String^>^ CalculateGreeks_();
//ii->CalculateGreeks = Convert::ToString(strings);
// CalculateGreeks1->Add(strings);
// ReqCredObj_->CalculateGreeks = Convert::ToString(LocalArray->GetValue(0,1));
}
but my logic is not giving correct values
kindly help
|
|
|
|
|
Hello,
My question seems to be simple, but I stuck with it for long time.
Why it is at all needed more than one project in solution?
How to add multiple projects in one solution?
How to use them?
Thanks in advance.
|
|
|
|
|
Pranit Kothari wrote: Why it is at all needed more than one project in solution? There are several reasons why you might want to do this, including:
1. You have a static library with rarely-changed code, linked to your app.
2. You have a dll - for example, a shell extension - and an app that presents the gui.
3. You have a dll and several test apps.
Pranit Kothari wrote: How to add multiple projects in one solution? Right click on the solution in the Solution Explorer, then click on Add > New Project (or Existing Project).
Pranit Kothari wrote: How to use them? See answer to first question.
|
|
|
|
|
Thanks for your answer.
What I have guessed from your answer is, suppose I have some set of classes that I am creating in one project and I will create another project and use that library. It it correct?
If yes, may I know how to use it? Is it related with assembly or namespace?
|
|
|
|
|
That is correct, look up dynamic and/or static dll use in Google. Having the ability to have all of your dll's in the same workspace is very advantageous for debugging the dlls themselves or interactions between the application and dll.
|
|
|
|