| #include <string.h> |
| #include <limits.h> |
| #include <windows.h> |
| |
| int GetComputerName(char *buffer, unsigned long int *size) { |
| HKEY hKey; |
| unsigned long int DataSize = 4096; |
| if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Ident", 0, 0, &hKey) != ERROR_SUCCESS) return 0; |
| wchar_t wbuffer[PATH_MAX]; |
| if(RegQueryValueEx(hKey, L"Name", NULL, NULL, (unsigned char *)wbuffer, &DataSize) != ERROR_SUCCESS) { |
| RegCloseKey(hKey); |
| return 0; |
| } |
| wbuffer[PATH_MAX] = 0; |
| RegCloseKey(hKey); |
| unsigned long int len = wcslen(wbuffer) * 2; |
| if(*size < len) { |
| *size = len; |
| SetLastError(ERROR_BUFFER_OVERFLOW); |
| return 0; |
| } |
| *size = len; |
| //char *r = malloc(len * sizeof(char)); |
| wcstombs(buffer, wbuffer, len); |
| return 1; |
| } |
| |
| char *CeGetApplicationPath() { |
| wchar_t wname[PATH_MAX]; |
| /*int r = */GetModuleFileNameW(NULL, wname, sizeof(wname)/sizeof(wname[0])); |
| size_t len = wcslen(wname) * 2 - 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 *PathToAbsolutePath(char *path) { |
| if(path == NULL) 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; |
| } |