extern “C” char** environ - Windows - C++/CLI

2019-05-23 00:17发布

I have some old linux code I'm trying to port to Windows. When I first built it as a straight native DLL, I go no issues with this piece of code, but when I tried making it a mixed-mode C++/CLI DLL, I got an unresolved external object error on this:

extern "C" char** environ;

Why would this work for native and not CLI? Any idea how to work around this, or what it even does?

1条回答
看我几分像从前
2楼-- · 2019-05-23 01:08

That holds the environment variables (PATH, etc, etc). The C standard (if i recall correctly) requires environ to point to an array of these variables. They're also passed as the 3rd argument to the main entry point function.

Apparently, for some reason, the C++/CLI doesn't initialize that.

To fix that, you can allocate it yourself and fill with either either getenv (C) or Environment.GetEnvironmentVariables (Managed C++). I don't know of any in-place fix, but it shouldn't be too hard.

查看更多
登录 后发表回答