windows下,我在一個控制臺程式里創建視窗界面。vs啟動和雙擊生成的exe沒有問題。
但是命令列用命令啟動時出問題了。
開始用start命令加絕對路徑啟動exe,應該是exe的運行路徑變了,導致程式里用相對路徑讀檔案時報錯。
于是我切換到exe所在目錄,直接運行exe,這時候程式正常啟動了。
但是window視窗沒有創建。
有大佬知道的,請指教一下。
我的目的是在代碼里監視目標程式的運行,在它掛了的時候重啟它。
找到過一個現有的軟體可以使用,但是一樣重啟后有問題。
怎么樣在代碼里做到雙擊啟動目標程式的效果。
uj5u.com熱心網友回復:
這種區別一般是來自你程式內部,對一些組態檔讀取處理不一致。雙擊運行跟命令運行,當前路徑是不一樣的,也就是說代碼里面可能一些相對路徑會有不一樣的問題,包括一些動態加載背景圖片什么的
uj5u.com熱心網友回復:
SetCurrentDirectoryThe SetCurrentDirectory function changes the current directory for the current process.
BOOL SetCurrentDirectory(
LPCTSTR lpPathName // pointer to name of new current directory
);
Parameters
lpPathName
Pointer to a null-terminated string that specifies the path to the new current directory. This parameter may be a relative path or a fully qualified path. In either case, the fully qualified path of the specified directory is calculated and stored as the current directory.
Return Values
If the function succeeds, the return value is nonzero.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
Remarks
Each process has a single current directory made up of two parts:
A disk designator that is either a drive letter followed by a colon, or a server name and share name (\\servername\sharename)
A directory on the disk designator
QuickInfo
Windows NT: Requires version 3.1 or later.
Windows: Requires Windows 95 or later.
Windows CE: Unsupported.
Header: Declared in winbase.h.
Import Library: Use kernel32.lib.
Unicode: Implemented as Unicode and ANSI versions on Windows NT.
See Also
File I/O Overview, File Functions, GetCurrentDirectory
_fullpath, _wfullpath
Create an absolute or full path name for the specified relative path name.
char *_fullpath( char *absPath, const char *relPath, size_t maxLength );
wchar_t *_wfullpath( wchar_t *absPath, const wchar_t *relPath, size_t maxLength );
Function Required Header Compatibility
_fullpath <stdlib.h> Win 95, Win NT
_wfullpath <stdlib.h> or <wchar.h> Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a pointer to a buffer containing the absolute path name (absPath). If there is an error (for example, if the value passed in relPath includes a drive letter that is not valid or cannot be found, or if the length of the created absolute path name (absPath) is greater than maxLength) the function returns NULL.
Parameters
absPath
Pointer to a buffer containing the absolute or full path name
relPath
Relative path name
maxLength
Maximum length of the absolute path name buffer (absPath). This length is in bytes for _fullpath but in wide characters (wchar_t) for _wfullpath.
Remarks
The _fullpath function expands the relative path name in relPath to its fully qualified or “absolute” path, and stores this name in absPath. A relative path name specifies a path to another location from the current location (such as the current working directory: “.”). An absolute path name is the expansion of a relative path name that states the entire path required to reach the desired location from the root of the filesystem. Unlike _makepath, _fullpath can be used to obtain the absolute path name for relative paths (relPath) that include “./” or “../” in their names.
For example, to use C run-time routines, the application must include the header files that contain the declarations for the routines. Each header file include statement references the location of the file in a relative manner (from the application’s working directory):
#include <stdlib.h>
when the absolute path (actual file system location) of the file may be:
\\machine\shareName\msvcSrc\crt\headerFiles\stdlib.h
_fullpath automatically handles multibyte-character string arguments as appropriate, recognizing multibyte-character sequences according to the multibyte code page currently in use. _wfullpath is a wide-character version of _fullpath; the string arguments to _wfullpath are wide-character strings. _wfullpath and _fullpath behave identically except that _wfullpath does not handle multibyte-character strings.
Generic-Text Routine Mappings
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tfullpath _fullpath _fullpath _wfullpath
If the absPath buffer is NULL, _fullpath calls malloc to allocate a buffer of size _MAX_PATH and ignores the maxLength argument. It is the caller’s responsibility to deallocate this buffer (using free) as appropriate. If the relPath argument specifies a disk drive, the current directory of this drive is combined with the path.
Example
/* FULLPATH.C: This program demonstrates how _fullpath
* creates a full path from a partial path.
*/
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <direct.h>
char full[_MAX_PATH], part[_MAX_PATH];
void main( void )
{
while( 1 )
{
printf( "Enter partial path or ENTER to quit: " );
gets( part );
if( part[0] == 0 )
break;
if( _fullpath( full, part, _MAX_PATH ) != NULL )
printf( "Full path is: %s\n", full );
else
printf( "Invalid path\n" );
}
}
File Handling Routines
See Also _getcwd, _getdcwd, _makepath, _splitpath
uj5u.com熱心網友回復:
_chdir, _wchdirChange the current working directory.
int _chdir( const char *dirname );
int _wchdir( const wchar_t *dirname );
Routine Required Header Optional Headers Compatibility
_chdir <direct.h> <errno.h> Win 95, Win NT
_wchdir <direct.h> or <wchar.h> <errno.h> Win NT
For additional compatibility information, see Compatibility in the Introduction.
Libraries
LIBC.LIB Single thread static library, retail version
LIBCMT.LIB Multithread static library, retail version
MSVCRT.LIB Import library for MSVCRT.DLL, retail version
Return Value
Each of these functions returns a value of 0 if successful. A return value of –1 indicates that the specified path could not be found, in which case errno is set to ENOENT.
Parameter
dirname
Path of new working directory
Remarks
The _chdir function changes the current working directory to the directory specified by dirname. The dirname parameter must refer to an existing directory. This function can change the current working directory on any drive and if a new drive letter is specified in dirname, the default drive letter will be changed as well. For example, if A is the default drive letter and \BIN is the current working directory, the following call changes the current working directory for drive C and establishes C as the new default drive:
_chdir("c:\\temp");
When you use the optional backslash character (\) in paths, you must place two backslashes (\\) in a C string literal to represent a single backslash (\).
_wchdir is a wide-character version of _chdir; the dirname argument to _wchdir is a wide-character string. _wchdir and _chdir behave identically otherwise.
Generic-Text Routine Mapping:
TCHAR.H Routine _UNICODE & _MBCS Not Defined _MBCS Defined _UNICODE Defined
_tchdir _chdir _chdir _wchdir
Example
/* CHGDIR.C: This program uses the _chdir function to verify
* that a given directory exists.
*/
#include <direct.h>
#include <stdio.h>
#include <stdlib.h>
void main( int argc, char *argv[] )
{
if( _chdir( argv[1] ) )
printf( "Unable to locate the directory: %s\n", argv[1] );
else
system( "dir *.wri");
}
Output
Volume in drive C is CDRIVE
Volume Serial Number is 0E17-1702
Directory of C:\write
04/21/95 01:06p 3,200 ERRATA.WRI
04/21/95 01:06p 2,816 README.WRI
2 File(s) 6,016 bytes
71,432,116 bytes free
Directory Control Routines
See Also _mkdir, _rmdir, system
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/256886.html
標籤:工具平臺和程序庫
上一篇:洛谷題求解,二分查找
下一篇:qt自定義的委托單擊編輯
