// To build with Digital Mars C++ Compiler: // dmc -mn -WD -o testdll.cpp kernel32.lib // // To build with MSVC 2022+: // cl.exe /std:c++17 /EHsc /LD testdll.cpp /link /PDBSTRIPPED /out:testdll.dll #include extern "C" __declspec(dllexport) void (*Display)(const char *format, ...) = 0; extern "C" __declspec(dllexport) double (*EngAtof)(const char **string) = 0; union uData { bool b; char c; unsigned char uc; short s; unsigned short us; int i; unsigned int ui; float f; double d; long long int i64; unsigned long long int ui64; char *str; unsigned char *bytes; }; // int DllMain() must exist and return 1 for a process to load the .DLL // See https://docs.microsoft.com/en-us/windows/win32/dlls/dllmain for more // information. int __stdcall DllMain(void *module, unsigned int reason, void *reserved) { return 1; } // #undef pin names lest they collide with names in any header file(s) you might // include. #undef In #undef Out struct InstData { double X1Vals[3]; double fParms[3]; }; void parseFloatStr(const char *str, InstData &inst) { for (int i = 0; *str && i < sizeof(inst.X1Vals) / sizeof(inst.X1Vals[0]); i++) inst.X1Vals[i] = EngAtof(&str); } void getfParms(InstData &inst) { const char *fVals[] = {"f1", "f2", "f3"}; // must be lower case for (int i = 0; i < sizeof(fVals) / sizeof(fVals[0]); i++) { double val = EngAtof(fVals + i); inst.fParms[i] = val; } } extern "C" __declspec(dllexport) void testdll( InstData **opaque, double t, union uData *data) { double In = data[0].d; // input const char *X1Vals = data[1].str; // input parameter const char *X2Vals = data[2].str; // input parameter double &Out = data[3].d; // output InstData *inst = *opaque; if (!inst) { inst = *opaque = (InstData *)calloc(sizeof(InstData), 1); Display("X1Vals Attribute String From Schematic: \"%s\"\n", X1Vals); Display("X2Vals Attribute String From Schematic: \"%s\"\n", X2Vals); parseFloatStr(X1Vals, *inst); Display("Parsed Values From X1Vals Attribute String:\n"); Display("X1Vals[0]=%f, X1Vals[1]=%f, X1Vals[2]=%f\n", inst->X1Vals[0], inst->X1Vals[1], inst->X1Vals[2]); getfParms(*inst); Display("Param Values Obtained Directly From Schematic:\n"); Display("Params F1=%f, F2=%f, F3=%f\n", inst->fParms[0], inst->fParms[1], inst->fParms[2]); } Out = In * In; // do something } extern "C" __declspec(dllexport) void Destroy(InstData *inst) { free(inst); }