cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

C Function getenv() crashing in Windows10

Highlighted
Garnet

C Function getenv() crashing in Windows10

I.T.  updated one of my Engineers to Windows 10.  He is our first one using Creo 3.0 m120 on the Windows 10 platform.   One of my Toolkit Applications crashes on initialization.  The apps work fine on the Win7 platform.   I have Narrowed it down to the getenv() function.     I checked on another program and sure enough the function where that is used crashes too.  It does not crash on the call, but on the next line.    I am compiling with VS 2012 (approved for Creo 3).

The getenv() is pretty critical to me as my apps run in different sites and different countries.

Has anyone else seen this, or can someone else verify this? 

FILE *fp; /* test output */

char myvar[80];

char myvar2[16];

char *value;

 

SiteCode = 1;

fp = fopen ("c:\\temp\\subs.txt", "w");

/*================================================================*/

/*       Set global variables for exports  */

 

      value = getenv("MFG_SITE");

      strcpy(myvar2, value);     /******************** Will crash here */

      if (strstr(myvar2, "MTP") != NULL)

                                  SiteCode = 1;

      if (strstr(myvar2, "CRV") != NULL)

                                  SiteCode = 2;

      if (strstr(myvar2, "BGV") != NULL) 

                                  SiteCode = 3;

 

I also have crashes when trying to use the return value directly. 

  fprintf (fp, "Plat code is: %s  \n" , getenv("MFG_SITE"));

 

Tags (5)
7 REPLIES 7
Highlighted

Re: C Function getenv() crashing in Windows10

Hi,

 

I was just curious, therefore I asked Google > getenv() function windows 10

 

One resulting link is https://stackoverflow.com/questions/631664/accessing-environment-variables-in-c - maybe it gives you some idea how to solve the problem.

 

MH


Martin Hanák
Highlighted

Re: C Function getenv() crashing in Windows10

Thanks Martin.  From What I can tell so far it seems that Windows10 doesn't appreciate app compiled in VS2012 which is the required compiler for Creo 3.0.   That function seems to be a source of contention, but I am just reading the vars.   That being said I have a colleague who says getenv  it works for them in a Creo3 app running on Win10..  I made a freestanding (console app) and it wont run in windows 10 either.  I am using VS studio express.  I compile via command line using MAK files.  

  

Highlighted

Re: C Function getenv() crashing in Windows10

Hi @TomasLoun,

 

please can you comment the problem ?


Martin Hanák
Highlighted

Re: C Function getenv() crashing in Windows10

I have talked to a colleague that write toolkit app, and he also cannot get this simple app to start up.

I am curious if anyone with Creo3 on Windows10 can get this toolkit app to start. Without it crashing Creo.  (DLL)

int user_initialize() 

{

char myvar2[40];

char* value;

value = getenv("USERNAME");

strcpy(myvar2, value);

return(0);

}

Highlighted

Re: C Function getenv() crashing in Windows10

Hi I use getenv inside my code and it work.

 

char const* tmp = getenv(tag.c_str());
if (tmp != NULL) {
std::string s(tmp);

return s;
}

 

We also use _wgetenv() and the windows precompiler variant : GetEnvironmentVariable() 🙂

We compile for Creo 2 (VS2010) and Creo 3 (VS2012) and have no problems running these application at the moment on over some thousend mashines all over europe (so different time and language settings).

 

Don't know if there are some compiler flags change the way this one get used inside of windows.

 

Br,

Eike

 

PS: getenv is unsafe. Better use getenv_s if you want to be sure that your buffer is big enough. Or something that handle it like this :


/*====================================================================*\
FUNCTION : readEnvironmentVariable(char* value, char * variable)
PURPOSE : reads the given environment variable (suprise...) \*====================================================================*/
int readEnvironmentVariable(char* value, char * variable) {
TCHAR szCmd[MAX_PATH];
if (GetEnvironmentVariable(string2wstring(variable).c_str(), szCmd, sizeof(szCmd))) {
writeLog("<GTFC> Environment variable found", 1);
strcpy(value, wstring2string(wstring(szCmd)).c_str());
return 0;
}
else {
writeLog("<GTFC> No environment variable found", 1);
return 1;
}
}

Highlighted

Re: C Function getenv() crashing in Windows10

Thanks for your reply Eike.    Interesting that you can get it to work.  The same code for me simply wont run.   It crashes pro every time.   PTC has written me a sample app, same thing.    I have compiled on two different installs of VS2012 just to be safe.    Wondering if its my make file, but I use the samples given in protk folder,  and for PTCLIBS_DLL I am using $(PROTOOL_SYS)/obj/protk_dll_NU.lib $(PROTOOL_SYS)/obj/ucore.lib $(PROTOOL_SYS)/obj/udata.lib .   This is what the user guide says to use, even tho the included mak files do not reflect this.   Same result either way. 

Highlighted

Re: C Function getenv() crashing in Windows10

Hi

 

I use a std. VS project. Don't think that make files are a good solution if you can avoid it, because you need to fill them up with and you don't need them if you choose a dll-project in VS 2010 / 2012 / 2015 ... whatever.

 

You can download our project (its 2 years old ... so no magic inside) here:

https://modelprocessor.inneo.com/updates/VS2010Proj.zip

 

It's a VS 2010 project and we have updated many different thinks inside it ... but you can simply change the compiler to 2012 and change the include paths (Properties --> C/C++ --> General) and the Linker paths (Linker --> Additional Library Directories).

 

Br,

Eike

 

PS: We use protk_dll.lib;otk_cpp.lib only

Announcements