cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - Learn all about the Community Ranking System, a fun gamification element of the PTC Community. X

Prime 3.0 Custom Functions (string arguments)

spolak
1-Visitor

Prime 3.0 Custom Functions (string arguments)

I have a user function for Mathcad 15 that works great (in Mathcad 15). I'm attempting to build that .dll for Mathcad Prime 3.0. After compiling the code (linking the mcaduser.lib that is distributed with Mathcad Prime 3.0), all the custom functions defined in my .dll work fine in Mathcad Prime 3.0, except those which return a string type value. Any function which returns a string will crash Mathcad Prime 3.0 when the function is called. Even the most simple test cases, such as the following function:

LRESULT testFunction(LPMCSTRING ParamValue, LPCMCSTRING ParamName)

{

ParamValue->str = "foo";

return 0;

}

will crash Prime 3.0 when invoked in a worksheet.

Anyone else experience this issue? Thanks for the feedback!

Scott

ACCEPTED SOLUTION

Accepted Solutions
LucMeekes
23-Emerald III
(To:spolak)

Hi Scott,

I hope the failure is not due to missing a call to MathcadAllocate, to obtain storage for the string...

{Don't know if allocation for storage of strings and arrays is required in Prime as it is in Mathcad.}

Success!
Luc

View solution in original post

6 REPLIES 6
spolak
1-Visitor
(To:spolak)

By the way, the custom/user function I'm trying to build is a wrapper for CoolProp, which is an open-source library of thermophysical fluid properties, including humid air (water/steam). The user function I compiled for Mathcad 15 wraps the CoolProp library quite well. I've only implemented a few of the top level functions, but enough to sufficiently extract all properties for all the fluids in the CoolProp library. CoolProp is open-source, and so is the Mathcad wrapper for it. Here's a link to the Mathcad wrapper:

http://sourceforge.net/projects/coolprop/files/CoolProp/4.0.0beta/MathCAD/

Now I just need to figure out these "nuances" of Prime 3.0 that are preventing me from building the CoolProp wrapper...

Scott

RogerYeh
12-Amethyst
(To:spolak)

R&D has been informed of this issue. Thank you for bringing it to our attention!

LucMeekes
23-Emerald III
(To:spolak)

Hi Scott,

I hope the failure is not due to missing a call to MathcadAllocate, to obtain storage for the string...

{Don't know if allocation for storage of strings and arrays is required in Prime as it is in Mathcad.}

Success!
Luc

jsheehan
12-Amethyst
(To:LucMeekes)

Luc

You are correct, nice catch . Our QA/Engineering team suggests replacing the call to "new"

char

* c = MathcadAllocate(s.size()+1);

// creat a c-string (pointer) c with the same size as s

In your

GetCoolPropsParamMathcad

Function

This will protect your code across DLL boundaries.

John

Hi guys,

just to develop a little more this subject, this is what works for me:

// -----------------------------------------------

// File: test_string_return.cpp

// test for a function that returns a string

#include "stdafx.h"

#include "mcadincl.h"

#include <string>

using namespace std;

LRESULT test_string_return_Function( LPMCSTRING ParamValue, LPCMCSTRING ParamName );

LRESULT test_string_return_Function( LPMCSTRING ParamValue, LPCMCSTRING ParamName )

{

// see: http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char

string str_val = "foox";

char *cstr_val = MathcadAllocate(str_val.size() + 1);

std::copy(str_val.begin(), str_val.end(), cstr_val);

cstr_val[str_val.size()] = '\0';

ParamValue->str = cstr_val;

// normal return

return 0;

}

// fill out a FUNCTIONINFO structure with

// the information needed for registering the

// function with Mathcad

FUNCTIONINFO test_string_return =

{

// Name by which mathcad will recognize the function

"test_string_return",

// description of "test_string_return_Function" parameters to be used

// by the Insert Function dialog box

"string-value",

// description of the function for the Insert Function dialog box

"test: pass a string and receive a string as output",

// pointer to the executible code

(LPCFUNCTION)test_string_return_Function,

// type of returned object

STRING,

// number of input parameters (not including the returned object)

// test_string_return_Function( value )

1,

// types of input parameters

{STRING}

};

// -----------------------------------------------

This is needed, for example, when one wants to read the attribute of a dataset in an HDF file. In that case the string, which is the attribute value, has to be returned to Mathcad.

Agostino

Luc, thanks for the tip - you were absolutely correct. I've only ever used the MathcadAllocate functions for array objects, not for strings. It makes perfect sense though. Thanks again!

Scott

Announcements

Top Tags