NextPVR Forums
  • ______
  • Home
  • New Posts
  • Wiki
  • Members
  • Help
  • Search
  • Register
  • Login
  • Home
  • Wiki
  • Members
  • Help
  • Search
NextPVR Forums Public Developers v
« Previous 1 … 17 18 19 20 21 … 93 Next »
c# ==> c++/cpp Need Help

 
  • 0 Vote(s) - 0 Average
c# ==> c++/cpp Need Help
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#1
2010-11-28, 05:49 PM (This post was last modified: 2010-11-28, 06:50 PM by kindt nick.)
Hi,

I'm trying to make a LCD/VFD interface plugin for all iMon.
Problem is that the API should be written in C++...

I already got the code for IEventNotification up and running but only in c#...
Would anybody care to help me out (short) converting this code to c++ code?
I've been trying all day but sinds I'm an embedded programmer, no luck...
Also searched in the forum, downloaden source code from the wiki... still crap...

It is based on the code that was generated here:
http://forums.gbpvr.com//showthread.php?47939-NPVR-IEventNotification&p=388441&highlight=#post388441

Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

using NUtility;
using NUtility.Base;
using NUtility.Controls;

namespace nPVR_iMON_API
{

    public class TestEventNotify : IEventNotification
    {

        //
        public TestEventNotify()
        {
            Logger.Debug("nPVR_iMON_API: Testing; Debug Message System...");
            Logger.Error("nPVR_iMON_API: Testing; Error Message System...");
            Logger.Info("nPVR_iMON_API: TestEventNotify v" + this.GetType().Assembly.GetName().Version.ToString() + " called by 'nPVR_iMON_API'-plugin");
            EventBus.GetInstance().AddListener(this);
        }

        public void Notify(string eventType, object eventText)
        {
            try
            {
                Logger.Debug("nPVR_iMON_API: EventType; " + eventType.ToString());
                Logger.Debug("nPVR_iMON_API: EventText; " + eventText.ToString());
            }

            catch (Exception ex)
            {
                Logger.Error("nPVR_iMON_API: Exception Message; " + ex.Message);
                Logger.Error("nPVR_iMON_API: Exception; " + ex.ToString());
            }
        }

    }

}

I already have this:
Code:
using namespace System::Text;

using namespace NUtility;
using namespace NUtility::Base;
using namespace NUtility::Controls;

using namespace std;

public ref class cTestEventNotify : public IEventNotification // Defining the class
{

public:
  cTestEventNotify();     // Constructor
  //~cTestEventNotify();     // Destructor
  virtual void Notify(System::String^ eventType,
              System::Object^ eventText);

};

///Constructors can accept arguments, but this one does not
cTestEventNotify::cTestEventNotify()
{
    Logger::Debug("nPVR_iMON_API: Testing; Debug Message System...");
    Logger::Error("nPVR_iMON_API: Testing; Error Message System...");
    //Logger::Info("nPVR_iMON_API: TestEventNotify v" + this::GetType()::Assembly.GetName()::Version::ToString() + " called by 'nPVR_iMON_API'-plugin");
    //EventBus::GetInstance();
    //EventBus::AddListener(this);
};

void cTestEventNotify::Notify(System::String^ eventType,
                              System::Object^ eventText)
{

};


// This is the main DLL file.

#include "stdafx.h"
#include "nPVR_iMON_API_cpp.h"


//cTestEventNotify fTestEventNotify;

But I can't seem to figure out how to convert the following lines into cc++ code:
Code:
[COLOR=darkred]EventBus.GetInstance().AddListener(this);[/COLOR]
Logger.Info("nPVR_iMON_API: TestEventNotify v" + this.GetType().Assembly.GetName().Version.ToString() + " called by 'nPVR_iMON_API'-plugin");


also, am I working the correct way? Declaring the entire class in the header and just create an instance int the code file?

Thx in advance!

Nick
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,823
Threads: 769
Joined: Nov 2003
#2
2010-11-28, 06:14 PM
kindt nick Wrote:Also I can't seem to figure out how to convert the following line into cc++ code:
Code:
[COLOR=darkred]EventBus.GetInstance().AddListener(this);[/COLOR]
EventBus::GetInstance()->AddListener(this);

I checked on of my C++ classes which implements IEventNotification. Relevent snippets of code below:

Header file has class implementing IEventNotification interface.
Code:
public ref class PlaybackNative: public NUtility::IEventNotification

Header file declares method
Code:
virtual void Notify(System::String ^eventName, System::Object ^eventArg);


.cpp file adds listener in constructor
Code:
PlaybackNative::PlaybackNative(void)
{    
    ...
    EventBus::GetInstance()->AddListener(this);
    ...
}

.cpp file implements method
Code:
void PlaybackNative::Notify(System::String ^eventName, System::Object ^eventArg)
{
    if (eventName == "WINDOW_RESIZE")
    {
        HandleWindowResize();
    }
}
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#3
2010-11-28, 09:06 PM
Hey Sub thx for the reply,

The dll project itself compiles without errors or warning

Thx,
Nick
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,823
Threads: 769
Joined: Nov 2003
#4
2010-11-28, 09:14 PM
Quote:2010-11-28 21:46:46.012 [DEBUG][1] About to check for plugin interfaces in: C:\Users\Public\NPVR\Plugins\nPVR_iMON_API_cpp\nPVR_iMON_API_cpp.dll
2010-11-28 21:46:46.012 [DEBUG][1] Plugin PDB also exists: C:\Users\Public\NPVR\Plugins\nPVR_iMON_API_cpp\nPVR_iMON_API_cpp.pdb
2010-11-28 21:46:46.076 [ERROR][1] Unexpected error: System.IO.FileLoadException: Unverifiable code failed policy check. (Exception from HRESULT: 0x80131402)
at System.Reflection.Assembly.nLoadImage(Byte[] rawAssembly, Byte[] rawSymbolStore, Evidence evidence, StackCrawlMark& stackMark, Boolean fIntrospection)
at System.AppDomain.Load(Byte[] rawAssembly, Byte[] rawSymbolStore)
at NUtility.PluginRegistry.LoadPluginsFromRoot(String pluginDirectoryBase)
Sorry, I dont know really know what this means. I've not tried to write plugins in C++ using unsafe native code (ie, 3rd party unmanaged code). You might be best to write your code in your plugin in c#, and just have a reference to a small C++ helper managed DLL that contains a class that does the native calls you need. You may even be able to do away with that helper DLL, and directly call the iMon API in it's DLL using PInvoke (effectively calling C/C++ functions in a DLL from C#).
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#5
2010-11-28, 09:55 PM
Hey sub,

Aparetly I have to compile the code with /clrConfusedafe... but it generates thousands of errors, some of them can be turned off.
The shitty part is that all of the error's are generated in .h files I don't use but I don't know how to exclude them from the build.
(Strange part is that they are actually not included in the project, just viewable in the "external dependencies")

Like I said before, I'm a noob when it comes to windows applications...
A couple of days ago I've read that there 's a solution to combine c# with c++ code in one solution via a COM...
But it sounds to difficult for what I wont to do...
Still, I'll I'll give it a go with the Pinvoke...

Would the structure be like this?
1. Write the API-dll in c++ which controls the display.
2. Write a dll in c# which extracts the information for nPVR,
Use pinvoke to call the functions that are needed in the c++ dll

Ps: I wanted to write it in c++ sinds the api is already written in c++ and the code that is needed to extract the info out of nPVR is so small...

thx,
Nick
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,823
Threads: 769
Joined: Nov 2003
#6
2010-11-28, 11:23 PM
kindt nick Wrote:Would the structure be like this?
1. Write the API-dll in c++ which controls the display.
2. Write a dll in c# which extracts the information for nPVR,
Use pinvoke to call the functions that are needed in the c++ dll
The two models I can think of include

Model #1 using PInvoke
- write the plugin DLL implementing IEventNotification in C#
- your plugin c# code uses PInvoke to call the iMon API calls in it's DLL (see http://msdn.microsoft.com/en-us/magazine/cc164123.aspx for examples of calling DLL functions directly from )

Model #2 using two DLLs
- write the main plugin DLL, implementing IEventNotification, using C#.
- write a secondary DLL in C++, much like you're already done, linking to the iMon API library, or dynamically loading it's DLL or whatever you were originally intending to do. This DLL would probably contain a managed C++ class, that exposes the functions you'd like C# to be able to do (for example, InitDevice, SendMessage, CloseDevice), acting as a simple wrapper between .net and the imon API.
- C# DLL has a reference to the secondary C++ DLL, and creates an instances of the helper class in that DLL (something like ImonHelperClass), and call method when it needs stuff sent to the device, ie imonHelperClass.SendMessage(something).

Quote:Ps: I wanted to write it in c++ sinds the api is already written in c++ and the code that is needed to extract the info out of nPVR is so small...
Fair enough, but I dont really know anything about the error you're getting. You could stick with this scheme if you want to work though the issues.
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#7
2010-11-29, 10:00 PM
Thx,

I'll give it a go!
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#8
2010-12-22, 05:00 PM
Hey,

the plugin is almost ready.
The basics are written, still I've got a major problem, which I can't resolve...
It concerns handling messages between windows...

Problem is in certain cases it works, in other cases it does not.
I gues i'm missing some info on how the plugin is handled by nPVR...

For example, when I init the API, I receive a message from the API with the state of the API and the displaytype.
I can send text, (de)activate icons, etc... All that without getting errors.

Still, when the API closes, I shoud receive a message that the api is closed, same for restart, and (important one) that the text is finished scrolling...
I've tried looking for other messages that do not come from the API. Still, no luck.

I'm using a "protected override" to handle messages.

Is it possible that nPVR blocks certain messages? Or maybe I'm declaring something wrong in the so that the pluging does not run in a independent thread. I don't know.

Below you can find a part of the code:
Code:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using NUtility;
using NUtility.Base;
using NUtility.Controls;
//using iMon.DisplayApi;

namespace iMon.DisplayApi
{

    public class TestEventNotify : IEventNotification
    {

        public TestEventNotify()
        {
            EventBus.GetInstance().AddListener(this);
        }

        public void Notify(string eventType, object eventText)
        {
            try
            {
                if (eventText  != null)
                    iMonApi.EventHandler(eventType.ToString(), eventText.ToString());
                else
                    iMonApi.EventHandler(eventType.ToString(), "");
            }

            catch (Exception ex)
            {
                Logger.Error("nPVR_iMON_API: Exception Message; " + ex.Message);
                Logger.Error("nPVR_iMON_API: Exception; " + ex.ToString());
                Logger.Info("nPVR_iMON_API: EventType was: " + eventType.ToString());
                Logger.Info("nPVR_iMON_API: EventText was: " + eventText.ToString());
            }
        }
    }


    public class iMonApi : Control
    {
        #region Private

            //CONSTANTS
            private const int WM_APP = 0x8000;
            private const int WM_DSP_PLUGIN_NOTIFY = WM_APP + 1001;

            //STRUCTS
            public struct DSPNotifyStruct
            {
                public iMonNativeApi.iMonDisplayNotifyCode NotifyCode;
                public iMonNativeApi.iMonDisplayType DispType;
                public iMonNativeApi.iMonDisplayResult Result;
            }

            //VARIABLES
            private DSPNotifyStruct APINotifyInfo;
            private string PrevEventType;
            private string PrevEventText;

            //WNDPROC
            protected override void WndProc(ref Message msg)
            {
                //Create Static Instance of the class
                iMonApi iMonApiInstance = new iMonApi();

                //Process unkown Messages
                if (msg.Msg != WM_DSP_PLUGIN_NOTIFY)
                {
                    if (msg.Msg != null)
                        Logger.Info("nPVR_iMON_API: Message Received from Unknow Source: Msg: " + msg.Msg.ToString());

                    if (msg.WParam != null)
                        Logger.Info("nPVR_iMON_API: Message Received from Unknow Source: WParam: " + msg.WParam.ToString());

                    if (msg.LParam != null)
                        Logger.Info("nPVR_iMON_API: Message Received from Unknow Source: LParam: " + msg.LParam.ToString());
                }
                //Process known Messages
                else
                {
                    //Create Temp values
                    iMonNativeApi.iMonDisplayResult Result;

                    //Clear info structStruct
                    APINotifyInfo = new DSPNotifyStruct();

                    //Process WParam
                    APINotifyInfo.NotifyCode = (iMonNativeApi.iMonDisplayNotifyCode)msg.WParam;

                    //Process LParam
                    switch (APINotifyInfo.NotifyCode)
                    {
                        case iMonNativeApi.iMonDisplayNotifyCode.PluginSuccess:
                            APINotifyInfo.DispType = (iMonNativeApi.iMonDisplayType)msg.LParam;
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.PluginFailed:
                            APINotifyInfo.Result = (iMonNativeApi.iMonDisplayResult)msg.LParam;
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.iMonRestarted:
                            APINotifyInfo.DispType = (iMonNativeApi.iMonDisplayType)msg.LParam;
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.iMonClosed:
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.HardwareConnected:
                            APINotifyInfo.DispType = (iMonNativeApi.iMonDisplayType)msg.LParam;
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.HardwareDisconnected:
                            APINotifyInfo.Result = (iMonNativeApi.iMonDisplayResult)msg.LParam;
                            break;

                        case iMonNativeApi.iMonDisplayNotifyCode.LCDTextScrollDone:
                            iMonApiInstance.DisplaySetLcdText(PrevEventText);
                            break;
                    }

                    //Perform additional necessary tasks
                    if (APINotifyInfo.DispType == iMonNativeApi.iMonDisplayType.LCD)
                        Result = iMonApiInstance.DisplaySetLcdText("nPVR");

                }

                base.WndProc(ref msg);
            }

thx in advance,
Nick
sub
Offline

Administrator

NextPVR HQ, New Zealand
Posts: 106,823
Threads: 769
Joined: Nov 2003
#9
2010-12-22, 05:34 PM
kindt nick Wrote:HI'm using a "protected override" to handle messages.

Is it possible that nPVR blocks certain messages? Or maybe I'm declaring something wrong in the so that the pluging does not run in a independent thread. I don't know.
NPVR doesnt know anything about your Window class, so shouldnt be blocking messages delivered to it's WndProc method.

I dont know anything about the iMon API, so cant provide too much advise on it's use. I'm guessing the problem relates to the Window you're creating, or its live time, or not freeing the iMon API correctly or something like that.
kindt nick
Offline

Member

Posts: 128
Threads: 9
Joined: May 2008
#10
2010-12-25, 03:06 AM (This post was last modified: 2010-12-28, 10:22 PM by kindt nick.)
Hey,

Fixed!
Problem was that I've created more than one instance of the API<->EventHandler.

thx in advance,
Nick
« Next Oldest | Next Newest »

Users browsing this thread: 1 Guest(s)

Pages (2): 1 2 Next »


  • View a Printable Version
  • Subscribe to this thread
Forum Jump:

© Designed by D&D, modified by NextPVR - Powered by MyBB

Linear Mode
Threaded Mode