Calling C Function That Uses getenv() Doesn't Work (46851)



The information in this article applies to:

  • Microsoft QuickBASIC 4.0
  • Microsoft QuickBASIC 4.0b
  • Microsoft QuickBASIC 4.5
  • Microsoft BASIC Compiler for MS-DOS 6.0
  • Microsoft BASIC Compiler for MS-DOS 6.0b
  • Microsoft Basic Professional Development System for MS-DOS 7.0
  • Microsoft QuickC Compiler
  • Microsoft C Compiler

This article was previously published under Q46851

SUMMARY

When a compiled Basic program CALLs a C function that uses the getenv() function, no path is returned from getenv(). The getenv() function in C uses the ENVIRON variable to access the environment table. The ENVIRON variable is an array of pointers to the strings that constitute the process environment. This variable is initialized by the C start-up code. Since the C function is CALLed from the Basic program, the C start-up code is not executed; therefore, the ENVIRON variable is not initialized (it is NUL by default).

To work around this problem, use the ENVIRON$ function in the Basic program and pass the desired path to the C function.

This information applies to Microsoft QuickBasic Versions 4.00, 4.00b, and 4.50; to Microsoft Basic Compiler Versions 6.00 and 6.00b; to Microsoft Basic PDS Version 7.00; to Microsoft QuickC Compiler Versions 1.00, 1.01, 2.00, 2.01; and to Microsoft C Compiler Versions 5.00 and 5.10.

MORE INFORMATION

Code Example

The following are the Basic and C source files that demonstrate that CALLing a C function that uses the getenv() function returns a NUL ENVIRON variable:
' Basic Source Code:
DECLARE SUB EnvironTest CDECL ()
CLS
CALL EnvironTest
END
/* C source code: */ 
#include <stdlib.h>
#include <stdio.h>
void EnvironTest(void)
{
  char *temp;
  temp = getenv( "PATH" );
  printf( "Path: %s\nenviron: %x\n", temp, environ );
}
				

Modification Type:MinorLast Reviewed:8/16/2005
Keywords:KB46851