extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
LPWSTR wcSerialNumber = new wchar_t[newsize];
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber, newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber,
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
delete[] wcSerialNumber;
}
return returnValue;
}
To change this code to use smart pointers. My code look like this
extern "C" int __stdcall calcA(
LPCSTR SerialNumber,
double Diameter,
int Design,
int FoilSpacing,
double RotorSpeed,
double Altitude,
double SupplyAirInletTemperature,
double SupplyAirInletMoistureContent,
double ExhaustAirInletTemperature,
double ExhaustAirInletMoistureContent,
double SupplyAirOutletFlow,
double ExhaustAirInletFlow,
double PressureDifference,
bool PurgingSector,
double* PowerTotal,
double* PowerSensibel,
double* TemperatureEfficiency,
double* HumidityEfficiency,
double* SupplyAirOutletTemperature,
double* SupplyAirOutletMoistureContent,
double* ExhaustAirOutletTemperature,
double* ExhaustAirOutletMoistureContent,
double* SupplyAirOutletVelocity,
double* ExhaustAirInletVelocity,
double* SupplyAirSidePressureDrop,
double* ExhaustAirSidePressureDrop,
double* HumidifyingDehumidifying,
double* ExcessWater,
double* ExhaustAirTransferRatio,
double* OutdoorAirCorrectionFactor,
bool* Frostrisk,
double* CarryOverAirFlow)
{
int returnValue = 0;
if (NULL == SerialNumber)
{
returnValue = NULL_POINTER;
}
else
{
size_t newsize = strlen(SerialNumber) + 1; /* Including NULL */
auto wcSerialNumber = make_unique<wchar_t>(newsize);
size_t convertedChars = 0;
if ((0 != mbstowcs_s(&convertedChars, wcSerialNumber.get(), newsize, SerialNumber, _TRUNCATE)) || (convertedChars != newsize))
{
returnValue = INVALID_STRING_CONVERSION;
}
if (0 == returnValue)
{
returnValue = calcW(wcSerialNumber.get(),
Diameter,
Design,
FoilSpacing,
RotorSpeed,
Altitude,
SupplyAirInletTemperature,
SupplyAirInletMoistureContent,
ExhaustAirInletTemperature,
ExhaustAirInletMoistureContent,
SupplyAirOutletFlow,
ExhaustAirInletFlow,
PressureDifference,
PurgingSector,
PowerTotal,
PowerSensibel,
TemperatureEfficiency,
HumidityEfficiency,
SupplyAirOutletTemperature,
SupplyAirOutletMoistureContent,
ExhaustAirOutletTemperature,
ExhaustAirOutletMoistureContent,
SupplyAirOutletVelocity,
ExhaustAirInletVelocity,
SupplyAirSidePressureDrop,
ExhaustAirSidePressureDrop,
HumidifyingDehumidifying,
ExcessWater,
ExhaustAirTransferRatio,
OutdoorAirCorrectionFactor,
Frostrisk,
CarryOverAirFlow);
}
}
return returnValue;
}
The code compile but at run time i get _CrtIsValidHeapPointer(pUserData). Have a totally misunderstood smart pointers. The program is getting a heap error....
Those two lines do very different things. The first dynamically allocates an array of
wchar_t
s with a number of elements equal tonewsize
. The second line dynamically allocates a singlewchar_t
, calling the constructor withnewsize
as an argument.std::unique_ptr
is not the best option in this case. Dynamic arrays are better replaced withstd::vector
. In your case, this might look like:Then instead of calling
wcSerialNumber.get()
, callwcSerialNumber.data()
.