获得系统路径

qubo_528 2009-02-19 03:04:35
如何获得c:\window2\system32路径
应该是获得系统放动态库的路径。xp、vista、windows7各系统平台的路径一旦有变化,都能准确的获得
如考虑多系统的情况下,可能就不是c盘了。
...全文
330 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
csgdseed 2009-02-19
  • 打赏
  • 举报
回复
vista下api应该不变吧
waizqfor 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 qubo_528 的回复:]
要是系统在d盘,也会正确获得吧
要是vist时,是获得GetSystemDirectory还是获得GetSystemWow64Directory呢
[/Quote]
我记得VIST下GetSystemDirectory也可以的
qubo_528 2009-02-19
  • 打赏
  • 举报
回复
要是系统在d盘,也会正确获得吧
要是vist时,是获得GetSystemDirectory还是获得GetSystemWow64Directory呢
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 baihacker 的回复:]
C/C++ code#include <windows.h>
#include <iostream>
using namespace std;

int main(void)
{

char path[MAX_PATH];
GetSystemDirectory(path, MAX_PATH);
cout << path << endl;

return 0;
}
[/Quote]
Win下的不熟,学习了。
waizqfor 2009-02-19
  • 打赏
  • 举报
回复
[Quote=引用楼主 qubo_528 的帖子:]
如何获得c:\window2\system32路径
应该是获得系统放动态库的路径。xp、vista、windows7各系统平台的路径一旦有变化,都能准确的获得
如考虑多系统的情况下,可能就不是c盘了。
[/Quote]
用GetSystemDirectory函数就可以了

#include <windows.h>
#include <tchar.h>
#include <stdio.h>

TCHAR* envVarStrings[] =
{
TEXT("OS = %OS%"),
TEXT("PATH = %PATH%"),
TEXT("HOMEPATH = %HOMEPATH%"),
TEXT("TEMP = %TEMP%")
};
#define ENV_VAR_STRING_COUNT (sizeof(envVarStrings)/sizeof(TCHAR*))
#define INFO_BUFFER_SIZE 32767
void printError( TCHAR* msg );

void main( )
{
DWORD i;
TCHAR infoBuf[INFO_BUFFER_SIZE];
DWORD bufCharCount = INFO_BUFFER_SIZE;

// Get and display the name of the computer.
bufCharCount = INFO_BUFFER_SIZE;
if( !GetComputerName( infoBuf, &bufCharCount ) )
printError( TEXT("GetComputerName") );
_tprintf( TEXT("\nComputer name: %s"), infoBuf );

// Get and display the user name.
bufCharCount = INFO_BUFFER_SIZE;
if( !GetUserName( infoBuf, &bufCharCount ) )
printError( TEXT("GetUserName") );
_tprintf( TEXT("\nUser name: %s"), infoBuf );

// Get and display the system directory.
if( !GetSystemDirectory( infoBuf, INFO_BUFFER_SIZE ) )
printError( TEXT("GetSystemDirectory") );
_tprintf( TEXT("\nSystem Directory: %s"), infoBuf );

// Get and display the Windows directory.
if( !GetWindowsDirectory( infoBuf, INFO_BUFFER_SIZE ) )
printError( TEXT("GetWindowsDirectory") );
_tprintf( TEXT("\nWindows Directory: %s"), infoBuf );

// Expand and display a few environment variables.
_tprintf( TEXT("\n\nSmall selection of Environment Variables:") );
for( i = 0; i < ENV_VAR_STRING_COUNT; ++i )
{
bufCharCount = ExpandEnvironmentStrings(envVarStrings[i], infoBuf,
INFO_BUFFER_SIZE );
if( bufCharCount > INFO_BUFFER_SIZE )
_tprintf( TEXT("\n\t(Buffer too small to expand: \"%s\")"),
envVarStrings[i] );
else if( !bufCharCount )
printError( TEXT("ExpandEnvironmentStrings") );
else
_tprintf( TEXT("\n %s"), infoBuf );
}
}

void printError( TCHAR* msg )
{
DWORD eNum;
TCHAR sysMsg[256];
TCHAR* p;

eNum = GetLastError( );
FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, eNum,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
sysMsg, 256, NULL );

// Trim the end of the line and terminate it with a null
p = sysMsg;
while( ( *p > 31 ) || ( *p == 9 ) )
++p;
do { *p-- = 0; } while( ( p >= sysMsg ) &&
( ( *p == '.' ) || ( *p < 33 ) ) );

// Display the message
_tprintf( TEXT("\n\t%s failed with error %d (%s)"),
msg, eNum, sysMsg );
}

baihacker 2009-02-19
  • 打赏
  • 举报
回复
#include <windows.h>
#include <iostream>
using namespace std;

int main(void)
{

char path[MAX_PATH];
GetSystemDirectory(path, MAX_PATH);
cout << path << endl;

return 0;
}
wuyu637 2009-02-19
  • 打赏
  • 举报
回复
GetSystemDirectory
The GetSystemDirectory function retrieves the path of the system directory. The system directory contains such files as dynamic-link libraries, drivers, and font files.

UINT GetSystemDirectory(
LPTSTR lpBuffer, // 系统路径名指针
UINT uSize // 路径字符长度
);

Parameters
lpBuffer
Pointer to the buffer to receive the null-terminated string containing the path. This path does not end with a backslash unless the system directory is the root directory. For example, if the system directory is named WINDOWS\SYSTEM on drive C, the path of the system directory retrieved by this function is C:\WINDOWS\SYSTEM.
uSize
Specifies the maximum size of the buffer, in characters. This value should be set to at least MAX_PATH.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer, not including the terminating null character. If the length is greater than the size of the buffer, the return value is the size of the buffer required to hold the path.

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧