inherit class in c++ clr - visual-studio

I am trying to build a simple project in Visual Studio 2017 using the template CLR/Empty CLR project .. I have added references to required namespaces, but when I try to inherit "IMessageFilter" class in my class I get below error, What should I do?
class fails to implement interface member function "System::Windows::Forms::IMessageFilter::PreFilterMessage" (declared in "c:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Windows.Forms.dll"
#pragma once
using namespace System;
using namespace System::Windows::Forms;
ref class myClass : public IMessageFilter // this is not accepted by compiler with error
{
public:
myClass();
};

As the error message suggests, you need to implement the PreFilterMessage message function that is required by the IMessageFilter interface:
virtual bool PreFilterMessage(Message% message)
{
// from MSDN: return true to filter the message and stop it from being dispatched; false to allow the message to continue to the next filter or control.
return true;
};

Related

How to Reference .Net Core Library in a .Net Core Console Application

I am following this code example
I am on Visual Studio Community 2019 for Mac. I created a .Net Core - Class Library project and compiled to create the assembly file P1-ProgramStructure.dll.
I created another solution with program2.cs code. Please see the code below.
I renamed the .dll to acme.dll and copied the file into its directory.
Class library - .Net Core Project
Program1.cs
using System;
namespace Acme.Collections
{
public class Stack
{
Entry top;
public void Push(object data)
{
top = new Entry(top, data);
}
public object Pop()
{
if (top == null)
{
throw new InvalidOperationException();
}
object result = top.data;
top = top.next;
return result;
}
class Entry
{
public Entry next;
public object data;
public Entry(Entry next, object data)
{
this.next = next;
this.data = data;
}
}
}
}
.Net Core Console App
Program2.cs
using System;
using Acme.Collections;
class Example
{
static void Main()
{
Stack s = new Stack();
s.Push(1);
s.Push(10);
s.Push(100);
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
Console.WriteLine(s.Pop());
}
}
When I run the project, I get the error:
$ dotnet run
Program.cs(15,7): error CS0246: The type or namespace name 'Acme' could not be found (are you missing a using directive or an assembly reference?) [/Users/csarami/VisStudioProjects/cSharp Projects/Project2-ProjectStructure/Project2-ProjectStructure/Project2-ProjectStructure.csproj]
The build failed. Please fix the build errors and run again.
Make sure both projects have the same target framework

Unity cannot build GRPC Project for UWP with IL2CPP Backend

Here, or here for a complete version, you can find a sample GRPC "Hello World" project for Unity. Only the first version, that is built for Unity and wrapped in a DLL is working perfectly fine in Unity IDE and on Standalone build. The Raw Grpc.Core files are referencing everything correctly in IDE but they have Marshaling problem.
Unfortunately, it cannot get build for UWP with IL2CPP backend. Unity builds the project and creates a .sln project. But Visual Studio always gives LNK2001 for GRPC properties on the final compilation.
Here are first error codes:
LNK2001 unresolved external _grpccsharp_init#0
LNK2001 unresolved external _grpccsharp_shutdonw#0
LNK2001 unresolved external _grpccsharp_version_string#0
...
Ok, thanks to #Sunius, I digged into it a little bit more. There are some points, I am going to add to the question:
There are two methods regarding referencing extern methods in GRPC C# package. They are named static and shared libs.
internal class DllImportsFromStaticLib
{
private const string ImportName = "__Internal";
[DllImport(ImportName)]
public static extern void grpcsharp_init();
[DllImport(ImportName)]
public static extern void grpcsharp_shutdown();
...
}
and
internal class DllImportsFromSharedLib
{
private const string ImportName = "grpc_csharp_ext";
[DllImport(ImportName)]
public static extern void grpcsharp_init();
[DllImport(ImportName)]
public static extern void grpcsharp_shutdown();
...
}
I tried to test it with the shared one, I got another linking error file which is a little bit different.
LNK2001 unresolved external _dlopen#8
LNK2001 unresolved external _dlsym#8
...
In two separate methods, extern methods are getting connected to the internal interface:
public NativeMethods(DllImportsFromStaticLib unusedInstance)
{
this.grpccsharp_init = DllImportsFromStaticLib.grpccsharp_init;
this.grpccsharp_shutdown = DllImportsFromStaticLib.grpccsharp_shutdonw;
...
}
and
public NativeMethods(DllImportsFromSharedLib unusedInstance)
{
this.grpccsharp_init = DllImportsFromSharedLib.grpccsharp_init;
this.grpccsharp_shutdown = DllImportsFromSharedLib.grpccsharp_shutdonw;
...
}
And which method will get called is defined here:
private static NativMethods LoadNativeMethodsUnity()
{
switch(PlatformApis.GetUnityRuntimePlatform())
{
case "IPhonePlayer":
return new NativeMethods(new NativeMethods.DllImportsFromStaticLib());
default:
return new NativeMethods(new NativeMethods.DllImportsFromSharedLib());
}
}
Some updates:
Thanks to #jsmouret, there is Stub.c file in his Grpc Github with fake methods, so Linker does not complain about Grpc_init methods anymore.
Next Error: dlopen, dlsym, dlerror:
First, I tried to use the same, Stub technique, but it did not help in this case, or maybe I did it wrong.
Thanks to #Sunius, I commented out all of "__Internal" dll import codes. So I am not getting any dlopen, dlsym, and dlerror errors.
Next Error: It happens from inside application, not the visual studio debugger. It tells me: "exception: to marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition."
exception: error loading the embedded resource "Grpc.Core.roots.pem"
and
exception: To marshal a managed method, please add an attribute named 'MonoPInvokeCallback' to the method definition.
After I googled it, I know my options, but the question it, for which method should I do that?!
Thanks to my colleague Alice, #Sunius and #jsmouret, at the end, grpc works on UWP on Unity Platform through this steps:
Download Grpc.Core folder from Google Grpc Github.
Download Grpc Unity plugin from their official site.
Copy the runtime folder to your Grpc.Core folder. Please remove Grpc.Core.dll that you get from Grpc Unity Plugin, since we are using their source code.
Grpc should be in a folder called, Plugins in Unity, otherwise it will not be recognized.
Include this file in your runtime folder.
Include the Stub also from the Unity Plugin Inspector for WSA.
Find runtime .dll for Windows and include them in WSA from Unity Plugin Inspector.
By now, you should be getting _dlopen error.
Search through your Unity Solution with an IDE for "__Internal". There are not so many places, but comment them out. Also some methods that are depended on "__Internal"s, like dlopen and dlsym.
By now, you are not getting anymore build error but you need to make Grpc work.
Search for something like "DefaultSslRootsOverride" and comment out like below:
internal static class DefaultSslRootsOverride
{
const string RootsPemResourceName = "Grpc.Core.roots.pem";
static object staticLock = new object();
/// <summary>
/// Overrides C core's default roots with roots.pem loaded as embedded resource.
/// </summary>
public static void Override(NativeMethods native)
{
lock (staticLock)
{
//var stream = typeof(DefaultSslRootsOverride).GetTypeInfo().Assembly.GetManifestResourceStream(RootsPemResourceName);
//if (stream == null)
//{
// throw new IOException(string.Format("Error loading the embedded resource \"{0}\"", RootsPemResourceName));
//}
//using (var streamReader = new StreamReader(stream))
//{
// var pemRootCerts = streamReader.ReadToEnd();
// native.grpcsharp_override_default_ssl_roots(pemRootCerts);
//}
}
}
}
Search for something like "static void HandWrite" and add an attribute like something in below:
[MonoPInvokeCallback(typeof(GprLogDelegate))]
private static void HandleWrite(IntPtr fileStringPtr, int line, ulong threadId, IntPtr severityStringPtr, IntPtr msgPtr)
{
try
{
var logger = GrpcEnvironment.Logger;
string severityString = Marshal.PtrToStringAnsi(severityStringPtr);
string message = string.Format("{0} {1}:{2}: {3}",
threadId,
Marshal.PtrToStringAnsi(fileStringPtr),
line,
Marshal.PtrToStringAnsi(msgPtr));
switch (severityString)
{
case "D":
logger.Debug(message);
break;
case "I":
logger.Info(message);
break;
case "E":
logger.Error(message);
break;
default:
// severity not recognized, default to error.
logger.Error(message);
break;
}
}
catch (Exception e)
{
Console.WriteLine("Caught exception in native callback " + e);
}
}
I guess, you are done. In case, it did not work for your UWP, let me know, maybe I can help. :)
It looks like your plugin uses "__Internal" P/Invoke to call those native functions:
https://github.com/grpc/grpc/blob/befc7220cadb963755de86763a04ab6f9dc14200/src/csharp/Grpc.Core/Internal/NativeMethods.Generated.cs#L542
However, the linker cannot locate those functions and thus fails. You should change that code to either specify the DLL file name where the functions are implemented, or drop the source files with definitions for those functions into your Unity project. Or, if that code path isn't actually invoked (since you said it works on the standalone player), #ifdef it out from UWP build.
You can find more information about "__Internal" P/Invoke here:
https://docs.unity3d.com/Manual/windowsstore-plugins-il2cpp.html

VS 2017 crashes when compiling template implementation

To make it short visual studio 2017 crashes when I am compiling this file:
#pragma once
/// #file
/// #brief Class mbe::HandleBase
#include <unordered_map>
//#include <cassert>
namespace mbe
{
template <class Derived>
class HandleBased abstract
{
public:
typedef unsigned long long int HandleID;
public:
HandleBased();
~HandleBased();
// Maybe rename to GetHandleId()?
HandleID ThisHandleId();
/*{
return id;
}*/
// Maybe rename to FindHandledObject
static Derived * FindPtr(HandleID id)
{
auto it = HandleBased::GetMap().find(id);
if (it == HandleBased::GetMap().end())
return nullptr;
// Should always be save
//assert(dynamic_cast<Derived *>(it->second));
return static_cast<Derived *>(it->second);
}
private:
static HandleID NextHandle()
{
// Every handle will get its own unique id
static HandleID next = 0;
return next++;
}
static std::unordered_map<HandleID, HandleBased *>& GetMap()
{
// Create the static map which will be used to keep track of the Derived handles and their ids
static std::unordered_map<HandleID, HandleBased *> map;
return map;
}
private:
HandleID id; // The id of this handle object
};
#pragma region Template Implementation
template<class Derived>
HandleBased<Derived>::HandleBased() :
id(NextHandle())
{
HandleBased::GetMap()[id] = this;
}
template<class Derived>
HandleBased<Derived>::~HandleBased()
{
auto it = HandleBased::GetMap().find(id);
HandleBased::GetMap().erase(it);
}
template<class Derived>
inline HandleID HandleBased<Derived>::ThisHandleId()
{
return id;
}
#pragma endregion
} // namespace mbe
It compiles fine when the ThisHandleId() function is defined directly below its definition. Is something wrong with my template implementation? I have noticed that the HandleID typedef does not show up in intellisense.
Sometimes VS crashes completely (goes grey and windows displays the message: "Visual Studio 2017 stopped working". Sometimes it just shows that ingame message: "C/C++ optimising compiler stopped working"
Furthermore, I get a ton of compile errors when defining the other functions beneath the HandleBase class or in an inline file. As I said, everything compiles just fine if all functions are implemented just beneath their definition. I have also experimented with removing inline which avoids the crash but gives me even more compile errors. Mosty complete non-sense such as:
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(75): warning C4346: "ThisHandleId": Abhängiger Name ist kein Typ
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(76): note: Präfix mit "typename" zum Angeben eines Typs
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(76): error C2988: Unerkannte Vorlagendeklaration/-definition
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(76): error C2059: Syntaxfehler: ""
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(76): error C2143: Syntaxfehler: Es fehlt ";" vor "{"
2>c:\users\adrian\documents\visual studio 2017\projects\mars base engine ecs 5\mars base engine ecs\handlebase.h(76): error C2447: "{": Funktionsheader fehlt - Parameterliste im alten Stil?
Sorry for the German comments, but u can probably guess what some of them meen. There is stuff like 'depended name is not a type', 'syntax error ""' and 'missing an ; before {'
Also, I don't think removing the inline is a good idea in the first place.
In case you are wondering what the code is for, its described in the acceted answer of this stack overflow question: Using shared_ptr for unique ownership (kind of) - is this good practice?
Hope you can help me with this weird occurence....
Thanks,
Adrian
HandleID is a scoped type. Hence, you will need to use HandleBased<Derived>::HandleID. Furthermore, since HandleID is a dependent type. Hence, you will need to use typename HandleBased<Derived>::HandleID.
Use:
template<class Derived>
inline typename HandleBased<Derived>::HandleID HandleBased<Derived>::ThisHandleId()
{
return id;
}
Alternatively, use trailing return type (Thanks are due to #Angew):
template <class Derived>
auto HandleBase<Derived>::ThisHandleId() -> HandleId
{
return id;
}
That works since trailing return types are within the scope of the class.

Serial Ports in a .DLL file

I have created .dll file in Visual C++.
It includes a function which takes two arguments and send data through serial port. Next i wanted to include this .dll in my application and call these functions. But i am unable to call these functions. Kindly Help.
Here is header file for my .dll
namespace positioncontrol
{
using namespace std;
using namespace System;
using namespace System::IO::Ports;
public ref class control
{
static int rotate(char a, String^ b);
};
}
And here is .cpp for my .dll
#include "goniometer.h"
namespace positioncontrol
{
void control::rotate(char a, String^ b)
{ SerialPort^ serialPort = gcnew SerialPort(L"COM5",9600,Parity::None,1,StopBits::One);
int inp_rotation;
array<unsigned char>^ inp_c = gcnew array<unsigned char>(2);
String^ inp_string;
inp_c[0] = a;
inp_string= b;
inp_rotation=Int32::Parse(inp_string);
inp_c[1] = (unsigned char)inp_rotation;
serialPort->Write(inp_c,0,2);
}
}
I am using this .dll in a desktop application. I have include the header file
#ifndef goniometer_h
#define goniometer_h
#include "goniometer.h"
#endif
Added the path for include directories and added .dll as a reference.
Now i am using the function defined in .dll for a click event
private: System::Void button9_Click(System::Object^ sender, System::EventArgs^ e) {
char dir;
dir = 0x42;
String^ inp_string;
inp_string=enter_degree->Text;
positioncontrol::control::rotate(dir,inp_string);
}
Now when i build my desktop application i am getting following error
1>C:\Users\DELL\Desktop\Final\Motor_Dual_API\Debug\goniometer.h(10): error C2011: 'positioncontrol::control' : 'class' type redefinition
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(530): error C3861: 'rotate': identifier not found
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C2027: use of undefined type 'positioncontrol::control'
1> c:\users\dell\desktop\final\vc++dll\debug\goniometer.dll : see declaration of 'positioncontrol::control'
1>c:\users\dell\desktop\final\motor_dual_api\motor_next\Form1.h(540): error C3861: 'rotate': identifier not found
Kindly help me in figuring out error.
Thanks and Regards
In C++, the namespace separator is :: rather than .. Check your using statement.
Your code does not define serialPort. Add the definition to the global namespace, to the namespace positionControl or as an auto variable in your function
From the semantic point of view, serialPort is a pointer. So must also create an instance of the object, where serialPort points to.

VC++ 2010 template typedef typename error

I am trying to migrate a project to VC++ 2010
The project contains the file TabbedMDI.h (by Danial Bowen) which gives the error "named followd by '::' must be a class or namespace name for the line
"typedef typename TClient::TTabCtrl TTabCtrl;"
code snipit follows
template <
class T,
class TClient = CTabbedMDIClient< CDotNetTabCtrl<CTabViewTabItem> >,
class TBase = WTL::CMDIWindow,
class TWinTraits = ATL::CFrameWinTraits>
class ATL_NO_VTABLE CTabbedMDIFrameWindowImpl :
public WTL::CMDIFrameWindowImpl<T, TBase, TWinTraits >
{
public:
// Expose the type of MDI client
typedef typename TClient TClient;
// Expose the type of tab control
typedef typename TClient::TTabCtrl TTabCtrl;
// Member variables
protected:
TClient m_tabbedClient;
Just compiled Daniel's SimpleTabbedMDIDemo sample from Custom Tab Controls, Tabbed Frame and Tabbed MDI with VC2010 Express (WTL 8.1, ATL 8.00 from WinDDK) without problem (except manifest duplication).
Your problem is elsewhere.
[Edit]
Do you use the latest TabbedMDI.h? mine has:
// History (Date/Author/Description):
// ----------------------------------
//
// 2005/07/13: Daniel Bowen
// - Namespace qualify the use of more ATL and WTL classes.
// - CTabbedMDIFrameWindowImpl:
// * Add GetMDITabCtrl

Resources