| /* cedll |
| A part from the Windows CE C Extra Library (celibc) by PC GO |
| Copyright 2007-2015 PC GO Ld. |
| |
| This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
| */ |
| |
| #include <windows.h> |
| #include <limits.h> |
| #include <string.h> |
| #include <wchar.h> |
| |
| int GetComputerName(char *buffer, unsigned long int *size) { |
| HKEY key; |
| unsigned long int DataSize = 4096; |
| if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Ident", 0, 0, &key) != ERROR_SUCCESS) return 0; |
| wchar_t wbuffer[PATH_MAX]; |
| if(RegQueryValueEx(key, L"Name", NULL, NULL, (unsigned char *)wbuffer, &DataSize) != ERROR_SUCCESS) { |
| RegCloseKey(key); |
| return 0; |
| } |
| wbuffer[PATH_MAX] = 0; |
| RegCloseKey(key); |
| unsigned long int len = wcslen(wbuffer) * 2; |
| if(*size < len) { |
| *size = len; |
| SetLastError(ERROR_BUFFER_OVERFLOW); |
| return 0; |
| } |
| *size = len; |
| return (int)wcstombs(buffer, wbuffer, len) >= 0; |
| } |
| |
| char *CeGetApplicationPath() { |
| wchar_t wname[PATH_MAX]; |
| size_t len = GetModuleFileNameW(NULL, wname, sizeof(wname)/sizeof(wname[0])) * 2; |
| if(len > PATH_MAX + 1) len = PATH_MAX + 1; |
| char name[len]; |
| wcstombs(memset(name, 0, len), wname, len); |
| int i = len - 2, s = 0; |
| char *path = malloc((len - 1) * sizeof(char)); |
| for(; i>=0; i--) { |
| if(name[i] == '/' || name[i] == '\\') { |
| if(!s) path[i+1] = 0; |
| s = 1; |
| name[i] = '/'; |
| } |
| if(s) path[i] = name[i]; |
| } |
| return path; |
| } |
| |
| char *RelativePathToAbsolutePath(char *path) { |
| if(!path) return NULL; |
| if(path[0] == '/' || path[0] == '\\') return path; |
| char *appath = CeGetApplicationPath(); |
| size_t len = strlen(appath); |
| memmove(path + len, path, strlen(path) + 1); |
| strncpy(path, appath, len); |
| free(appath); |
| return path; |
| } |
| |
| char *PathToAbsolutePath(char *path) { |
| return RelativePathToAbsolutePath(path); |
| } |