11/*
2- Copyright (C) 2017 Trevor Hamilton
2+ Copyright (C) 2017 Trevor Hamilton
33
4- This program is free software; you can redistribute it and/or
5- modify it under the terms of the GNU General Public License
6- as published by the Free Software Foundation; either version 2
7- of the License, or (at your option) any later version.
4+ This program is free software; you can redistribute it and/or
5+ modify it under the terms of the GNU General Public License
6+ as published by the Free Software Foundation; either version 2
7+ of the License, or (at your option) any later version.
88
9- This program is distributed in the hope that it will be useful,
10- but WITHOUT ANY WARRANTY; without even the implied warranty of
11- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12- GNU General Public License for more details.
9+ This program is distributed in the hope that it will be useful,
10+ but WITHOUT ANY WARRANTY; without even the implied warranty of
11+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+ GNU General Public License for more details.
1313
14- You should have received a copy of the GNU General Public License
15- along with this program; if not, write to the Free Software
16- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
14+ You should have received a copy of the GNU General Public License
15+ along with this program; if not, write to the Free Software
16+ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
1717*/
1818
1919using System ;
@@ -68,8 +68,8 @@ static public implicit operator Measure(IntPtr data)
6868 {
6969 return ( Measure ) GCHandle . FromIntPtr ( data ) . Target ;
7070 }
71- public string inputStr ; //The string returned in GetString is stored here
72- public IntPtr buffer ; //Prevent marshalAs from causing memory leaks by clearing this before assigning
71+
72+ public string inputStr ;
7373 }
7474
7575 public class Plugin
@@ -87,7 +87,6 @@ public static void Reload(IntPtr data, IntPtr rm, ref double maxValue)
8787 Measure measure = ( Measure ) data ;
8888 Rainmeter . API api = ( Rainmeter . API ) rm ;
8989
90- //Read measure for an Input string
9190 measure . inputStr = api . ReadString ( "Input" , "" ) ;
9291 }
9392
@@ -102,72 +101,43 @@ public static double Update(IntPtr data)
102101 public static IntPtr GetString ( IntPtr data )
103102 {
104103 Measure measure = ( Measure ) data ;
105- if ( measure . buffer != IntPtr . Zero )
106- {
107- Marshal . FreeHGlobal ( measure . buffer ) ;
108- measure . buffer = IntPtr . Zero ;
109- }
110-
111- measure . buffer = Marshal . StringToHGlobalUni ( measure . inputStr ) ;
112- return measure . buffer ;
104+ return Rainmeter . StringBuffer . Update ( measure . inputStr ) ;
113105 }
114106
115107 [ DllExport ]
116108 public static IntPtr ToUpper ( IntPtr data , int argc ,
117109 [ MarshalAs ( UnmanagedType . LPArray , ArraySubType = UnmanagedType . LPWStr , SizeParamIndex = 1 ) ] string [ ] argv )
118110 {
119111 Measure measure = ( Measure ) data ;
120- if ( measure . buffer != IntPtr . Zero )
121- {
122- Marshal . FreeHGlobal ( measure . buffer ) ;
123- measure . buffer = IntPtr . Zero ;
124- }
125112
126- //If we are given one or more arguments convert to uppercase the first one
113+ // If we are given one or more arguments convert to uppercase the first one
127114 if ( argc > 0 )
128115 {
129- measure . buffer = Marshal . StringToHGlobalUni ( argv [ 0 ] . ToUpper ( ) ) ;
130- }
131- //If we are given no arguments convert to uppercase the string we recived with the input option
132- else
133- {
134- measure . buffer = Marshal . StringToHGlobalUni ( measure . inputStr . ToUpper ( ) ) ;
116+ return Rainmeter . StringBuffer . Update ( argv [ 0 ] . ToUpper ( ) ) ;
135117 }
136- return measure . buffer ;
118+
119+ // If we are given no arguments convert to uppercase the string we recived with the input option
120+ return Rainmeter . StringBuffer . Update ( measure . inputStr . ToUpper ( ) ) ;
137121 }
138122
139123 [ DllExport ]
140124 public static IntPtr ToLower ( IntPtr data , int argc ,
141125 [ MarshalAs ( UnmanagedType . LPArray , ArraySubType = UnmanagedType . LPWStr , SizeParamIndex = 1 ) ] string [ ] argv )
142126 {
143127 Measure measure = ( Measure ) data ;
144- if ( measure . buffer != IntPtr . Zero )
145- {
146- Marshal . FreeHGlobal ( measure . buffer ) ;
147- measure . buffer = IntPtr . Zero ;
148- }
149128
150- //If we are given one or more arguments convert to uppercase the first one
151129 if ( argc > 0 )
152130 {
153- measure . buffer = Marshal . StringToHGlobalUni ( argv [ 0 ] . ToUpper ( ) ) ;
154- }
155- //If we are given no arguments convert to uppercase the string we recived with the input option
156- else
157- {
158- measure . buffer = Marshal . StringToHGlobalUni ( measure . inputStr . ToLower ( ) ) ;
131+ return Rainmeter . StringBuffer . Update ( argv [ 0 ] . ToUpper ( ) ) ;
159132 }
160- return measure . buffer ;
133+
134+ return Rainmeter . StringBuffer . Update ( measure . inputStr . ToLower ( ) ) ;
161135 }
162136
163137 [ DllExport ]
164138 public static void Finalize ( IntPtr data )
165139 {
166140 Measure measure = ( Measure ) data ;
167- if ( measure . buffer != IntPtr . Zero )
168- {
169- Marshal . FreeHGlobal ( measure . buffer ) ;
170- }
171141 GCHandle . FromIntPtr ( data ) . Free ( ) ;
172142 }
173143 }
0 commit comments