I try to fetch some system env in windows from my C++ application. I tried getenv and GetEnvironmentVariable but both stuck. A program compiles but when I run it I see blinking pointer for some time, nothing displays and then program crash with message:
RUN FAILED (exit value -1 073 741 819, total time: 10s)
I tried a lot of examples from the net and all of them give the same result. Some examples I tried:
char l_strSingleVal[20];
GetEnvironmentVariable("PATH", l_strSingleVal,20);
printf("VariableName: %s\n",l_strSingleVal);
or:
std::string string_variable;
const std::string MY_VAR = "PATH";
char const* temp = std::getenv(MY_VAR.c_str());
if(temp != NULL)
{
string_variable = std::string(temp);
}
You have undefined behviour:
From GetEnvironmentVariable spec (l_strSingleVal is equivalent to lpBuffer): http://msdn.microsoft.com/en-us/library/windows/desktop/ms683188(v=vs.85).aspx
If lpBuffer is not large enough to hold the data, the return value is
the buffer size, in characters, required to hold the string and its
terminating null character and the contents of lpBuffer are undefined.
Accessing lpBuffer in your case is UB. A 20 character buffer for PATH is way too small. You need to check the return value of GetEnvironmentVariable (which in your case will be telling you the size of the buffer required for successful invocation).
Tried C standard library function getenv? It works for me on my Windows PC.
Example:
#include <stdlib.h>
#include <stdio.h>
int main()
{
char * src = getenv("PATH");
if (src)
printf("value of PATH is: %s", src);
else
printf("empty or not defined");
return 0;
}
Related
The code below demonstrates how stat and GetFileAttributes fail when the path contains some strange (but valid) ASCII characters.
As a workaround, I would use the 8.3 DOS file name. But this does not work when the drive has 8.3 names disabled.
(8.3 names are disabled with the fsutil command: fsutil behavior set disable8dot3 1).
Is it possible to get stat and/or GetFileAttributes to work in this case?
If not, is there another way of determining whether or not a path is a directory or file?
#include "stdafx.h"
#include <sys/stat.h>
#include <string>
#include <Windows.h>
#include <atlpath.h>
std::wstring s2ws(const std::string& s)
{
int len;
int slength = (int)s.length() + 1;
len = MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, 0, 0);
wchar_t* buf = new wchar_t[len];
MultiByteToWideChar(CP_ACP, 0, s.c_str(), slength, buf, len);
std::wstring r(buf);
delete[] buf;
return r;
}
// The final characters in the path below are 0xc3 (Ã) and 0x3f (?).
// Create a test directory with the name à and set TEST_DIR below to your test directory.
const char* TEST_DIR = "D:\\tmp\\VisualStudio\\TestProject\\ConsoleApplication1\\test_data\\Ã";
int main()
{
std::string testDir = TEST_DIR;
// test stat and _wstat
struct stat st;
const auto statSucceeded = stat(testDir.c_str(), &st) == 0;
if (!statSucceeded)
{
printf("stat failed\n");
}
std::wstring testDirW = s2ws(testDir);
struct _stat64i32 stW;
const auto statSucceededW = _wstat(testDirW.data(), &stW) == 0;
if (!statSucceededW)
{
printf("_wstat failed\n");
}
// test PathIsDirectory
const auto isDir = PathIsDirectory(testDirW.c_str()) != 0;
if (!isDir)
{
printf("PathIsDirectory failed\n");
}
// test GetFileAttributes
const auto fileAttributes = ::GetFileAttributes(testDirW.c_str());
const auto getFileAttributesWSucceeded = fileAttributes != INVALID_FILE_ATTRIBUTES;
if (!getFileAttributesWSucceeded)
{
printf("GetFileAttributes failed\n");
}
return 0;
}
The problem you have encountered comes from using the MultiByteToWideChar function. Using CP_ACP can default to a code page that does not support some characters. If you change the default system code page to UTF8, your code will work. Since you cannot tell your clients what code page to use, you can use a third party library such as International Components for Unicode to convert from the host code page to UTF16.
I ran your code using console code page 65001 and VS2015 and your code worked as written. I also added positive printfs to verify that it did work.
Don't start with a narrow string literal and try to convert it, start with a wide string literal - one that represents the actual filename. You can use hexadecimal escape sequences to avoid any dependency on the encoding of the source code.
If the actual code doesn't use string literals, the best resolution depends on the situation; for example, if the file name is being read from a file, you need to make sure that you know what encoding the file is in and perform the conversion accordingly.
If the actual code reads the filename from the command line arguments, you can use wmain() instead of main() to get the arguments as wide strings.
I've been racking my brain over this for hours and I can't find anything.
Potentially relevant information:
Running on OSX 10.10.1 Yosemite
This same code works perfectly fine on Windows.
Every time I run this, it breaks at the exact same spot.
The application is an OpenGL app that uses glfw3 to create a window.
There are no threads, it's just a single threaded app, so the pointer is not being overwritten or being deallocated.
These two methods are contained in two separate .c files that are compiled as c++ and contained within a built library that I link to. Other methods in the library work just fine.
OPchar* OPstreamReadLine(OPstream* stream) {
OPchar buffer[500];
i32 len, i;
// ALL WORKS FINE
// check to see if we are at the end of the stream or not
if(stream->_pointer >= stream->Length) return 0;
// Prints out the contents of the stream, and the start of the pointer just fine
OPlog("Buffer %s | Pointer %d", stream->Data, stream->_pointer);
sscanf((OPchar*)stream->Data stream->_pointer, "%500[^\n]", buffer);
len = strlen(buffer);
stream->_pointer = len 1;
// Spits out 'Read Hello of len 5'
OPlog("Read %s of len %d", buffer, len);
// ISSUE STARTS HERE
// OPchar is a typedef of char
// STEP 1. Make the call
OPchar* result = OPstringCreateCopy(buffer);
// STEP 6. The Pointer is printed out correctly, its the same thing
// ex: Pos: 0xd374b4
OPlog("Pos: 0x%x", result);
// STEP 7. This is where it breaks
// EXC_BAD_ACCESS and KERN_INVALID_ADDRESS
// What happened?
// Did returning the pointer from the function break it?
OPlog("len: %d", strlen(result));
OPlog("Result %s", result);
return result;
}
OPchar* OPstringCreateCopy(const OPchar* str) {
i32 len = strlen(str);
// STEP 2. Prints out 'Hello 5'
OPlog("%s %d", str, len);
// Allocates it (just uses malloc)
OPchar* result = (OPchar*)OPalloc(sizeof(OPchar) * (len + 1));
// Copies the previous string into the newly created one
strcpy(result, str);
// Ensures that it's null terminated
// even though strcpy is supposed to do it
result[len] = NULL;
// STEP 3. Gives a good pointer value
// ex: Pos: 0xd374b4
OPlog("Pos: 0x%x", result);
// STEP 4. Prints out '5'
OPlog("len: %d", strlen(result));
// STEP 5. Prints out 'Hello'
OPlog("hmmm: %s", result);
// Just return this same pointer
return result;
}
I've since replaced these functions with versions that don't use the sscanf stuff which got around the issue, however I'm now hitting the same problem with another returned pointer becoming invalid. This example was simpler to explain, so I thought I'd start there.
Here's a theory, which you may go test. Instead of using %x to print your pointers, use %p instead. You may be on a 64-bit OS and not realizing it. The problem could be that you did not supply a prototype for OPstringCreateCopy, in which case the return value was treated as an int (32 bits) instead of a pointer (64 bits). Since you are only printing out 32 bits of result, it seems like the pointer is valid, but the upper 32 bits may have been lost.
The fix for this is to make sure you always supply prototypes for all your functions. There should be some compiler warnings that you can turn on to assist you with finding uses of unprototyped functions. You might also want to go through your code and check for any other 64-bit problems, such as if you ever cast a pointer to an int.
I'm trying to execute a program on a file using the popen() command on a Mac. For this, I create a command of the form <path-to_executable> <path-to-file> and then call popen() on this command. Right now, both these two components are declared in a char*. I need to read the output of the command so I need the pipe given by popen().
Now it turns out that path-to-file can contain Chinese, Japanese, Russian and pretty much any other characters. For this, I can represent the path-to-file as wchar_t*. But this doesn't work with popen() because apparently Mac / Linux don't have a wide _wpopen() like Windows.
Is there any other way I can make this work? I'm getting the path-to-file from a data structure that can only give me wchar_t* so I have to take it from there and convert it appropriately, if needed.
Thanks in advance.
Edit:
Seems like one of those days when you just end up pulling your hair out.
So I tried using wcstombs, but the setlocale call failed for "C.UTF-8" and any of its permutations. Unsurprisingly, the wcstombs call failed returning -1 after that.
Then I tried to write my own iconv implementation based on some sample codes searched on Google. I came up with this, which stubbornly refuses to work:
iconv_t cd = iconv_open("UTF-8", "WCHAR_T");
// error checking here
wchar_t* inbuf = ...; // get wchar_t* here
char outbuf[<size-of-inbuf>*4+1];
size_t inlen = <size-of-inbuf>;
size_t outlen = <size-of-inbuf>*4+1;
char* c_inbuf = (char*) inbuf;
char* c_outbuf = outbuf;
int ret = iconv(cd, &c_inbuf, &inlen, &c_outbuf, &outlen);
// more error checking here
iconv always returns -1 and the errno is set to EINVAL. I've verified that <size-of-len> is set correctly. I've got no clue why this code's failing now.
Edit 2:
iconv was failing because I was not setting the input buffer length right. Also, Mac doesn't seem to support the "WCHAR_T" encoding so I've changed it to UTF-16. Now I've corrected the length and changed the from encoding but iconv just returns without converting any character. It just returns 0.
To debug this issue, I even changed the input string to a temp string and set the input length appropriately. Even this iconv call just returns 0. My code now looks like:
iconv_t cd = iconv_open("UTF-8", "UTF-16");
// error checking here
wchar_t* inbuf = ...; // get wchar_t* here - guaranteed to be UTF-16
char outbuf[<size-of-inbuf>*4+1];
size_t inlen = <size-of-inbuf>;
size_t outlen = <size-of-inbuf>*4+1;
char* c_inbuf = "abc"; // (char*) inbuf;
inlen = 4;
char* c_outbuf = outbuf;
int ret = iconv(cd, &c_inbuf, &inlen, &c_outbuf, &outlen);
// more error checking here
I've confirmed that the converter descriptor is being opened correctly. The from-encoding is correct. The input buffer contains a few simple characters. Everything is hardcoded and still, iconv doesn't convert any characters and just returns 0 and outbuf remains empty.
Sanity loss alert!
You'll need an UTF-8 string for popen. For this, you can use iconv to convert between different encodings, including from the local wchar_t encoding to UTF-8. (Note that on my Mac OS install, wchar_t is actually 32 bits, and not 16.)
EDIT Here's an example that works on OS X Lion. I did not have problems using the wchar_t encoding (and it is documented in the iconv man page).
#include <sys/param.h>
#include <string.h>
#include <iconv.h>
#include <stdio.h>
#include <errno.h>
char* utf8path(const wchar_t* wchar, size_t utf32_bytes)
{
char result_buffer[MAXPATHLEN];
iconv_t converter = iconv_open("UTF-8", "wchar_t");
char* result = result_buffer;
char* input = (char*)wchar;
size_t output_available_size = sizeof result_buffer;
size_t input_available_size = utf32_bytes;
size_t result_code = iconv(converter, &input, &input_available_size, &result, &output_available_size);
if (result_code == -1)
{
perror("iconv");
return NULL;
}
iconv_close(converter);
return strdup(result_buffer);
}
int main()
{
wchar_t hello_world[] = L"/éè/path/to/hello/world.txt";
char* utf8 = utf8path(hello_world, sizeof hello_world);
printf("%s\n", utf8);
free(utf8);
return 0;
}
The utf8_hello_world function accepts a wchar_t string with its byte length and returns the equivalent UTF-8 string. If you deal with pointers to wchar_t instead of an array of wchar_t, you'll want to use (wcslen(ptr) + 1) * sizeof(wchar_t) instead of sizeof.
Mac OS X uses UTF-8, so you need to convert the wide-character strings into UTF-8. You can do this using wcstombs, provided you first switch into a UTF-8 locale. For example:
// Do this once at program startup
setlocale(LC_ALL, "en_US.UTF-8");
...
// Error checking omitted for expository purposes
wchar_t *wideFilename = ...; // This comes from wherever
char filename[256]; // Make sure this buffer is big enough!
wcstombs(filename, wideFilename, sizeof(filename));
// Construct popen command using the UTF-8 filename
You can also use libiconv to do the UTF-16 to UTF-8 conversion for you if you don't want to change your program's locale setting; you could also roll your own implementation, as doing the conversion is not all that complicated.
To get the volume GUID i tried the code like below
int len = wcslen( pDetData->DevicePath);
pDetData->DevicePath[len] = '\\';
pDetData->DevicePath[len+1] = 0;
#define BUFFER_SIZE MAX_PATH
WCHAR volume[BUFFER_SIZE];
BOOL bFlag;
bFlag = GetVolumeNameForVolumeMountPoint( pDetData->DevicePath, volume, BUFFER_SIZE );
int loginErrCode = GetLastError();
printf("loginErrCode: %d\n", loginErrCode);
printf("BFLAG: %d\n", bFlag);
the GetLastError() also prints it as 1 . it means ERROR_INVALID_FUNCTION. The bFlag always returns zero it means false.
what is the problem in my code...
This requires some crystal-ball consulting. The DevicePath string looks like it comes from SP_DEVICE_INTERFACE_DETAIL_DATA. That's a string that you don't own, modifying it corrupts the internal setupapi database at best, the heap at worst. You'll have to copy the string into your own buffer before turning it into the root directory name.
This is just a theory, especially "loginErrCode" is a very strange name for what the code seems to do. Verify that the string you end up with at least looks similar to "F:\".
I'm running some commands with the C++ system() function:
int system ( const char * command );
How can I collect the standard output from the issued commands?
To be specific, I want to collect the output of the issued command (for example, the directory listing output from issuing the dir command).
Are you looking for returned value (as in "exit status") of the executed command, or for its output (as in "what did it print")?
If the latter, use popen() and pclose() instead.
If the former, look at the return value from system() (and use the documentation for waitpid() to interpret it).
system() returns an int, so just grab it: int rvalue = system(command);
I believe the exact details of what system() will return are system-specific, though.
There are typically two ways for a system program to "return" a value: by writing to stdout, and by returning a status integer at the end of the program. (there are often more ways to return results, eg. by writing to a file or into a database, but I assume those are out of scope here).
For receiving the status code, just check the return value of the system function.
For receiving the output, either redirect it into a file, and read the file afterwards, or use popen.
The return value of system is (ironically) system-dependent, but in POSIX systems (including Linux, etc), it's the same as for wait -- low 8 or 16 bits are the exit status of the child (probably what you mean by "value returned by"), higher bits indicating what kind of signal terminated the child, if any. The URL to the manpage I've given supplies the preprocessor macros you can use to pry apart that return value!
There is no such thing as a "return string" of a program, as you've now clarified in a comment is what you desire; as another answer already mentioned, if you want the text which gets output by the other program, you should use popen instead of system.
Inspired by bmorin's attempt, but working and tested, this snippet will take a char* command and return a char* containing the results of executing that command...
// Calling function must free the returned result.
char* exec(const char* command) {
FILE* fp;
char* line = NULL;
// Following initialization is equivalent to char* result = ""; and just
// initializes result to an empty string, only it works with
// -Werror=write-strings and is so much less clear.
char* result = (char*) calloc(1, 1);
size_t len = 0;
fflush(NULL);
fp = popen(command, "r");
if (fp == NULL) {
printf("Cannot execute command:\n%s\n", command);
return NULL;
}
while(getline(&line, &len, fp) != -1) {
// +1 below to allow room for null terminator.
result = (char*) realloc(result, strlen(result) + strlen(line) + 1);
// +1 below so we copy the final null terminator.
strncpy(result + strlen(result), line, strlen(line) + 1);
free(line);
line = NULL;
}
fflush(fp);
if (pclose(fp) != 0) {
perror("Cannot close stream.\n");
}
return result;
}
I looked into just editing bmorin's code, but would have had to change most lines, so a separate answer seemed more appropriate. Apologies if not. (Amongst other problems, bmorin's code didn't actually accumulate the lines; it printed them to stdout, where I presume they would not be wanted, since system() would have done that; and it returned void in one error path, when the function must return a char*, so the code wouldn't compile. Perhaps most egregious, it freed the result just before returning it.)
system() is declared and defined in libc. You can either read the first link I provided, or do man system at a command prompt in your shell.
I suggest the popen() functions, as said by other people as well,
but this problem is platform specific. the popen() function is
available on operating systems that use the POSIX API. I am not
sure if this command would work on other APIs like WIN32
Here is a code snippet (in plain C) executing a command with popen and returning its output:
char* exec(const char* command) {
FILE* fp;
char* result = NULL;
size_t len = 0;
fflush(NULL);
fp = popen(command, "r");
if (fp == NULL) {
printf("Cannot execute command:\n%s\n", command);
return;
}
while(getline(&result, &len, fp) != -1) {
fputs(result, stdout);
}
free(result);
fflush(fp);
if (pclose(fp) != 0) {
perror("Cannot close stream.\n");
}
return result;
}