Using x86 DLL in an unreal engine Win64 C++ class - c++

I have written code in a .NET Framework C++ file. The code uses the Articares DLL to connect with the device through an IP address. Where instance is simply creating an instance of the classes I want to use from the DLL. instance.EstablishConnection() is the function i want to call from the class.
The below code works perfectly in x86.
#include "pch.h"
#include "windows.h"
#include <iostream>
#include <chrono>
#include <thread>
#include<cstring>
#include <vector>
#include "Complex.h"
using namespace System;
using namespace Articares;
using namespace NLog;
int main(array<System::String^>^ args)
{
//Creating instances of the classes ----------------------------------------------------
Articares::Core::ArticaresComm instance;
Articares::Core::HMANData instance2;
Articares::Core::TargetParams instance3;
//Connecting to HMan -------------------------------------------------------------------
instance.EstablishConnection("192.168.102.1", 3000);
std::cout << "Connection with HMan established\n";
return 0;
}
However, i want to be able to use the DLLs in the unreal engine C++ file to replicate this code i have written.
But the error name must be a namespace name appears when i try to declare the DLLs in the unreal engine C++ file.
Could anybody tell me how to fix this problem to be able to use my x86 DLLs in the unreal engine C++ code?
Thanks.

Are you using C++ DLL or . NET DLL?
For C++ DLL, you need to add dependencies. Walkthrough: Create and use your own Dynamic Link Library (C++)
For .NET DLL, You are calling managed code in unmanaged C++.
Use Exposing .NET components to COM
For your reference: using c# dll in project c++

Related

How wrap a C++ class into another C++ class to hide <future> and be able to compile with /clr

I intend to use a c++ lib from a dotnet core application. I am trying to create a Wrapper around this c++ lib.
The class I try to wrap is Client ( see : https://github.com/theomessin/jetbridge/blob/develop/Client/Client.hh ) :
#pragma once
#include <Windows.h>
#include <future>
#include <map>
#include "../Protocol/Protocol.hh"
namespace jetbridge {
class Client {
private:
void* simconnect = 0;
std::map<int, std::promise<Packet*>*> requests;
public:
Client(void* simconnect);
void handle_received_client_data_event(void* event);
Packet* request(char data[], int timeout = 1000);
};
} // namespace jetbridge
As you can see, it includes future
I created a C++ CLR project, added the native c++ Client lib and created simple ClientWrapper.h / ClientWrapper.cpp files :
ClientWrapper.h
#pragma once
#include "../Client/Client.hh"
using namespace System;
namespace ClrClient {
public
ref class ClientWrapper {};
}
ClientWrapper.cpp
#include "pch.h"
#include "ClientWrapper.h"
I am not able to compile this code, because <future> is not supported when compiling with /clr or /clr:pure
I did my research and found several discussions where it was suggested to first wrap the c++ class into another c++ class that does not expose the future.
Client.hh > ClientNativeWrapper.h > ClientClrWrapper.h
I understand this suggestion but I don't understand how to proceed to "not expose" this #include <future> by including Client.hh in another c++ class, it will still be visible when compiling the c++/cli wrapper class in the end.
As you state it, /clr is stuck at C++17 with no current plan to move forward. <future> is C++20 (see this link)
There are two approaches that I can suggest, but one solution. A static or dynamic library used by your wrapper.
The problem is you need to hide the C++20 to the compiler compiling with /clr.
Thus PIMPL idiom + library is my suggestion.
The PIMPL part would be used in libA (a static library is probably the best option) but all of the <future> parts internal in source files.
Then a C++17 wrapper that compiles with /clr and links in the libA that contains the C++20 code. No parts of the or C++20 may then leak out of your library through header files included by the wrapper in C++17.

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 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 ..);

gSOAP segmentation fault

Recently I have got a small C++ SOAP client project (ubuntu linux) to build using gSOAP. I am using 2 web service classes which I believe I have created correctly according to the documentation. However, I am having problems with my code
#include "envH.h"
#include "betfairBFGlobalServiceProxy.h"
#include "betfairBFExchangeServiceProxy.h"
//#include "betfair.nsmap"
#include <iostream>
#include <string>
// Following included to prevent ligsoap++ complaining of undefined references
// to 'namespaces'
SOAP_NMAC struct Namespace namespaces[] = {};
using namespace std;
using namespace betfair;
int main()
{
BFGlobalServiceProxy bf;
BFExchangeServiceProxy betfair2; /* CRASH!! */
return 0;
}
I want to make API calls to both web services but my program crashes at the point where the second service proxy object is created. I have used wsdl2h -gbetfair option and soapcpp2 -i -n options and created a new env.h file as requested in the documentation when creating multiple client services using C++ namespaces to avoid linker errors when linking to libgsoap++ . Can anyone see what I am doing wrong???
I cannot reproduce the problem, so I'm not sure about my guess. The array namespaces is defined inside .nsmap files. Your code has a comment in the #include of .nsmap file and maybe gSOAP don't like if this array is empty. You really have to keep the #include "betfair.nsmap" commented out?

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.