Microsoft KB Archive/46851

= Calling C Function That Uses getenv Doesn't Work =

Article ID: 46851

Article Last Modified on 11/21/2006

-

APPLIES TO


 * Microsoft QuickBasic 4.0
 * Microsoft QuickBASIC 4.0b
 * Microsoft QuickBasic 4.5 for MS-DOS
 * Microsoft BASIC Compiler 6.0
 * Microsoft BASIC Compiler 6.0b
 * Microsoft BASIC Professional Development System 7.0
 * Microsoft QuickC Compiler with QuickAssembler 2.51
 * Microsoft C Professional Development System 7.0

-



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.



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: */ void EnvironTest(void) { char *temp; temp = getenv( &quot;PATH&quot; ); printf( &quot;Path: %s\nenviron: %x\n&quot;, temp, environ ); }
 * 1) include 
 * 2) include 

Additional query words: QuickBas BasicCom

Keywords: KB46851

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.