Is there a possibility to use xmlwriter (xmlserialization) without managed code (cli)?
using namespace System::Xml;
using namespace System::Xml::Schema;
using namespace System::Xml::Serialization;
My XML serialization managed code:
void TXML_Interface::LoadXML( String^ filename )
{
XmlSerializer^ serializer = gcnew XmlSerializer( TTEST::typeid );
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
XmlReader^ reader = gcnew XmlTextReader( fs );
m_test = dynamic_cast<TTEST^>(serializer->Deserialize( reader ));
}
Yes and no.
Yes it is possible to do XML maniuplation (including serialisation) without managed code - I'd normally do this using MSXML however there are various ways to perform xml serialisation in C++ (I'm not really a C++ person but Google is almost certainly the first place to look).
However this is using a different mechanism from the ones contained in the System.Xml.Serialization namespace. Unfortunately for you the Xml serialisation in .Net is all implemented in managed code, and so if you want to use it you will need to call into managed code (e.g. by using the /clr compiler option or COM interop).
Maybe the boost::serialization libary is what you are looking for.
Serialization capabilities are rather limited in C++ so boost::serialization is more like a framework that enable you to make your own classes serializable.
Related
For a my personal C++ project, I'd like been able to encrypt plain text data using private key. In my project I make an extensive usage of Poco C++ library, and I'd like handle such feature with it.
Currently I am successfully handling a private key file in order to create Poco::Crypto::RSAKey.
std::filesystem::path keyFile = std::filesystem::path("MyFile");
Poco::SharedPtr<Poco::Crypto::RSAKey> key(new Poco::Crypto::RSAKey("", keyFile.string()));
Poco::Crypto::CipherFactory& factory = Poco::Crypto::CipherFactory::defaultFactory();
Poco::Crypto::Cipher* pRSACipher = factory.createCipher(*key.get());
std::string plainText("MyTextToEncrypt");
std::string encrypted = pRSACipher->encryptString(plainText, Poco::Crypto::Cipher::ENC_BASE64_NO_LF);
Having a look at official Poco documentation I found out that both Poco::Crypto::RSAKey and Poco::Crypto::ECKey are deprecated. Looking for an alternative to such deprecated classes both in Poco documentation and all around in web, I could not understand why such classes are declared as deprecated. Moreover I could not find which classes shouold replace them.
In same time, reading Poco::Crypto::CipherFactory documentation, it is not depracted using methods which get Poco::Crypto::RSAKey for method Poco::Crypto::CipherFactory::createCipher.
Please, can somebody tell me if using Poco::Crypto::RSAKey is still advisable, or another class should be used? And which one?
Thanks in advance!
I'd like to understand how to transmit the contents of a C++ class between processes or across a network.
I'm reading the Google Protobuf tutorial:
https://developers.google.com/protocol-buffers/docs/cpptutorial
and it seems you must create an abstracted, non-C++ interface to represent your class:
syntax = "proto2";
package tutorial;
message Person {
optional string name = 1;
optional int32 id = 2;
optional string email = 3;
enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}
}
However, I'd prefer to specify my class via C++ code (rather than the abstraction) and just add something like serialize() and deserialize() methods.
Is this possible with Google Protobuf? Or is this how Protobuf works and I'd need to use a different serialization technique?
UPDATE
The reason for this is I don't want to have to maintain two interfaces. I'd prefer to have one C++ class, update it and not have to worry about a second .proto interface/definition. Code maintainability.
That's how Protobuf works. You have to use something else if you want to serialize your manually-written C++ classes. However, I'm not sure you really want that, because you then will have to either restrict yourself to very simple fields with no invariants (just like in Protobuf) or write custom (de)serialization logic yourself.
You could make a simple protocol buffer to hold binary information, but it sort of breaks the point of using Protocol buffers.
You can sort of cheat the system by using SerializeToString() and ParseFromString() to simply serialize binary information into a string.
There is also SerializeToOstream() and ParseFromIstream().
The real value of protocol buffers is being able to use messages across programs, systems and languages while using a single definition. If you aren't making messages using the protocol they've defined; this is more work than simply using native C++ capabilities.
How do I implement a function to load a dll(aka framework) on Mac OS using C++?
void LoadFramework(const char* frameworkPath)
{
//frameworkPath is the absolute path of the framework
}
Edit:
When I google searched for this problem, I mostly ended up with dlopen solution to load the framework. What I am instead looking for is to use CFBundleCreate to load the framework. It seems to me that there are a bunch of methods needed to be called to construct an URL from const char * path. I found the needed code in pieces, and could not write one comprehensive solution.
It typically is just a few lines of straightforward code to open a framework in Mac, something along the lines of :
bundleURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault,
CFSTR("/System/Library/Frameworks/<your_framework_name.framework>"),
kCFURLPOSIXPathStyle, true);
bundle = CFBundleCreate(kCFAllocatorDefault, bundleURL);
assert(bundle != NULL);
and pretty much everything in that snippet is well documented. I would suggest adding more detail in the question, as to the specifics of what exactly is not working for you.
Why not do this?
using DLL_Namespace;
This should give you access to the DLL.
I'm working in a native C++ enviroment on WP8.1.
Let's say I want to call a functions like
Microsoft.Phone.Info::DeviceExtendedProperties.GetValue( "DeviceUniqueId" );
The problem is no matter how I try, it didn't pass compile.
I know "Microsoft.Phone.Info" is a name space,
in C# ppl wrote:
using Microsoft.Phone.Info;
but in C++, I tried
using namespace Microsoft.Phone.Info;
void func()
{
DeviceExtendedProperties.GetValue("DeviceUniqueId");
}
didn't pass compile. or
void func()
{
Microsoft.Phone.Info::DeviceExtendedProperties.GetValue("DeviceUniqueId");
}
didn't pass compile.
It keeps telling me something like this
'Microsoft' : illegal use of namespace identifier in expression
so how do I use this namespace properly?
Thank you guys for the reading and answering. :-)
I can't find a C++ example for retriving the device ID. :-P
Although many of the properties from DeviceInformation can be found on EasClientDeviceInformation, the device ID is not one of them. Instead, you should look at using the ASHWID which will get you an app-specific hardware ID.
In native WP runtime, using a namespace follows this syntax
using namespace Windows::Phone::Info;
But you will find out that the namespace does not exist in runtime. Only in WP Silverlight.
So you can look at this MSDN webpage that tells you where they moved all the functionality from 8.0 SL to 8.1 runtime.
Windows Phone Silverlight to Windows Runtime namespace and class mappings
Then if you search for "Windows.Phone.Info" it will lead you to the new functions you should call.
Here is the ID : EasClientDeviceInformation class
I guess I got it:
EasClientDeviceInformation^ deviceInfo = ref new EasClientDeviceInformation();
deviceInfo->Id; //<- is what I want
I'm trying to launch an image using WinRT API WIndows::System::Launcher::LaunchFileAsync().
Code snippet is as follows:
RoInitialize(RO_INIT_MULTITHREADED);
String^ imagePath = ref new String(L"C:\\Users\\GoodMan\\Pictures\\wood.png");
auto file = Storage::StorageFile::GetFileFromPathAsync(imagePath);
Windows::System::Launcher::LaunchFileAsync(file);
I'm getting this error from the LaunchFileAsync() API:
error C2665: 'Windows::System::Launcher::LaunchFileAsync' : none of
the 2 overloads could convert all the argument types
Can I please get help how to solve this. I'm very new to WinRT C++ coding .
The method GetFileFromPathAsync does not return a StorageFile, but it returns IAsyncOperation<StorageFile>^. What you have to do is convert the latter to the former, as follows:
using namespace concurrency;
String^ imagePath = ref new String(L"C:\\Users\\GoodMan\\Pictures\\wood.png");
auto task = create_task(Windows::Storage::StorageFile::GetFileFromPathAsync(imagePath));
task.then([this](Windows::Storage::StorageFile^ file)
{
Windows::System::Launcher::LaunchFileAsync(file);
});
Generally all Windows Store app framework methods that end in Async will return either an IAsyncOperation, or a task. These methods are what are known as asynchronous methods, and require some special handling. See this article for more info: Asynchronous programming in C++ .
So now everything is great, correct? Well, not quite. There is another issue with your code. It is that when you run the code above, you will get an access-denied error. The reason is that Windows Store Apps are sandboxed, and you cannot generally access just any file on the filesystem.
You are in luck, though, because you are trying to access a file in your Pictures folder. The Pictures folder is a special folder that Windows Store apps have access to. You can get at it using the KnownFolders class:
using namespace concurrency;
Windows::Storage::StorageFolder^ pictures =
Windows::Storage::KnownFolders::PicturesLibrary;
auto task = create_task(pictures->GetFileAsync("wood.png"));
task.then([this](Windows::Storage::StorageFile^ file)
{
Windows::System::Launcher::LaunchFileAsync(file);
});
Note that in order to access the Pictures folder your application has to declare it in the project manifest. To do so, double click on the Package.appmanifest file in the project "tree" in Visual Studio, and select the Capabilities tab. Then under Capabilities, check Pictures Library.