std::string getenv(const char *var) { size_t bufSize; std::wstring wvar = boost::filesystem::path(var, qi::unicodeFacet()).wstring(qi::unicodeFacet()); #ifdef _MSC_VER wchar_t *envDir = NULL; _wdupenv_s(&envDir, &bufSize, wvar.c_str()); if (envDir == NULL) return ""; boost::filesystem::path dest(envDir, qi::unicodeFacet()); std::string ret(dest.string(qi::unicodeFacet()).c_str()); free(envDir); return ret; #else _wgetenv_s(&bufSize, NULL, 0, wvar.c_str()); wchar_t *envDir = (wchar_t *) malloc(bufSize * sizeof(wchar_t)); _wgetenv_s(&bufSize, envDir, bufSize, wvar.c_str()); if (envDir == NULL) return ""; boost::filesystem::path dest(envDir, qi::unicodeFacet()); std::string ret(dest.string(qi::unicodeFacet()).c_str()); free(envDir); return ret; #endif }
std::wstring getAppDataFolderPath() { std::wstring str = L""; wchar_t* buf; size_t size; _wdupenv_s(&buf, &size, L"APPDATA"); str += buf; str += L"\\";// TODO str += s_appDataFolderBasename; return str; }
bool GetEnvVar(_In_z_ wchar_t *var, MyString &res) { wchar_t *buffer; size_t size; _wdupenv_s(&buffer, &size, var); if (!size || !buffer) return false; res = buffer; free(buffer); // Don't forget to free the buffer! return true; }
static PyObject * winutil_getenv(PyObject *self, PyObject *args) { const Py_UNICODE *q; if (!PyArg_ParseTuple(args, "u", &q)) return NULL; wchar_t *buf = NULL; size_t sz = 0; PyObject *ans = NULL; if (_wdupenv_s(&buf, &sz, q) != 0 || buf == NULL || sz == 0) { ans = Py_None; Py_INCREF(ans); } else ans = PyUnicode_FromWideChar(buf, sz - 1); if (buf) free(buf); return ans; }
wchar_t *find_on_path(wchar_t *name) { wchar_t *pathext; size_t varsize; wchar_t *context = NULL; wchar_t *extension; wchar_t *result = NULL; DWORD len; errno_t rc; /* Return value storage - we don't need to be re-entrant */ static wchar_t path_buf[MAX_PATH]; if (wcschr(name, L'.') != NULL) { /* assume it has an extension. */ len = SearchPathW(NULL, name, NULL, MAX_PATH, path_buf, NULL); if (len) { result = path_buf; } } else { /* No extension - search using registered extensions. */ rc = _wdupenv_s(&pathext, &varsize, L"PATHEXT"); if (rc == 0) { extension = wcstok_s(pathext, L";", &context); while (extension) { len = SearchPathW(NULL, name, extension, MAX_PATH, path_buf, NULL); if (len) { result = path_buf; break; } extension = wcstok_s(NULL, L";", &context); } free(pathext); } } if (result) { /* We just want the directory */ wchar_t *end = wcsrchr(result, L'\\'); *end = L'\0'; } return result; }
void IntelFaceScanner::renderScan() { // Setup the output path to a writable location. size_t unused; WCHAR* pUserProfilePath; _wdupenv_s(&pUserProfilePath, &unused, L"USERPROFILE"); const PXC3DScan::FileFormat format = PXC3DScan::PLY; // OBJ, PLY or STL const pxcCHAR* ext = PXC3DScan::FileFormatToString(format); const size_t FSIZE = 4096; WCHAR filename[FSIZE]; //swprintf_s(filename, FSIZE, L"%s\\Documents\\Pierre3dscan.%s", pUserProfilePath, ext); swprintf_s(filename, FSIZE, L".\\data\\3dscan.%s", ext); // If applicable, reconstruct the 3D Mesh to the specific file/format pxcStatus result = PXC_STATUS_NO_ERROR; bool bMeshSaved = false; if (scanner->IsScanning()) { wprintf_s(L"Generating %s...", filename); result = scanner->Reconstruct(format, filename); if (result >= PXC_STATUS_NO_ERROR) { bMeshSaved = true; wprintf_s(L"done.\n"); } else if (result == PXC_STATUS_FILE_WRITE_FAILED) { wprintf_s(L"the file could not be created using the provided path. Aborting.\n"); } else if (result == PXC_STATUS_ITEM_UNAVAILABLE || result == PXC_STATUS_DATA_UNAVAILABLE) { wprintf_s(L"no scan data found. Aborting.\n"); } else if (result < PXC_STATUS_NO_ERROR) { wprintf_s(L"\nError: Reconstruct returned %d\n", result); } } else wprintf_s(L"Empty scanning volume.\n"); }
wstring TargetPage::getEnvironmentVariable(const wchar_t *name) { wchar_t *buffer; size_t length; wstring value; if (_wdupenv_s(&buffer,&length,name) == 0) { if (length > 0) { value=wstring(buffer); free(buffer); return(value); } } return(value); }
std::wstring getFullPathUnicode(FileLocalization fileLocalization, const char* filepath) { std::wstring str; #if defined(USES_WINDOWS8_DESKTOP) || defined(USES_WINDOWS_OPENGL) if (fileLocalization == APPLICATION_FOLDER) { str += Utils::getCurrentDirectoryUnicode(); } else if (fileLocalization == APPLICATION_DATA_FOLDER) { wchar_t* buf; size_t size; errno_t res = _wdupenv_s(&buf, &size, L"APPDATA"); Assert(res == 0 && buf != NULL); str += buf; str += L"\\";// TODO str += s_appDataFolderBasename; struct _stat st; if (_wstat(str.c_str(), &st) != 0) // if file does not exist { _wmkdir(str.c_str()); } free(buf); } else { Assert(false);// TODO } #elif defined(USES_WINDOWS8_METRO) str = getStorageFolder(fileLocalization)->Path->Data(); #elif defined(USES_LINUX) #error todo #endif str += L"\\"; str += Utils::convertStringToWString(filepath); return str; }
mmString mmOperatingSystem::GetUniqueIDString(void) { mmString v_sOutIDString; // Hard Drive ID DWORD v_sSN,v_sDW1,v_sDW2; wchar_t v_cS1[256]; wchar_t v_cS2[256]; wchar_t * v_pcWinDir = NULL; std::size_t v_iWinDirSize = 0; _wdupenv_s(&v_pcWinDir, &v_iWinDirSize, L"windir"); mmString v_sDrive = mmString(v_pcWinDir).substr(0,3); GetVolumeInformation(v_sDrive.c_str(), v_cS1, 256, &v_sSN, &v_sDW1, &v_sDW2, v_cS2, 256); free(v_pcWinDir); wchar_t v_cHDD[256]; swprintf_s(v_cHDD,L"%d",v_sSN); mmString v_sHDD = v_cHDD; // Program tick count std::clock_t v_sCurrentClock = v_sCurrentClock = clock(); wchar_t v_cCLK[256]; swprintf_s(v_cCLK,L"%d",v_sCurrentClock); mmString v_sCLK = v_cCLK; // date time mmString v_sDateTime = mmStringUtilities::GetCurrentDateTimeString(); v_sOutIDString = v_sHDD + v_sCLK + mmString(L"_") + v_sDateTime; return v_sOutIDString; }
int _tmain(int argc, _TCHAR* argv[]) { TCHAR szPath[MAX_PATH] = { 0, }; DWORD dwSize = GetModuleFileName(NULL, szPath, MAX_PATH); TCHAR * pdest = _tcsrchr(szPath, '\\'); _tcsncpy_s(gServicePath, sizeof(gServicePath) / sizeof(TCHAR), szPath, (size_t)(pdest - szPath)); if (argc < 2) { ExecuteSubProcess(); return 0; } if (_tcsicmp(L"/i", argv[1]) == 0) return Install(szPath, ServiceName); else if (_tcsicmp(L"/k", argv[1]) == 0) return KillService(ServiceName); else if (_tcsicmp(L"/u", argv[1]) == 0) return UnInstall(ServiceName); else if (_tcsicmp(L"/s", argv[1]) == 0) return RunService(ServiceName); else if (_tcsicmp(L"/t", argv[1]) == 0) { DWORD dwPID; WCHAR *szServicePath; WCHAR *cmd = L"drbdadm.exe initial-split-brain minor-6"; WCHAR fullName[MAX_PATH] = {0}; size_t len; errno_t err = _wdupenv_s(&szServicePath, &len, L"DRBD_PATH"); if (err) { // default szServicePath = L"C:\\Program Files\\drbd\\bin"; } if ((wcslen(szServicePath) + wcslen(cmd) + 4) > MAX_PATH) { printf("szServicePath: too big!!\n"); } wcsncpy_s(fullName, szServicePath, wcslen(szServicePath)); wcscat_s(fullName, L"\\"); wcsncat_s(fullName, cmd, wcslen(cmd)); //wnsprintf printf("fullName: %ws\n", fullName); // test! DWORD ret = RunProcess(EXEC_MODE_WIN, SW_NORMAL, NULL, cmd, szServicePath, dwPID, 0, NULL, NULL); free(szServicePath); return ERROR_SUCCESS; } #if 1 // _WIN32_HANDLER_TIMEOUT: test by a separate application, not daemon. remove later else if (_tcsicmp(L"/n", argv[1]) == 0) { // internal test only: no-daemon test unsigned short servPort = DRBD_DAEMON_TCP_PORT; DWORD threadID; if (CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) SockListener, &servPort, 0, (LPDWORD) &threadID) == NULL) { WriteLog(L"pthread_create() failed\n"); return 0; } int i = 0; while (1) { printf("test main loop(%d)...\n", i++); Sleep(10000); } } #endif else { TCHAR msg[256]; _stprintf_s(msg, _T("Usage: drbdService.exe [/i|/k|/u|/s]\n")); WriteLog(msg); return ERROR_INVALID_PARAMETER; } return ERROR_SUCCESS; }