Here a little test code illustrating the problem:
Compiling configurations:
Common Language Runtime Support: /clr
C++ Language
Error message:
Error 4 error C2065: 'DataContractSerializer' : undeclared identifier C:...\SerializationTest.cpp 21 1 SerializationTest
The code:
// SerializationTest.cpp : main project file.
#include "stdafx.h"
using namespace System::Collections::Generic;
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Runtime::Serialization;
int main(array<System::String ^> ^args)
{
Console::WriteLine(L"Hello World");
Dictionary<System::String^, System::Double>^ teste = gcnew Dictionary<System::String^, System::Double>();
teste->Add("Teste1",2);
teste->Add("Teste2",4);
DataContractSerializer^ serializer = gcnew DataContractSerializer(teste->GetType());
StringWriter^ writer = gcnew StringWriter();
XmlTextWriter^ stm = gcnew XmlTextWriter(writer);
serializer->WriteObject(stm, teste);
Console::WriteLine(writer->ToString());
return 0;
}
It sounds simply like you're missing a reference to System.Runtime.Serialization.dll (which is required in addition to the using directive):
#using <System.Runtime.Serialization.dll>
Related
I am using EASendEmail with Visual C++. After I compile the code and run it, I get an error which reads: "Debug Error! /Program:[file path] /abort() has been called/ (press retry to debug the application)"
The code is from this website:https://www.emailarchitect.net/easendmail/kb/vc.aspx?cat=0
The error is occurring in the lins:
oSmtp->LicenseCode = _T("TryIt");
'
_tprintf(_T("Start to send email ...\r\n"));
I have tried debugging mode in visual studio and it lead me to an unhandled expression in an attached library and an error at a specific memory location.
c++
#include "pch.h"
#include "easendmailobj.tlh"
#include <tchar.h>
#include <iostream>
using namespace std;
using namespace EASendMailObjLib;
int _tmain(int argc, _TCHAR* argv[])
{
::CoInitialize(NULL);
IMailPtr oSmtp = NULL;
oSmtp.CreateInstance("EASendMailObj.Mail");//<- one cause of the error
oSmtp->LicenseCode = _T("TryIt");
//...
_tprintf(_T("Start to send email ...\r\n"));//<- one cause of the error
the error:https://imgur.com/a/M3C72Kc
When I tried to compile simple GTA San Andreas ASI (dll) project using SDK, I saw error
C2065 'D3DFORMAT': undeclared identifier.
Line: RwBool _rwD3D9CheckValidCameraTextureFormat(D3DFORMAT format); // 0x4CBE20
Interesting thing is that, a few months ago, before reset to factory settings (Windows 10) all was ok.
I tried on Visual Studio 2015/2017/2019.
My code is so simple, because all is in SDK. (https://github.com/DK22Pac/plugin-sdk)
#include "plugin.h"
#include "common.h"
#include "CTimer.h"
#include "CStreaming.h"
using namespace plugin;
class PlayerWeapon {
public:
PlayerWeapon() {
static int keyPressTime = 0;
Events::gameProcessEvent += [] {
CPed *playa = FindPlayerPed();
if (playa && KeyPressed(VK_TAB) && CTimer::m_snTimeInMilliseconds - keyPressTime > 500) {
keyPressTime = CTimer::m_snTimeInMilliseconds;
CStreaming::RequestModel(MODEL_M4, 2);
CStreaming::LoadAllRequestedModels(false);
playa->GiveWeapon(WEAPON_M4, 10, true);
playa->SetCurrentWeapon(WEAPON_M4);
CStreaming::SetModelIsDeletable(MODEL_M4);
}
};
}
} playerWeapon;
I am trying to use C++/WinRT to write something interesting. Having very little experience in Windows programming and no experience in C++/CX, I started out by trying sample program (OCR).
The sample program is about optical character recognition, I modified it to be face detector (console-based). It worked very well.
I want to convert getting file from command-line to file dialog, so I ported the following snippet from C#:
FileOpenPicker picker = FileOpenPicker();
picker.ViewMode(PickerViewMode::Thumbnail);
picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".jpeg");
picker.FileTypeFilter().Append(L".png");
StorageFile file = co_await picker.PickSingleFileAsync();
No compile error, but when I run the program, I get this error message:
hresult_error: (0x80070578) Invalid window handle.
I think the error occurs because it is a console-based program (wmain), not wWinMain.
I tried to find a solution online, but they are all about UWP. Please provide a solution that does not involve UWP and must be able to compile from cl.exe directly.
Note:
According to UWP APIs callable from a classic desktop app, if an API has DualApiPartitionAttribute attribute, it will work in classic desktop app, otherwise it won't. For example, Geolocator has this attribute so it will work.
However, even though FaceDetector does not have this attribute it still works proven in my toy program.
Anything from Windows::UI definitely requires UWP (although there is https://aka.ms/windowsui/inwin32). FileOpenPicker does not have this attribute but it is not under Windows::UI, so there might be a chance it can be workaround.
Complete code:
#pragma comment(lib, "windowsapp")
#include <winrt/Windows.Storage.Pickers.h>
#include <winrt/Windows.Storage.Streams.h>
#include <winrt/Windows.Graphics.Imaging.h>
#include <winrt/Windows.Media.FaceAnalysis.h>
using namespace winrt;
using namespace std::chrono;
using namespace Windows::Foundation;
using namespace Windows::Storage;
using namespace Windows::Storage::Pickers;
using namespace Windows::Storage::Streams;
using namespace Windows::Graphics::Imaging;
using namespace Windows::Media::FaceAnalysis;
using Windows::Foundation::Collections::IVector;
IAsyncOperation<int> AsyncSample() {
FileOpenPicker picker = FileOpenPicker();
picker.ViewMode(PickerViewMode::Thumbnail);
picker.SuggestedStartLocation(PickerLocationId::PicturesLibrary);
picker.FileTypeFilter().Append(L".jpg");
picker.FileTypeFilter().Append(L".jpeg");
picker.FileTypeFilter().Append(L".png");
StorageFile file = co_await picker.PickSingleFileAsync();
//StorageFile file = co_await StorageFile::GetFileFromPathAsync(
// L"C:\\Users\\user\\Pictures\\20170318_202325.jpg");
IRandomAccessStream stream = co_await file.OpenAsync(FileAccessMode::Read);
BitmapDecoder decoder = co_await BitmapDecoder::CreateAsync(stream);
SoftwareBitmap bitmap = co_await decoder.GetSoftwareBitmapAsync();
FaceDetector detector = co_await FaceDetector::CreateAsync();
SoftwareBitmap converted =
SoftwareBitmap::Convert(bitmap, BitmapPixelFormat::Nv12);
IVector<DetectedFace> result = co_await detector.DetectFacesAsync(converted);
printf("Detection done\n");
for (auto& face : result) {
BitmapBounds box = face.FaceBox();
printf("[%u %u %u %u]\n", box.X, box.Y, box.Width, box.Height);
}
printf("Printing done\n");
return 0;
}
int wmain() {
init_apartment();
try {
int res = AsyncSample().get();
printf("%d\n", res);
} catch (hresult_error& e) {
printf("hresult_error: (0x%8X) %ls\n", e.code(), e.message().c_str());
}
return 0;
}
So this was the modification neeeded:
IAsyncOperation<int> AsyncSample() {
HWND hwnd = GetConsoleWindow(); // #include <windows.h>
FileOpenPicker picker = FileOpenPicker();
picker.as<IInitializeWithWindow>()->Initialize(hwnd);
I'm building a GTK+ VS2010 project configuration on (VS2013) and I'm trying to add a browse folder functionality (I want to use Native Windows way). I'm using the following code:
#include <windows.h>
#include <ShlObj.h>
DWORD WINAPI BrowseFolder(void *ptr)
{
char path[MAX_PATH];
cchar * path_param = (cchar*)ptr;
BROWSEINFO bi = { 0 };
bi.lpszTitle = ("Select Folder");
bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_NEWDIALOGSTYLE;
bi.lpfn = BrowseCallbackProc;//callback function defined..
bi.lParam = (LPARAM)path_param;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);
if (pidl != 0)
{
//get the name of the folder and put it in path
SHGetPathFromIDList(pidl, path);
//free memory used
IMalloc * imalloc = 0;
if (SUCCEEDED(SHGetMalloc(&imalloc)))
{
imalloc->Free(pidl);
imalloc->Release();
}
strcpy(g_sDefaultDir, path);
return 0;
}
}
but when compiling I'm getting this error message
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Include\shobjidl.h(17624): error C2061: syntax error : identifier 'IXMLDOMDocument'
This is my configuration:
I'm running VS2010 solution on VS2013
Use Standard Windows Libraries
Not Using ATL
and Use Multi-Byte character set
Any ideas?
I found 2 ways to answer solve my problem.
I had msxml and ISoftDistExt previously defined to solve ambiguity problem, so I had to #undef them like the following:
#undef __msxml_h__
#undef __ISoftDistExt_INTERFACE_DEFINED__
#include <MsXml.h>
#include <ShlObj.h>
Also, you can use the #import statement like the follownig
#undef __msxml_h__
#undef __ISoftDistExt_INTERFACE_DEFINED__
#import <msxml6.dll>
using namespace MSXML2;
#include <ShlObj.h>
Hope this will help someone later.
I have loaded an JPG image to the Image^ object but now I would like to "go back" to using standard C++ in my program so I would like to somehow convert Image^ object into any BMP class Object(I don't know which c++ class is good).
I would like to edit color of particular pixels in that bitmap.
Please help me do it.
// a.cpp : main project file.
#include "stdafx.h"
#include "Form1.h"
#using <mscorlib.dll> //requires CLI
using namespace System;
using namespace System::IO;
using namespace System::Windows::Media::Imaging;
using namespace System::Windows::Media;
using namespace System::Windows::Controls;
using namespace a;
[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
// Enabling Windows XP visual effects before any controls are created
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
// Create the main window and run it
Application::Run(gcnew Form1());
// Open a Stream and decode a JPEG image
Stream^ imageStreamSource = gcnew FileStream("C:/heart.jpg", FileMode::Open, FileAccess::Read, FileShare::Read);
JpegBitmapDecoder^ decoder = gcnew JpegBitmapDecoder(imageStreamSource, BitmapCreateOptions::PreservePixelFormat, BitmapCacheOption::Default);
BitmapSource^ bitmapSource = decoder->Frames[0];//< --mamy bitmape
// Draw the Image
System::Windows::Controls::Image^ myImage = gcnew System::Windows::Controls::Image(); //<--- this image in the Form1 -------
myImage->Source = bitmapSource;
myImage->Stretch = Stretch::None;
//CONVERT MYIMAGE INTO C++ BMP
return 0;
}
try the Image.save method. There you can save to a file or stream and you can specify the file format.