Does setenv not set variables for MEX functions?

7 ビュー (過去 30 日間)
angainor
angainor 2012 年 8 月 9 日
Hello,
I have noticed that when I use setenv in MATLAB on Windows, the change is not visible in MEX functions. The behavior is as I would expect on Linux. I tested the following MEX function on MATLAB 2011a and 2012a:
#include <mex.h>
void mexFunction(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[])
{
printf("OS %s\n", getenv("OS"));
printf("AAA %s\n", getenv("AAA"));
}
When I run test from MATLAB comandline I get
>> test
OS Windows_NT
AAA (null)
I get exactly the same when I setenv('AAA') to some value:
>> setenv('AAA','BBB')
>> getenv('AAA')
ans =
BBB
>> test
OS Windows_NT
AAA (null)
Is that a feature, or is it a bug?
Thanks a lot!
Marcin

採用された回答

Friedrich
Friedrich 2012 年 8 月 13 日
Hi,
I think the following Microsoft statement should help here:
"getenv and _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. "
I think you would need to use GetEnvironmentVariable instead
To answer it pretty short: It seems like getenv only looks at the global variables, and GetEnvironmentVariable works for application specific too
I modified the code to (change extension to .cpp)
#include <mex.h>
#include "Windows.h"
void mexFunction(int nargout, mxArray *pargout [ ], int nargin, const mxArray *pargin[])
{
const DWORD buff_size = 50;
LPTSTR buff = new TCHAR[buff_size];
DWORD var_size;
buff[0] = '\0';
printf("OS %s\n", getenv("OS"));
printf("AAA %s\n", getenv("AAA"));
var_size = GetEnvironmentVariable("AAA",buff,buff_size);
printf("AAA through GetEnvironmentVariable %s\n",buff);
}
And when you run it you see this:
>> test
OS Windows_NT
AAA (null)
AAA through GetEnvironmentVariable
>> setenv('AAA','BBB11')
>> test
OS Windows_NT
AAA (null)
AAA through GetEnvironmentVariable BBB11
>>
  1 件のコメント
Kaustubha Govind
Kaustubha Govind 2012 年 8 月 14 日
Nice! Great answer Friedrich. :)

サインインしてコメントする。

その他の回答 (1 件)

Kaustubha Govind
Kaustubha Govind 2012 年 8 月 9 日
I think this is an OS-defined behavior and not specific to MATLAB. Extrapolating from this discussion, the loader (which is what calls into the MEX-file - since MEX-files are essentially DLLs/shared libraries) reads the environment variables at the time of MATLAB startup, and does not notice your changes to the environment variables, which in turn means that the MEX-file doesn't see them.
  6 件のコメント
Kaustubha Govind
Kaustubha Govind 2012 年 8 月 10 日
Ah! Okay then. I'm still inclined to think that this might be OS-defined behavior, but I don't have much experience with this, so I would recommend contacting MathWorks Tech Support at this point. Sorry!
angainor
angainor 2012 年 8 月 10 日
Thank you! I guess I'll do that..

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeWrite C Functions Callable from MATLAB (MEX Files) についてさらに検索

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by