Visual Studio 2017: namespace "System" has no member "Windows" - c++

I recently installed the CLR package for Visual C++ and am making my first GUI. For some reason System is not showing all of the options that it has in the past. There are red lines under everything after System::* with similar errors. Here is the start to my header file.
#pragma once
namespace Form1 {
using namespace System;
using namespace System::Windows;
using namespace System::ComponentModel;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
While searching for a solution I found this thread, and followed the steps
Set Common Language RunTime Support to be "Common Language RunTime
Support (/clr)" in 2 places in your project properties :
Configuration Properties -> General
Configuration Properties -> C/C++ -> General
But that was already the default. Any suggestions?

Try add these references by :
Go to Project > References > Common Properties > Framework and References
Click Add New References... button
Under Assemblies -> Frameworks check System."Your needs" and click ok.

Related

Getting 2 errors, the project only has a standard windows form

I'm new to programming and I just made a windows form in my new project, I've changed the project properties and I still get these 2 errors:
The SDK 'Microsoft.NET.Sdk' specified could not be found.
command-line error: cannot open metadata file "System.Runtime.dll"
I don't really know what to do and I'd like some help.
It seems that you are creating a C++ winform project. After modifying project properties, do you add below codes into your form.cpp:
using namespace System;
using namespace System::Windows::Forms;
[STAThreadAttribute]
void Main(array<String^>^ args) {
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
//WinformCDemo is your project name
WinformCDemo::MyForm form;
Application::Run(%form);
}
And then, you need to re-open visual studio. The workround: https://social.msdn.microsoft.com/Forums/en-US/a9529502-6304-4aa6-90ee-0757ab258d87/create-c-windows-forms-application-in-visual-studio-2017?forum=winforms

How to create a VTK project in Windows Form Project (VisualC++ 2012 )

I'm trying to embed scene created with Visualization ToolKit (VTK)library into VisualC++ 2012 created windows form so I can design my Windows native GUI interface.
I'd like to underline that, all examples with console app are configured with (Cmake), compiled with VC++2012 and works flawlessly, as instructed by the official VTK wiki page.
The issue is, if I try to call those VTK functions and class initializations inside of Win Form application I get the Error LNK1107: invalid or corrupt file: cannot read at 0x2E0 D:\.....\VTK_61_BUILD_VS2012\bin\Debug\vtkViewsCore-6.1.dll even if I add everything normally as expected, include headers and external library dependencies.
This makes me think that I'm originating from wrong Visual C++ 2012 project template or something obvious that I'm completely missing, otherwise compiler would arise many not found files or syntax error.
This is the first lines where I'm trying to invoke the VTK library, even the intellisense suggest the vtk..... named proc,functions and structures, but application fails to compile.
#pragma once
#include <vtkSmartPointer.h>
#include <vtkTriangle.h>
#include <vtkCellArray.h>
#include <vtkPolyData.h>
#include <vtkRenderWindow.h>
namespace CLR_Project1 {
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Windows::Forms;
using namespace System::Data;
using namespace System::Drawing;
...
...
vtkSmartPointer<vtkPoints> points = vtkSmartPointer<vtkPoints>::New();
What is the problem here ?
The following link (in particular the answer of Mr. bnsteel) helped me very much, when I had the same problem ==>
http://vtk.1045678.n5.nabble.com/VTK-and-Winforms-integration-td5721086.html
You can use Swig as it is suggested under the link. But it is not necessary. Using the similar way (passing the windows form panel handle) you can create a C++/CLI wrapper as jalal sadeghi proposed. You will pass the panel handle through the wrapper to your C++ library that does all the "VTK work". This way you don't have to create individual wrappers for all VTK classes, all the "VTK work" and dependencies on VTK stay hidden in your C++ layer.
Something like this ==>
C++ side
setImageRenderWindowParentID(void *theID){
... (init your vtk render window)
renWin->SetParentId(theID);
}
C++/CLI side
void setRenderWindowParentID(IntPtr parentID, .. also pass the panel size .. ){
void* p = parentID.ToPointer();
myCPPVTK->setRenderWindowParentID(p, .. also pass the panel size ..);
}
C# side
VTKWrapper.setRenderWindowParentID(m_panel.Handle, .. also pass the panel size ..);

How can I use the System.Net namespace into a Visual Studio 2010 c++ project?

I'm having troubles to import System.Net namespace from .NET into my C++ project.
I've tried it by using: "using namespace System.Net" but I guess I'd need to import some DLL to my project properties in order to make this statement work. How can I do this? Does someone have any opinion on how to overcome this problem? Or what am I doing wrong?
In your C++/CLI code, you can do:
#using <System.dll>
using namespace System::Net;
in addition to specifying
using namespace System::Net;
you also need to add a reference to the System.Net assembly...
do the following:
Right Click project and choose properties
Click the "Common Properties" item in the left tree view
Click the "Add New Reference" button
In the "Add Reference Dialog", select Assemblies/Framework in the left tree view
Find "System.Net" in the list box on the add add it to your project

Native dll built with /CLR flag containing a managed class

I have a old native MFC/c++ dll I have managed to get it compiled with /CLR flag.
Now I have added a managed C++/CLI class to the dll within a namaspace.
The header file is below, and the cpp file only has #include for the header file.
The native dll is a huge dll project with lot of un managed code, but it has only one managed c++ class like below.
When I add that dll as a reference to a .net winforms project I don't see that namespace / class, in the object browser,
and I get compile error for not finding the namespace "ShashiTest"
I am using Visual studio 2008.
Native dlls in mixed mode can not be added as reference to a managed project ?
Or am I missing something
Please help.
#pragma once
#using<mscorlib.dll>
#using<system.windows.forms.dll>
// Class derived from Forms
using namespace System::Windows::Forms;
using namespace System;
using namespace System::ComponentModel;
using namespace System::Collections;
using namespace System::Diagnostics;
using namespace System::Windows::Forms;
namespace ShashiTest {
/// <summary>
/// Summary for test
/// </summary>
public ref class test
{
public:
test(void)
{
};
void ShowMessage()
{
MessageBox::Show("Hello World");
}
};
}
When I simplified my problem..I created a fresh MFC dll and added a manged C++ class to it (same class as above) . Compiled with /CLR flag.
When I add this dll to winforms project and run it i get
System.BadImageFormatException. Any clue ?
However I see the class and the name space and winform project compiles fine unlike the problem I have the above.
System.BadImageFormatException is usually caused by having an AnyCPU .NET application reference a DLL containing 32-bit native code. You get an error when running on a 64-bit platform, because the AnyCPU application runs as 64-bit, and can't load the DLL. The fix for this is (easy) to mark the application as x86-only or (hard) to provide both 32-bit and 64-bit versions of all DLLs containing native code.
Of course, you could have some other problem. Checking your DLL with Red Gate Reflector as suggested by #cdleonard in the comments is a great next step. Or ILSpy, which is free.

How to use Directory.GetFiles

I want to use Directory.GetFiles to read files from a folder. From msdn website i found:
For c, i have include:
using System;
using System.IO;
For C++, i have include:
#using <mscorlib.dll>
using namespace System;
using namespace System::IO;
as header.
For the C++, after the line #using is added, the "fatal error C1190: managed targeted code requires a '/clr' option" appears. So i tried by adding /clr in the visual studio Properties\Linker\Command Line as i found it in other website, but i fail too.
I did it wrongly. So, How to handle error of "fatal error C1190: managed targeted code requires a '/clr' option"? How to "compile with the /clr flag"?
Because i am writing for OpenCv, i have include the following additional dependencies in order to use c and c++:
cv210d.lib
cxcore210d.lib
highgui210d.lib
cvaux210d.lib
Cannot work. What other requirements in order to use Directory.GetFiles?
I also cannot use foreach even i #include .
Is there something similar to use foreach and Directory.GetFiles which is necessary to be included in a project?
I am using Visual Studio 2008
Please help me. Tq
You need to create managed c++ CLI, this should include all the flags for you.
To set /clr from visual studio, go to porject properities->configurations properities->general set common language runtime support to /clr