It is currently March 28th, 2024, 8:43 am

How to get value from a custom made plugin

Share and get help with Plugins and Addons
Post Reply
Lopzx
Posts: 3
Joined: July 4th, 2021, 11:04 am

How to get value from a custom made plugin

Post by Lopzx »

Hi, I tried to write a plugin that returns a string Hour: Minutes
I don't know how to get it to my ini file

I believe this is the API to call ?
RmReadString(rm, measure->Str.c_str(), L"Not Working");

The code is under:
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue) Function

Short Explanation Of The Code:
1. Getting Hours and Minute with window API
2.Store it into Measure struct as wide string
3.call RmReadString function using WideString inside Struct as a argument

Code: Select all

	#include <Windows.h>
#include "../../API/RainmeterAPI.h"
#include <string>


struct Measure
{
	SYSTEMTIME lt;
	std::wstring Str;

	Measure()
	{
		GetLocalTime(&lt);
	}
};

PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
	Measure* measure = new Measure;
	*data = measure;
}




PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
	Measure* measure = (Measure*)data;
	unsigned short Hours = measure->lt.wHour;
	unsigned short Minutes = measure->lt.wMinute;
	measure->Str = std::wstring(Hours + L":" + Minutes);
	LPCWSTR value = RmReadString(rm, measure->Str.c_str(), L"Not Working");
}

PLUGIN_EXPORT double Update(void* data)
{
	Measure* measure = (Measure*)data;
	return 0.0;
}

PLUGIN_EXPORT LPCWSTR GetString(void* data)
{
	Measure* measure = (Measure*)data;
	return L"";
}

PLUGIN_EXPORT void Finalize(void* data)
{
	Measure* measure = (Measure*)data;
	delete measure;
}

Thank you.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: How to get value from a custom made plugin

Post by SilverAzide »

Lopzx wrote: July 4th, 2021, 11:17 am Hi, I tried to write a plugin that returns a string Hour: Minutes
I don't know how to get it to my ini file
Sorry to answer a question with a question, but are you doing this just to learn how to write a plugin? I ask because Rainmeter already has a measure that does what you are trying to do, if I understand you correctly.

Code: Select all

[Measure24HrTime]
Measure=Time
Format=%H:%M
Lopzx
Posts: 3
Joined: July 4th, 2021, 11:04 am

Re: How to get value from a custom made plugin

Post by Lopzx »

SilverAzide wrote: July 4th, 2021, 3:17 pm Sorry to answer a question with a question, but are you doing this just to learn how to write a plugin? I ask because Rainmeter already has a measure that does what you are trying to do, if I understand you correctly.

Code: Select all

[Measure24HrTime]
Measure=Time
Format=%H:%M

Yeah, it was just a learning purpose I have just started learning C++ to prepare before I get to uni, I thought maybe I should try to create something with basic knowledge I know there are already built-in plugin, thank you for your reply.
User avatar
Brian
Developer
Posts: 2673
Joined: November 24th, 2011, 1:42 am
Location: Utah

Re: How to get value from a custom made plugin

Post by Brian »

You are close, but missing a few key aspects.

The functions that start with "Rm" (like RmReadString) get option values from the plugin measure (from the skin .ini file). The functions that return values to the measure are Update for the number value of the measure, and GetString for string value of the measure.

Here is some more information on the plugin interface.
https://docs.rainmeter.net/developers/plugin/plugin-anatomy/
https://docs.rainmeter.net/developers/plugin/cpp/
https://docs.rainmeter.net/developers/plugin/cpp/api/

So, you have all the functions defined ok, you just need to build the string correctly and return it in GetString function.

I won't go into too much of the specifics the C++ programing language since that is beyond the scope of this support forum, but I will give you a few pointers.

First, I would set up your plugin like this:

Code: Select all

#include <Windows.h>
#include "../../API/RainmeterAPI.h"
#include <string>

struct Measure
{
	std::wstring returnedString;

	Measure() : returnedString(L"")
	{
	}
};

PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
	Measure* measure = new Measure;
	*data = measure;

	// This example won't need to initialize anything other than
	// our local measure structure.
}

PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
	Measure* measure = (Measure*)data;

	// This example doesn't need to read any options from the measure.
}

PLUGIN_EXPORT double Update(void* data)
{
	Measure* measure = (Measure*)data;

	// This is the function that should do the "work" of your plugin for
	// both the number value and string value of the measure.

	// Make sure to build the string and save it to your measure
	// structure. For example: measure->returnedString = L"...";

	return 0.0;
}

PLUGIN_EXPORT LPCWSTR GetString(void* data)
{
	Measure* measure = (Measure*)data;

	// This function is where you return a string value back to the measure.
	// Note: This function is optional and is not needed if your plugin
	// only returns numbers.

	return measure->returnedString.c_str();
}

PLUGIN_EXPORT void Finalize(void* data)
{
	Measure* measure = (Measure*)data;
	delete measure;

	// This example doesn't need to do any cleanup other than
	// our local measure structure.
}
Depending on your goal, you will probably want to call GetLocalTime in your Update function. This will get the local time each time the skin is Updated (assuming that is your goal). If you goal is to just get the time once, then you might get the time in the Initialize function (or the Measure constructor like you did before).

Either way, once you get the time, you will need to "build" your string correctly and save it in your Measure structure.



You may want to read up on the standard string library, since you cannot do this: measure->Str = std::wstring(Hours + L":" + Minutes);

Here is some good C++ references on the string library.
https://www.cplusplus.com/reference/string/wstring/
https://www.cplusplus.com/reference/string/to_wstring/

I hope that helps.

-Brian
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get value from a custom made plugin

Post by jsmorley »

Here is one approach that worked for me...

Code: Select all

#include <Windows.h>
#include "../../API/RainmeterAPI.h"
#include <string>

struct Measure
{
	std::wstring returnedString;

	Measure() {}
};

PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
	Measure* measure = new Measure;
	*data = measure;
}

PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
	Measure* measure = (Measure*)data;
}

PLUGIN_EXPORT double Update(void* data)
{
	Measure* measure = (Measure*)data;

	SYSTEMTIME lt;
	GetLocalTime(&lt);

	measure->returnedString = std::to_wstring(lt.wHour) + L":" + std::to_wstring(lt.wMinute);

	return 0.0;
}

PLUGIN_EXPORT LPCWSTR GetString(void* data)
{
	Measure* measure = (Measure*)data;

	return measure->returnedString.c_str();
}

PLUGIN_EXPORT void Finalize(void* data)
{
	Measure* measure = (Measure*)data;
	delete measure;
}
So aside from moving GetLocalTime() into the Update() function so it happens on each skin/measure update, I built the string returnedString, which is declared as a std::wstring() in the Measure Struct, converting the numeric GetLocalTime() members lt.wHour and lt.wMinute using std::to_wstring() and returning the string value to the measure using the GetString() function.


The fact that 10:02 is returned as 10:2 is another matter, beyond the scope of this theoretical discussion I guess. I imagine that you might want to to_wstring() lt.wMinute to a string variable and add leading 0's as desired before using it in returnedString.

Maybe something like:

Code: Select all

PLUGIN_EXPORT double Update(void* data)
{
	Measure* measure = (Measure*)data;

	SYSTEMTIME lt;
	GetLocalTime(&lt);
	
	std::wstring sMinute;
	if (lt.wMinute < 10) {
		sMinute = L"0" + std::to_wstring(lt.wMinute);
	}
	else {
		sMinute = std::to_wstring(lt.wMinute);
	}

	measure->returnedString = std::to_wstring(lt.wHour) + L":" + sMinute;

	return 0.0;
}

1.jpg
Lopzx
Posts: 3
Joined: July 4th, 2021, 11:04 am

Re: How to get value from a custom made plugin

Post by Lopzx »

you are right my Hours and Minutes Variable implicitly convert its type so it didn't work.
The actual question was actually how to get the value to my ini file
after reading some documentation i come up with this:

Code: Select all

[ClockMeasure]
DynamicVariables=1
Measure = Plugin
Plugin = Time

[ClockMeter]
Meter=String
MeasureName = ClockMeasure
X=50%
Y=30
W=200
H=20
FontColor=255,255,255,255
Text=Time: %1
ClockMeasure use the plugin
ClockMeter call the function return and display it.
This is my understanding (CMIIW)

and also,
Thanks a lot to all of you who replied I really appreciate it I'm able to get it working.
Last edited by balala on July 6th, 2021, 3:44 pm, edited 1 time in total.
Reason: Please use <code> tags whenever are posting code snippets. It's the </> button.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get value from a custom made plugin

Post by jsmorley »

Lopzx wrote: July 6th, 2021, 9:53 am you are right my Hours and Minutes Variable implicitly convert its type so it didn't work.
The actual question was actually how to get the value to my ini file
after reading some documentation i come up with this:

[ClockMeasure]
DynamicVariables=1
Measure = Plugin
Plugin = Time

[ClockMeter]
Meter=String
MeasureName = ClockMeasure
X=50%
Y=30
W=200
H=20
FontColor=255,255,255,255
Text=Time: %1

ClockMeasure use the plugin
ClockMeter call the function return and display it.
This is my understanding (CMIIW)

and also,
Thanks a lot to all of you who replied I really appreciate it I'm able to get it working.
That is right. Good job. The only minor issue is that you can't define X as a percentage in a meter. That syntax is only applicable for X in Rainmeter.ini, where you are positioning the "skin" on the "screen". It won't work positioning a "meter" on a "skin". Trying to set X in a meter to a percentage is in a sense a circular reference.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: How to get value from a custom made plugin

Post by jsmorley »

If you want to center a meter in a skin, you might use:

Code: Select all

[Rainmeter]
Update=1000
DynamicWindowSize=1
AccurateText=1

[Variables]

[MeasureTime]
Measure=String
String=6:36

[MeterTime]
Meter=String
MeasureName=MeasureTime
X=200
W=400
StringAlign=Center
FontSize=15
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1

1.jpg


So you set the X (the horizontal anchor) of the meter at 1/2 of the W of the meter, (which being the only meter, is also the width of the skin) and then StringAlign it to the center. Don't think of this as having anything to do with where the meter is on the screen though. That isn't the job, or within the capabilities of a "meter". Positioning on the screen is done by moving dragging or positioning the "skin" on the screen.
Post Reply