LPTSTR nla_make_spn(const char* ServiceClass, const char* hostname)
{
	DWORD status = ERROR_INTERNAL_ERROR;
	DWORD SpnLength;
	LPTSTR hostnameX = NULL;
	LPTSTR ServiceClassX = NULL;
	LPTSTR ServicePrincipalName = NULL;
#ifdef UNICODE
	ConvertToUnicode(CP_UTF8, 0, hostname, -1, &hostnameX, 0);
	ConvertToUnicode(CP_UTF8, 0, ServiceClass, -1, &ServiceClassX, 0);
#else
	hostnameX = _strdup(hostname);
	ServiceClassX = _strdup(ServiceClass);
#endif

	if (!hostnameX || !ServiceClassX)
		goto fail;

	if (!ServiceClass)
	{
		ServicePrincipalName = (LPTSTR)_tcsdup(hostnameX);
		status = ERROR_SUCCESS;
		goto fail;
	}

	SpnLength = 0;
	status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, NULL);

	if (status != ERROR_BUFFER_OVERFLOW)
		goto fail;

	ServicePrincipalName = (LPTSTR)calloc(SpnLength, sizeof(TCHAR));

	if (!ServicePrincipalName)
		goto fail;

	status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, ServicePrincipalName);

	if (status != ERROR_SUCCESS)
		goto fail;

fail:
	if (status != ERROR_SUCCESS)
	{
		free(ServicePrincipalName);
		ServicePrincipalName = NULL;
	}
	free(ServiceClassX);
	free(hostnameX);
	return ServicePrincipalName;
}
LPTSTR nla_make_spn(const char* ServiceClass, const char* hostname)
{
	DWORD status = ERROR_INTERNAL_ERROR;
	DWORD SpnLength;
	LPTSTR hostnameX = NULL;
	LPTSTR ServiceClassX = NULL;
	LPTSTR ServicePrincipalName = NULL;
#ifdef UNICODE
	ConvertToUnicode(CP_UTF8, 0, hostname, -1, &hostnameX, 0);
	ConvertToUnicode(CP_UTF8, 0, ServiceClass, -1, &ServiceClassX, 0);
#else
	hostnameX = _strdup(hostname);
	ServiceClassX = _strdup(ServiceClass);
#endif

	if (!hostnameX || !ServiceClassX)
		goto fail;

	if (!ServiceClass)
	{
		ServicePrincipalName = (LPTSTR)_tcsdup(hostnameX);
		status = ERROR_SUCCESS;
		goto fail;
	}

	SpnLength = 0;
	status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, NULL);

	if (status != ERROR_BUFFER_OVERFLOW)
		goto fail;

	ServicePrincipalName = (LPTSTR)calloc(SpnLength, sizeof(TCHAR));

	if (!ServicePrincipalName)
		goto fail;

	status = DsMakeSpn(ServiceClassX, hostnameX, NULL, 0, NULL, &SpnLength, ServicePrincipalName);

	if (status != ERROR_SUCCESS)
		goto fail;

fail:
	if (status != ERROR_SUCCESS)
	{
		free(ServicePrincipalName);
		ServicePrincipalName = NULL;
	}
	free(ServiceClassX);
	free(hostnameX);
	return ServicePrincipalName;
}