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 PTC Community Badges. Engage with PTC and see how many you can earn! X

mathcad DLL cannot run twice with different parameter

KC_9978072
5-Regular Member

mathcad DLL cannot run twice with different parameter

Hi, I build a DLL function and run well in visual studio debug and got the result as i want.

But mathcad load into this dll and try to run this dll twice with different parameter.

As below pictures,

first, i load this dll twice with different parameter (INP_g_pow_acc) & (INP_g_pow_acc1), but got same result (this result is correspond to INP_g_pow_acc)

second, i exchange the parameter (INP_g_pow_acc & INP_g_pow_acc1), also got same result (this result is correspond to INP_g_pow_acc1)

Seem mathcad just load this dll once, how can mathcad run dll twice with different parameter??

Very thanks for your kindly support.  

 

 

圖片1.png圖片2.png 

ACCEPTED SOLUTION

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

In the top of your source code you (globally) define two arrays:

      float INP_g_pow_acc[224];

      float INP_g_theta_1[224];

The definition of your Mathcad function has parameters (which should behave as local variables within the function) with the same name:

     LPCCOMPLEXARRAY INP_g_pow_acc

     LPCCOMPLEXARRAY INP_g_theta_1

Now the compiler should be capable of discriminating between the global and the local variables with the same name. But, considering that these two parameters appear to be involved in what goes wrong in your double calling of the function from within Mathcad, I would comment out the global definition of the arrays, and see what happens.

 

I hope this solves your problem.

 

Other notes:

- The function MathcadArrayAllocate() is used to allocate storage for the result array Product. The function MathcadArrayFree() is called only when the function is interrupted by the user, and releases the memory set aside for the result (that isn't going to be delivered in that case). Both functions are used OK as far as I can tell.

- You have a lot of magic numbers (constants) in your code. What if you call the function from within Mathcad with as first argument an array of not 694, but 311, or 1250 elements. You could (should ?) make a lot of the constants dependent on the size(s) of the array(s) supplied to the function. You now only check that the arrays don't contain imaginary parts, but since you do not use any of that, this check can be omitted. If you supply the arrays with complex data, nothing of it is used, you should get the same result. If you stick with the fixed array sizes, it makes more sense to check that the arrays supplied to the function are as large as you expect them to be.

 

Success!
Luc

View solution in original post

10 REPLIES 10
KC_9978072
5-Regular Member
(To:KC_9978072)

I am a newer to use mathcad. could someone can support or give me a keyword or hint? 

it is confused me for a long time.

thanks you for your kindly support.

Hi,

The kronecker function is an example DLL in the Mathcad distribution.  See Help | Developer's Reference

Capture3.JPG

DLL can be found in the following directory.

Capture.JPG

Possible to use different parameters at each call.

Capture2.JPG

Check your DLL. The double use of one DLL is illustrated at the top of the enclosed file.

Cheers

Terry

Hi,

I get different results when you change the input parameter.

Capture.JPG

KC_9978072
5-Regular Member
(To:terryhendicott)

Hi, Terry, thank you for your comment.

Actually, i follow the instruction of folder "mathcad 15/userefi/microsoft/source/simple/multiply.c to build my dll function.

I also try multiply.dll in mathcad, yes, multiply.dll can be called twice with different parameter and got different result.

I use VS2019 to develop my dll function, and no error and my dll can build.

I load my dll function to mathcad (just once), the result is also correct.

But if i call my dll function twice, the secondly call will something wrong.

As you found the result is different with mine.

Yes, if i push "ctrl+F9", i will got the result as yours.

So, i am so confused to this result.

The origin C source code is used in embedded system. I try to use mathcad dll function to calculate before load into embedded system.

I am checking the c code now and rebuild the dll again.

But i debug this dll with VS2019 and load it into mathcad once. The result is correct.

So hope someone can offer the keyword or hint. (Why second DLL call can not running well?)

Very thanks for your kindly support.  

LucMeekes
23-Emerald III
(To:KC_9978072)

I get a message that the .dll file that is contained in the .ZIP file contains a virus or unsafe code....

 

Luc

KC_9978072
5-Regular Member
(To:LucMeekes)

Dear Terry, Luc and all guys,

Thanks for your kindly support. last time, i delete a parameter so the simulation result is wrong which Terry point.

I modify the file and describe my actions and my problem as below.

1). I prepare a DLL function in VS2019 and debug ok and generate the DLL.

2). I load this DLL into mathcad with parameter (INP_g_pow_acc) and result as figure 1. (just notice the test2 row[529] = -0.983 is correct)

3). I reopen this mathcad file and load this DLL again but with another parameter (INP_g_pow_acc1) and result as figure 2. (just notice the test4 row[529] =2.221 is also correct)

4). i reopen this mathcad file and load same DLL twice with different parameter (figure 3.) test2 with INP_g_pow_acc is -0.983 is ok, but test4 with INP_g_pow_acc1 also become -0.983 (should be 2.221, why??)

I also attached multiply.dll and work well even load twice with different parameter. i follow the mathcad instruction to build my DLL. I know the problem should be my DLL source code, but debug ok, the result which first run is also ok, i donot know how to improve my DLL souce code.

So have someone suffer same issue?? 

I re-compress the zip file, could you kindly to download and try again?

Due to dll file is refused to upload, i hardly to upload the dll file just include into zip file.

Very very thanks for your kindly support. Have a nice weekend. 

 

圖片1.png圖片2.png圖片3.png

 

 

 

 

 

 

 

LucMeekes
23-Emerald III
(To:KC_9978072)

The problem may be your .DLL.

I fear that your code leaves some data static, so that it gets reused upon a second call to the function.

(Did you properly use MathcadArrayAllocate and MathcadArrayFree?)

Can you post the .DLL source code...?

 

Luc

KC_9978072
5-Regular Member
(To:LucMeekes)

Dear Luc,

 

Thanks for your support.

Actually, i donot know how to use "MathcadArrayAllocate and MathcadArrayFree", i just follow the instruction from folder "mathcad 15/userefi/microsoft/source/simple/multiply.c.

Seems "MathcadArrayFree" donot work, so mathcad always catch first call data.

The attached file is the dll source code.

I just follow multiply.c structure, why multiply.dll can work but new dll cannot?? 

LucMeekes
23-Emerald III
(To:KC_9978072)

In the top of your source code you (globally) define two arrays:

      float INP_g_pow_acc[224];

      float INP_g_theta_1[224];

The definition of your Mathcad function has parameters (which should behave as local variables within the function) with the same name:

     LPCCOMPLEXARRAY INP_g_pow_acc

     LPCCOMPLEXARRAY INP_g_theta_1

Now the compiler should be capable of discriminating between the global and the local variables with the same name. But, considering that these two parameters appear to be involved in what goes wrong in your double calling of the function from within Mathcad, I would comment out the global definition of the arrays, and see what happens.

 

I hope this solves your problem.

 

Other notes:

- The function MathcadArrayAllocate() is used to allocate storage for the result array Product. The function MathcadArrayFree() is called only when the function is interrupted by the user, and releases the memory set aside for the result (that isn't going to be delivered in that case). Both functions are used OK as far as I can tell.

- You have a lot of magic numbers (constants) in your code. What if you call the function from within Mathcad with as first argument an array of not 694, but 311, or 1250 elements. You could (should ?) make a lot of the constants dependent on the size(s) of the array(s) supplied to the function. You now only check that the arrays don't contain imaginary parts, but since you do not use any of that, this check can be omitted. If you supply the arrays with complex data, nothing of it is used, you should get the same result. If you stick with the fixed array sizes, it makes more sense to check that the arrays supplied to the function are as large as you expect them to be.

 

Success!
Luc

KC_9978072
5-Regular Member
(To:LucMeekes)

Dear Luc,

Thanks for your kindly support.

Yes, the problem is my DLL source code.

I already fix the bug and solve this problem.

Thanks again for your help.

Best regard,

 

Announcements

Top Tags