Files
wfassoc/wfassoc/wfassoc.c

85 lines
2.7 KiB
C

#include "wfassoc.h"
#include <strsafe.h>
// private function and variable declearions.
/// <summary>
/// Convert multi byte string to wide char string
/// Notice: this function will allocate memory for returns and it should be released safely.
/// </summary>
/// <param name="source">The string will be converted</param>
/// <param name="error">The error happend during converting</param>
/// <returns>The string has been converted. If return NULL, it mean that the converting failed.</returns>
WCHAR* ConvMultiByteToWideChar(CHAR* source);
#define LEGACY_PROGID_FORMATA "%s.%s.%d"
#define LEGACY_PROGID_FORMATW L"%s.%s.%d"
#define SAFE_FREE(obj) if(obj!=NULL)free(obj);
// public function implements.
WFERROR WFInstallApplicationW(WFAPP_PROFILEW* profile) {
if (profile->WFVersion != WFVERSION) return WFERROR_INVALID_VERSION;
return WFERROR_OK;
}
WFERROR WFInstallApplicationA(WFAPP_PROFILEA* profile) {
return WFERROR_OK;
}
WFERROR WFUninstallApplicationW(WFAPP_PROFILEW* profile) {
if (profile->WFVersion != WFVERSION) return WFERROR_INVALID_VERSION;
return WFERROR_OK;
}
WFERROR WFUninstallApplicationA(WFAPP_PROFILEA* profile) {
return WFERROR_OK;
}
WFERROR WFGenerateProgIDW(WCHAR* vendor, WCHAR* component, INT version, WCHAR* result, INT* result_length) {
if (result_length == NULL) return WFERROR_NULLPTR;
if (result == NULL) {
*result_length = _snwprintf(NULL, 0, LEGACY_PROGID_FORMATW, vendor, component, version) + 1;
} else {
int write_result = _snwprintf(result, *result_length, LEGACY_PROGID_FORMATW, vendor, component, version);
if (write_result < 0 || write_result >= *result_length) return WFERROR_INSUFFICIENT_BUFFER;
}
return WFERROR_OK;
}
WFERROR WFGenerateProgIDA(CHAR* vendor, CHAR* component, INT version, CHAR* result, INT* result_length) {
if (result_length == NULL) return WFERROR_NULLPTR;
if (result == NULL) {
*result_length = _snprintf(NULL, 0, LEGACY_PROGID_FORMATA, vendor, component, version) + 1;
} else {
int write_result = _snprintf(result, *result_length, LEGACY_PROGID_FORMATA, vendor, component, version);
if (write_result < 0 || write_result >= *result_length) return WFERROR_INSUFFICIENT_BUFFER;
}
return WFERROR_OK;
}
// private function and variable implements.
WCHAR* ConvMultiByteToWideChar(CHAR* source) {
WCHAR* dest = NULL;
size_t sourceLength = strlen(source);
int destLength = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, NULL, 0);
if (destLength <= 0) return NULL;
destLength += 10;
dest = (WCHAR*)malloc(sizeof(WCHAR) * destLength);
if (dest == NULL) return NULL;
memset(dest, 0, sizeof(WCHAR) * destLength);
int error = MultiByteToWideChar(CP_ACP, 0, source, sourceLength, dest, destLength);
if (error <= 0) {
free(dest);
return NULL;
}
return dest;
}