[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TRNSYS-users] C++ API not working



FYI

Finally I found a way to call the getDeckFileName() function  from C/C++ directly.
See example below.

TRNSYS.h
extern "C" __declspec(dllimport) int    _cdecl TRNSYSFUNCTIONS_mp_GETMAXPATHLENGTH(void);
extern "C" __declspec(dllimport) char*    _cdecl TRNSYSFUNCTIONS_mp_GETDECKFILENAME(char* dck, size_t len);
#define getMaxPathLength                TRNSYSFUNCTIONS_mp_GETMAXPATHLENGTH 
#define getDeckFileName                TRNSYSFUNCTIONS_mp_GETDECKFILENAME

Example
std::string trim(const std::string& str)
{
    size_t first = str.find_first_not_of(' ');
    if (std::string::npos == first)
    {
        return str;
    }
    size_t last = str.find_last_not_of(' ');
    return str.substr(first, (last - first + 1));
}

size_t maxlen = getMaxPathLength();
char *fname= new char[maxlen];
getDeckFileName(fname, maxlen);
std::string deckFileName = trim(std::string(fname,0,maxlen)); // trimming the string, just in case
delete[] fname;

cheers,

Yuichi

On 2016/05/26 14:17, Yuichi Yasuda via TRNSYS-users wrote:
Michael,

I assume you are using TRNSYS16 coding style in your component. I'm not sure that the getMaxLabelLength() function works with the coding style.However, the function works fine in my TRNSYS'17' coding style component.

The getDeckFileName() seems to me that the function does not care about C/C++ strings. I have got exactly same error.
Unless, you modified the function to cope with C/C++ style, I think that you can not avoid the access violation error.
For example, I have added the following new function in the TrnsysFunction.f90 to avoid the error.

-TrnsysFunction.f90

Function getDeckFileNameEx() bind(c,name="getDeckFileNameEx")
 !dec$ attributes dllexport :: getDeckFileNameEx
 Use TrnsysData
 Use, intrinsic :: iso_c_binding
 type(C_ptr) :: getDeckFileNameEx
 Character(len=maxPathLength) ret 
 ret = trim(deckn1)//CHAR(0)
 getDeckFileNameEx = C_LOC(ret)
End Function getDeckFileNameEx

- C/C++ Component sourcecode
extern "C" __declspec(dllimport) char*    _cdecl getDeckFileNameEx(void);

      char *dckfilename;
      dckfilename = getDeckFileNameEx();

I do not like this idea because an expensive Fortran compiler is needed to build.
Does anyone know that how to call FORTRAN function returns characters?

Best regards,

Yuichi Yasuda
quattro corporate design co.,ltd
T2000 Bld. 4F, 1-11-29 Mita, Meguro-ku, Tokyo, 153-0062, Japan
Tel:+81 (0)3 5768 6744 Fax:+81 (0)3 5768 6745
Email: yasuda@qcd.co.jp
http://www.qcd.co.jp/



 


On 2016/05/21 0:22, Michael Holzhauser via TRNSYS-users wrote:
Hello all,

I managed to build a customized TRNSYS Type in C++ by extending the Type202 example that is delivered with TRNSYS 17. I can access the input values and return the corresponding output values without any problems. However, the communication with the TRNSYS Core functions somehow don’t work. Whenever I invoke some function that returns an integer value (like getMaxLabelLength()), the result is NaN. Whenever I try to invoke some function that returns characters (like getDeckFileName()), TRNSYS crashes with something like „Access violation at address XXXXX in module ‚TRNDLL.DLL‘. Write of address XXXXX.“ or with „forttrl: severe (408): fort: (18): Dummy character variable ‚GETLABEL@0‘ has length 300 which is greater than actual variable length 1“ (in case of getLabel).

As I read in other topics like this one, there seemed to be several issues in the TRNSYS.h header file and the library files long ago: 

However, 11 years later, is there a workaround for this problem?

Thanks in advance.

--

M.Sc. Michael Holzhauser
AG Optimization
University of Kaiserslautern

Paul-Ehrlich-Str. 14 - 451
67663 Kaiserslautern, Germany

Phone:  +49 (631) 205-2511
E-mail: holzhauser@mathematik.uni-kl.de




_______________________________________________
TRNSYS-users mailing list
TRNSYS-users@lists.onebuilding.org
http://lists.onebuilding.org/listinfo.cgi/trnsys-users-onebuilding.org




_______________________________________________
TRNSYS-users mailing list
TRNSYS-users@lists.onebuilding.org
http://lists.onebuilding.org/listinfo.cgi/trnsys-users-onebuilding.org


--

株式会社クアトロ
http://qcd.co.jp/
安田 雄市
TEL 03-5768-6744
FAX 03-5768-6745
e-mail: yasuda@qcd.co.jp