How is array<System::String ^> standard C++? - c++

After choosing to create a new console application in Microsoft Visual Studio 2010, it created the following source file for me:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
What are those caret (^) characters?
It doesn't look like Standard C++.
They are not digraphs.
Nevertheless, it builds without any errors.

It's not standard C++, it's a language created by Microsoft called “C++/CLI”.
On StackOverflow, use the c++-cli tag if you have questions about this language. Here's what the tag info says:
C++/CLI is based on C++, modified to allow compilation of a mixture of native code and code for Microsoft's Common Language Infrastructure (CLI). It replaces Microsoft's Managed Extensions for C++, which aimed for stronger C++ conformance.

Related

WinRT GetSystemIdForPublisher() unable to execute due to C++20 coroutines?

I am trying to get the system ID using
auto info = winrt::Windows::System::Profile::SystemIdentification::GetSystemIdForPublisher();
auto id = info.Id();
auto asHex = winrt::Windows::Security::Cryptography::CryptographicBuffer::EncodeToHexString(id);
But when I try to run this code I get errors E0035 and C1189 and both have the exact same description:
The <experimental/coroutine> and <experimental/resumable> headers are only supported with /await and implement pre-C++20 coroutine support. Use coroutine for standard C++20 coroutines.
(Note: the italic coroutine above is inside <>, as soon as I put <> around the a word it disappears so I have omitted it)
I am including all the relevant files in my header that I think might be needed:
#include <Windows.h>
#include <coroutine>
#include <windows.foundation.h>
#include <winrt/Windows.System.Profile.h>
#include <winrt/Windows.Security.Cryptography.h>
I am using:
Microsoft Visual Studio Community 2019
Microsoft Visual C++ 2019
Windows 10 SDK 10.0.19041.0
In your project, if you go to
Property Pages ->
Configuration Properties ->
General ->
C++ Language Standard,
do you by any chance have it set to "ISO C++20 Standard (/std:c++20)"?
C++/WinRT only runs C++17.
A few hours ago I realized that C++/WinRT exists, and I am currently trying understand the fundamentals of it. If my memory serves me correctly, I also got this exact error message when trying some not-at-all-copy-pasted-from-msdn code out. Although I am using Visual Studio 2022 (Community Edition), I do not see why that would make a difference.
I hope this is of any use.
Kind regards,
a not so experienced programmer

Using Powershell class in C++ application

The CLR option(compile with /clr) is necessary to use the managed code(ie. Powershell class) in unmanaged C++ code.
A sample code is as follows:
using namespace System;
using namespace System::Collections::ObjectModel;
using namespace System::Management::Automation;
int main() {
PowerShell^ ps = PowerShell::Create();
ps->AddScript("Get-ChildItem C:\\");
ps->Invoke();
return 0;
}
After writing a code, I noticed that the compile option /MT and /CLR are incompatible. Then, I knew that the redistributable package is necessary to support the various executing environment.
Is there any solution not using redistributable package?
Is there any solution which enables to embed Powershell script in C++ application?

How to use a C struct in C++ code?

I am trying to write a program that should use a C library (the LIS library) in a C++ program. There seems to be a problem with the creation/initialization of struct objects.
When I run the example program on the wikipediapage: http://en.wikipedia.org/wiki/Lis_%28linear_algebra_library%29 it runs like a charm, but of course that is compiled as a C program.
In my C++ code I do it as follows:
#include "stdafx.h"
#include <iostream>
extern "C"
{
#include "lis.h"
#include "lis_config.h"
LIS_MATRIX A;
}
using namespace std;
int main(LIS_INT argc, char* argv[])
{
lis_initialize(&argc, &argv);
lis_matrix_create(LIS_COMM_WORLD, &A);
getchar();
return 0;
}
When I run this code, it gives me an access violation at the line lis_matrix_create. It seems as though A has an memory address, its data members (LIS_MATRIX is defined as a struct in Lis.h) have not been initialized, and therefore their addresses are NULL.
Could you please tell me how to create the LIS_MATRIX in such a way that I can use it like it is done in the example code on the wikipedia page?
Thank you in advance!
In reply to Adam and Ross Ridge:
I use visual studio 2013 on Windows 7 64 bit. The manual of the Lis library states that it is compatible with the Visual Studio 2008, 2010 and 2012 compilers, and also with gcc 3.4 and 4.4 and some IBM, Intel and PGI C++ compilers, I hope Visual Studio 2013 will not be a problem.
Also, in this code, if I take out the 'extern C' block, and include 'stdio.h' instead of iostream, it runs without problems (so I guess that it means the C compiler is used?). The minute I also include iostream, the access violation start.
You are including
lis_config.h
after
lis.h
wich is per se an error(you have to include it before). Also if you touched anything in lis_config you have to rebuild the whole library (using most same compilers flag of your project, for example "-msee2" if you used SSE2). Before rebuilding just swap headers only to see if that is enough..
A few more words: a library can easily detect headers included in wrong order, make a ticket to lis developers for that.

c++ And lua-how to begin?

im trying out other languages.
Got VB2013 and LuaForWindows 5.1 What is the most basic file structure to run a .lua file in my program??
I have currently done http://www.youtube.com/watch?v=w51pftzS1_8 includes part, made a .h file that looks like this
#ifndef __LUA_INC_H__
#define __LUA_INC_H__
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
#endif // __LUA_INC_H__
and a Run.cpp like this
#include <iostream>
#include <conio.h>
#include <iostream>
#include "LuaInc.h"
using namespace std;
int main()
{
int iErr = 0;
lua_State *lua = lua_open(); // Open Lua
luaopen_io(lua); // Load io library
if ((iErr = luaL_loadfile(lua, "test.lua")) == 0)
{
// Call main...
if ((iErr = lua_pcall(lua, 0, LUA_MULTRET, 0)) == 0)
{
// Push the function name onto the stack
lua_pushstring(lua, "helloWorld");
// Function is located in the Global Table
lua_gettable(lua, LUA_GLOBALSINDEX);
lua_pcall(lua, 0, 0, 0);
}
}
lua_close(lua);
_getch();
return 0;
}
the test.lua file is in the vb213 projects dir/MYPROJECT/MYPROJECT
and looks like this
function helloWorld ()
io.write ("hello World")
end
From VS2013 and Lua for Windows, you are going to have some pain related to getting the right C Runtime Library version in play. Lua for Windows was compiled against an older version of the CRT, that came with VS2005. It may or may not be possible to get VS2013 to link against that older version. Mixing CRT versions is a recipe for much confusion.
The easiest way out is to get a version of the Lua core built for your version of Visual Studio. There are two ways to do that.
Download a version from Lua Binaries. There are pre-built versions of Lua available from the "official" Lua Binaries distribution. It can be had for 32-bit and 64-bit builds, for Windows, and for other platforms.
Build Lua yourself as part of your solution. Building your own Lua51.dll is straightforward, the default configuration is sensible. It mostly amounts to including almost all the .c files in a DLL project. Note that lua.c is not part of the DLL, that is the source to lua.exe. Similarly, luac.c is not part of the DLL, it is the source to luac.exe which does require some care to build yourself; but you aren't likely to need it.
Either way, you need to pay attention to some details.
The Lua API is a C API, not C++. So if you insist on making your application be a C++ application, you should wrap the inclusion of the Lua header files inside of an exern "C" block:
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
In principle, you can build the Lua core as C++ instead. The core is written in a clean C flavor that is also a subset of C++, and is tested when compiled as C++. However, if you go that route then you must build any binary modules yourself against your C++ linkage, and that way can lie madness if you depend on many community supplied modules.
I believe that all of the builds available at Lua Binaries are compiled as C, not C++, so the extern "C" declaration will be required with those.
Well, for starters, I would recommend scratching c++ and switching to lua as your primary programming language. It is quite messy to call lua functions from with c++, at least that is my experience. Once you understand enough of Lua, THEN call c/c++ functions from Lua via dll or other file/system formats. I was torn between learning python or c/c++ and I chose python. From there I learned about Lua and loved it more than python. Again this is just my experience and suggestion. Now to answer your question, why run Lua in C++? Lua is based on the C API, so I would just use raw C or just stick with C++. I say this only because Lua is technically no different than a watered down version of C. However, Lua is not "watered down" any more, as it can be used as a fully operational standalone programming language.

Visual studio for c++?

I have installed visual studio express 2010 c++. However when trying to follow a beginners book and make the hello world program, visual studio opens up a project with this in it:
#include "stdafx.h"
using namespace System;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
return 0;
}
The thing is, this looks nothing like the c++ in the book i have just started reading (C++ Without Fear). In fact, if i type 'cout',even after entering using namespace std, cout is not found.
This is the books example:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main() {
cout << "Never fear, C++ is here!";
return 0;
}
Am i missing something here?
Create Windows (or Win32, don't remember exactly) C++ Console Application project, don't select C++/CLI project type.
C++/CLI is very special language, which is used only for interoperability between managed and unmanaged code. It is better to forget about C+++/CLI at this stage...
As Alex said start with C++ Win32 console project, but select empty project so that the IDE don't fill things out automatically! Go to Source Files in your Solution Explorer, right click on it and add new item and choose C++ file (call it main.cpp for example).
At that point you should be ready to go.
Try this sample code that I prepared for you...
#include <iostream>
using namespace std;
int main(char *argv[], int argc) {
cout << "Hello World!" << endl;
return 0;
}
It should print out Hello World! in the console.
You want to start with a Visual C++ Win32 Console Application.
If you want to create your own files completely (i.e. no stub file with a main defined), you can check "Empty Project" in the options of the Win32 Application Wizard.
This is not C++. This is so called "managed" C++. Basically, a totally different language by Microsoft that is compatible with their .NET platform. For example, you can mix C# code and managed C++ code in single binary. This technology is very Microsoft specific and is not portable to any other compiler/OS (even Mono, which is C# for Linux, doesn't have it). See Managed Extensions for C++ for more details.