Creating a Window via Plugin Problem

Discussion in 'Plugins' started by Darrter, Jun 29, 2013.

  1. Darrter

    Darrter Registered

    Joined:
    Jun 26, 2013
    Messages:
    15
    Likes Received:
    0
    Hey,
    I am trying to create a sort of plugin, which is showing some telemetry data on a extra monitor.
    I thought it would be the best way to create a create a new window via the plugin, so it would be easy to update the stats.

    I also can think of a solution in which i would have a seperate application and a plugin which send the data to this other application in some way.
    So i am using the ExamplePlugin and buildiing ontop of it, just to test it out.
    First step i created a class which is handling the creatin of of the window.
    This is what it looks like:

    Window.h
    Code:
    #ifndef _Window_h_
    #define _Window_h_
    
    #include <Windows.h>
    #include <string>
    
    class CWindow
    {
    private:
    
    	WNDCLASSEX wndClass;
    	std::string sWndClassName;
    	HWND hwnd;
    	HWND hwndlable;
    
    	static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
    
    public:
    	CWindow();
    	~CWindow();
    
    	int Init(HINSTANCE hInstance, HWND hwndParent);
    
    	//Get
    	HWND GetHwnd();
    	HWND GetHwndLable(){return hwndlable;};
    };
    
    #endif//_Window_h_
    Window.cpp
    Code:
    #include "Window.h"
    
    CWindow::CWindow()
    {
    	ZeroMemory(&wndClass, sizeof(wndClass));
    	sWndClassName = "Darter";
    	hwnd = 0;
    	hwndlable = 0;
    }
    
    CWindow::~CWindow()
    {
    
    }
    
    LRESULT CALLBACK CWindow::WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {	
    	//Reads the class pointer, so i have access to the member vars
    	CWindow* This = (CWindow*)GetWindowLong(hwnd, GWL_USERDATA); 
    
        switch (message)
        {    
    	case WM_CREATE:
    		This = (CWindow *) (((LPCREATESTRUCT) lParam)->lpCreateParams); 
    		SetWindowLong(hwnd, GWL_USERDATA, long(This));
    
    		This->hwndlable = CreateWindow("static", "Nur ein kleiner test",
    									WS_CHILD | WS_VISIBLE | WS_TABSTOP,
    									5, 5, 250, 80,
    									hwnd,
    									NULL,
    									(HINSTANCE) GetWindowLong (hwnd, GWL_HINSTANCE), NULL);
    
    
    		break;
        //case WM_DESTROY:
            //PostQuitMessage(0);
          //  break;
        default:
            return DefWindowProc(hwnd, message, wParam, lParam);
            break;
        }
        return 0;
    }
    
    int CWindow::Init(HINSTANCE hInstance, HWND hwndParent)
    {
    
    	wndClass.cbSize			= sizeof(WNDCLASSEX);
    	wndClass.style			= CS_HREDRAW | CS_VREDRAW;
    	wndClass.lpfnWndProc	= CWindow::WndProc;
    	wndClass.cbClsExtra		= 0;
    	wndClass.cbWndExtra		= 0;
    	wndClass.hInstance		= hInstance;
    	wndClass.hIcon			= LoadIcon(wndClass.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    	wndClass.hCursor		= LoadCursor(NULL, IDC_ARROW);
    	wndClass.hbrBackground	= (HBRUSH)(COLOR_WINDOW+1);
    	wndClass.lpszMenuName	= NULL;
    	wndClass.lpszClassName	= sWndClassName.c_str();
    	wndClass.hIconSm		= LoadIcon(wndClass.hInstance, MAKEINTRESOURCE(IDI_APPLICATION));
    
    	if(!RegisterClassEx(&wndClass))
    	{
    		MessageBox(NULL, "Call to RegisterClassEx failed!", "Error", NULL);
    		return 1;
    	}
    	
    	//char className[256];
    	//GetClassName(hwndParent, className, 256);
    
    	//WNDCLASSEX wce;
    	//GetClassInfoEx(GetModuleHandle(nullptr), className, &wce);
    
    
    	hwnd = CreateWindow(wndClass.lpszClassName,
    						"Test Fenster",
    						WS_BORDER | WS_SYSMENU | WS_MINIMIZEBOX,
    						CW_USEDEFAULT,
    						CW_USEDEFAULT,
    						300,
    						150,
    						hwndParent,
    						NULL,
    						wndClass.hInstance,
    						this				//Nicht vergessen !!!
    						);
    		
    	if(hwnd == 0)
    	{
    		return 1;
    	}
    		
    	ShowWindow(hwnd, SW_SHOW);
    	UpdateWindow(hwnd);
    
    	return 0;
    }
    
    HWND CWindow::GetHwnd()
    {
    	return hwnd;
    }
    The Example.cpp ist just modified to create a instance of the class CWindow and call Init.

    The creation of the window works just fine, but as soon as i click this new window it will stop responsing and killing rfactor2.
    So i would just like to know whats happening there?
    Is there a way to make this working or do i have to use a seperate application for displaying the data? If so i would love any recommendations how i get the data out of rfactor 2 to my seperate application?

    greetings
    Darter
     
  2. Lazza

    Lazza Registered

    Joined:
    Oct 5, 2010
    Messages:
    12,382
    Likes Received:
    6,600
    Wouldn't clicking the other window take the focus off rF2, putting it into a sort of sleep mode where your plugin wouldn't be getting any updates? Just a guess ;)
     
  3. Darrter

    Darrter Registered

    Joined:
    Jun 26, 2013
    Messages:
    15
    Likes Received:
    0
    Yeah thats true.
    So i have to display the stats in a seperate app i guess. Thats fine, but how would i get the data from rfactor2 into my application?
    I think it would not be clever writing the data into a file and reading this file from my seperate application?
    Writing the Data into a Database would be better?
     
  4. B1K3R

    B1K3R Registered

    Joined:
    Apr 6, 2012
    Messages:
    1,605
    Likes Received:
    88

    As far as I know there are two ways how to get data from the internals plugin. You can either write the data to a text file and then have a separate app to load the text files into a database or otherwise you'll have an app which listens to a tcp connection from plugin where you write the streamed data to a database.

    Writing data to a database directly from the plugin is not advisable. I have tried it and it slows down the rF2 game and consequently the FPS of the game goes very down or it might even crash it. I have tried this.

    Currently, I am writing data to files and then load it to the database. It works without any problems.
     

Share This Page