I'm trying to call a simple dll function to measure a piece of text, to see if it will fit in a confined space without being clipped.
I have defined an external DLL in C# and have linked it into the PB source using external function.
In my first attempt, I used float arguments as the C# utility routine uses float, and linked the function using the PB datatype Real. This caused a run time crash with no discernable codes except the usual 'well, that didn't work!' type message from PB.
I altered the DLL code to use int instead and tried to relink it using the Long parameter type but it will not compile now.
I have tried int, long and longlong.
Error message
---------- Compiler: Errors (10:40:55)
cobs2.pbl(w_appmain).w_appmain.of_addbutton.46: Error C0116: Reference argument type does not match function definition: getwordwidth
---------- Finished Errors (10:40:55)
function definition in PB
FUNCTION long GetWordWidth(string asString, string asFont, long aiPointsize, ref long aiheight ) LIBRARY "flutils.dll"
To be certain about the conversion, I imported the DLL using the import function.
This is what was generated.
public function long of_getwordwidth(string as_asstring,string as_asfont,long al_aipointsize,ref long al_aiheight);
//*-----------------------------------------------------------------*/
//* .NET function : GetWordWidth
//* Argument:
//* String as_asstring
//* String as_asfont
//* Long al_aipointsize
//* Long al_aiheight
//* Return : Long
//*-----------------------------------------------------------------*/
Return This.getwordwidth(as_asstring,as_asfont,al_aipointsize,ref al_aiheight)
end function
Here is the C# source code. I have independently tested this with a console test harness and I know it works.
public int GetWordWidth(string asString, string asFont, int aiPointsize, ref int aiheight)
{
TextBox tx = new TextBox();
Graphics g = tx.CreateGraphics();
Font stringFont = new Font(asFont, aiPointsize);
SizeF wordbounds;
wordbounds = g.MeasureString(asString, stringFont);
aiheight = Convert.ToInt32(wordbounds.Height);
return Convert.ToInt32(wordbounds.Width);
}
What am I doing wrong?