I need to make a new object of a cli class in plain C++ code.
I am new to cli, please help
my cli class:
using namespace System;
using namespace System::Windows::Forms;
using namespace CrystalDecisions::Shared;
using namespace CrystalDecisions::CrystalReports::Engine;
using namespace CrystalDecisions::Windows::Forms;
namespace CrystalRapport {
// This is the main form that will hold the viewer control.
ref class ViewForm : public System::Windows::Forms::Form
{
private:
//Declare the Viewer Control, Report Document, and other
//objects needed to set the connection information.
public:
ViewForm()..
void InitForm()..
//This function initializes the form and adds the viewer to the form.
void ViewForm_Load(System::Object^ sender, System::EventArgs^ e)..
};
}
You need to use gcnew if you want to create .NET object in CLI C++.
ref class Student
{
...
};
...
Student^ student = gcnew Student();
student->SelectSubject("Math", 97);
Ref:
http://www.codeproject.com/Articles/17787/C-CLI-in-Action-Instantiating-CLI-classes
I found an example. Use .NET Assemblies in Native C++ Applications
http://msdn.microsoft.com/en-us/vstudio/bb892742.aspx
If you create a C# Crystal Reports project you can use this example.
(with some changes)
Related
I'm trying to write a c++/cli wrapper for IO Industries Core2 DVR, which will then be used by LabView. The company provided a SDK with with all the headers (written in c++) and boost library. I've managed to build a wrapper that builds and LabView is able to see the function through the .net pallet.
// ManagedProject.h
#pragma once
#include "core_api_helper.h"
#include "core_api.h"
using namespace System;
using namespace CoreApi;
namespace ManagedProject {
//Setup class
public ref class Setup
{
private:
public:
unsigned int initializeTest();
};
}
// This is the DLL Wrapper.
#include "stdafx.h"
#include "ManagedProject.h"
#include "core_api_helper.h"
#include "core_api.h"
#include "resource.h"
using namespace CoreApi;
using namespace Common;
using namespace ManagedProject;
//Global handles
//A handle to the Core Api
InstanceHandle g_hApi;
//A handle to the Core Api Device Collection
DeviceCollectionHandle g_hCoreDeviceCollection;
unsigned int Setup::initializeTest()
{
try
{
//Initialize the Core API (must be called before any other Core API functions)
//Returns a handle to the Core Api
g_hApi = Instance::initialize();
// get a collection of Core devices
g_hCoreDeviceCollection = g_hApi->deviceCollection();
unsigned int deviceCount = g_hCoreDeviceCollection->deviceCount();
return deviceCount;
}
catch (GeneralException& e)
{
e.what();
return 3;
}
}
However when I run LabView through Visual studio 2015 in debug mode I run into the problem below, and what is returned to LabView is the 3 from the catch block.
First break in debug mode (NULL ptr)
NOTE: InstanceHandle is a shared_ptr
As can be seen the variable is a NULL pointer, the same thing happens for the g_hCoreDeviceCollectoin as well. I think I need to Instantiate it with the new command but am a little unsure as InstanceHandle is a shared_ptr.
Any help would be much appreciated
The C++/CLI has great feature called mixed mode. You can uses both managed and native data types in the same code (in the same C++/CLI class). Try to use object from that SDK written in C++ directly in your wrapper.
I am trying to create a Windows Runtime(WinRT) Component written in C++(c++-cx) which can be called from my UWP C# app. I followed this MSDN Tutorial
It all went fine. I am able to build & run the code/sample specified in the above link on both Windows-10 Desktop & Mobile
I then tried to create a even simpler WinRT Component. I followed the same steps as above tutorial. Created a Windows Runtime Component project (File->New->Project->Visual C++ ->Windows->Universal->Windows Runtime Component(Universal Windows))
Now my class has only one function, getNum which basically returns a uint16
This is my Class header file
#pragma once
#include <collection.h>
namespace WRT_sample
{
public ref class Class1 sealed
{
public:
Class1();
uint16 getNum();
};
}
This is the corresponding cpp file:
#include "Class1.h"
using namespace WRT_sample;
using namespace Platform;
Class1::Class1()
{
}
uint16 Class1::getNum()
{
return 100;
}
I am using the same class names(Class1.h & Class1.cpp) which VS-2015 provides when I create a new WinRT project.
This is how I make a call to WinRT class from my C# code:
namespace CS_WRT_sample
{
public sealed partial class MainPage : Page
{
public MainPage()
{
this.InitializeComponent();
initWRT();
}
void initWRT()
{
try
{
var nativeObj = new Class1();
var num = nativeObj.getNum();
this.Result2.Text = "Num from Cpp: " + num.ToString();
Debug.WriteLine("Num from Cpp: " + num);
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
this.Result3.Text = ex.Message;
}
}
}
}
I am able to build & run my code on Desktop(x86/x64 Local Machine), but when I try to run on Mobile(x86 Emulator or ARM device), it fails with the following error:
Exception thrown: 'System.IO.FileNotFoundException' in CS_WRT_sample.exe
The specified module could not be found. (Exception from HRESULT: 0x8007007E)**
This is the stack trace which I see:
System.StubHelpers.StubHelpers.GetWinRTFactoryObject(IntPtr pCPCMD)
at WRT_sample.Class1..ctor()
at CS_WRT_sample.MainPage.initWRT()
My VS-2015 details:
MS Visual Studio Professional 2015
Version 14.0.25123.00 Update 2
MS .NET Framework
Version 4.6.01038
I am not sure, why I am hitting the issue, as I followed the same steps mentioned in the MSDN tutorial.
Any help will be highly appreciated.
You will run into this if you use a direct reference to a C++ winmd, instead of a P2P reference. This is easy to check in your .csproj:
For example, your .csproj should not have this line:
..\Debug\Abc_def\Abc_Def.winmd
Instead, consider using something like:
{1ee77824-eaa8-40f1-9b56-6f8ffa44e727}
Abc_Def
My problem is as follows:
I got a Microsoft Visual C++ 2008 project and the order to organize all classes in it in nested namespaces. Now I get System::Resources::MissingManifestResourceException on runtime, because the custom System::Windows::Forms has a assembly ressource and with the new namespace, the old Resx-file does not fit anymore. My problem is now, that I have no idea how to reconnect it to the Form-class.
The old code of the Form was like:
namespace OldNamespace{
ref class MainForm : public System::Windows::Forms::Form{
...
};
}
The new code looks like:
namespace NewNamespace{
namespace GUI{
namespace MainWindow{
ref class MainForm : public System::Windows::Forms::Form{
...
};
}
}
}
I tried to adjust the filename of the created resource-file from the resx-file from
$(IntDir)\$(RootNamespace).$(InputName).resources
to
$(IntDir)\NewNamespace.GUI.MainWindow.MainForm.resources
But I am still getting System::Resources::MissingManifestResourceException on runtime.
What is my mistake?
I have some test class
using System;
using Microsoft.VisualStudio.TestPlatform.UnitTestFramework;
using Praktyka.Models;
namespace PraktykaTest
{
[TestClass]
public class PictureManagerTest
{
[TestMethod]
public void LoadImagesTest()
{
var pic = new PictureManager();
pic.LoadImages(new List<String>
{
"1.jpg",
"2.jpg"
});
// Assert.AreEqual(#"dataImages\1.jpg",pic.Current().UriSource);
Assert.AreEqual("test","test");
}
}
}
I have compile error
The type or namespace name 'List' could not be found (are you missing a using directive or an assembly reference?)
and
Cannot initialize object of type 'List<string>' with a collection initializer
How to add reference for correct working with Lists ?
First, add a reference to the System.Collections.Generic namespace to get the generic List<> class. Then try recompiling.
I put my NHibernate data access class in WCF service to can consume it by Silverlight project, but I have error and want to test my queries.
It is possible to test this queries in service class using NUnit ? Earlier I test normally this class but how do it when it is in service class ??
It's my WCF service class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using DataTransfer;
using NHibernate;
using NHibernate.Cfg;
using System.Diagnostics;
namespace WcfService1
{
public class Service1 : IService1
{
private ISession _session;
public Service1()
{
try
{
_session = (new Configuration()).Configure().BuildSessionFactory().OpenSession();
}
catch (Exception e)
{
Debug.Write(e);
throw;
}
}
public IList<Dziecko> GetChildByFirstname(string _firstname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByLastname(string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
public IList<Dziecko> GetChildByFirstnameAndLastname(string _firstname, string _lastname)
{
return _session.CreateCriteria(typeof(Dziecko))
.Add(NHibernate.Criterion.Expression.Eq("Imie", _firstname)).Add(NHibernate.Criterion.Expression.Eq("Nazwisko", _lastname)).List<Dziecko>();
}
}
}
If you want to test the queries themselves, I'd recommend putting them into a separate assembly (maybe using the repository pattern) and calling the methods in this assembly from your service. This will make it easier to test the queries themselves, and also allow you to mock the repositories when testing the service.