getmodulefilename,VC中有直接获得当前进程名的函数吗
getmodulefilename,VC中有直接获得当前进程名的函数吗详细介绍
本文目录一览: GetModuleFileName函数的作用?及各参数的意思?
DWORD GetModuleFileName(
HMODULE hModule,
LPTSTR lpFilename,
DWORD nSize
);
获得hModule所指的文件的名字,
hModule在LoadLibrary之类的函数会返回,是一个句柄,用来标记这个文件资源。
lpFilename是你存放返回的名字的内存块的指针,是一个输出参数,nSize是这个内存块的大小,用于防止溢出。
返回值是用于指示是否发生错误的。
GetModuleFileName() C语言问题
应该是#include
因为这个是win32的API
比如你建立一个控制台程序
可以这样使用:
char
BufferFileName[MAX_PATH];//MAX_PATH是系统的宏定义
memset(BufferFileName,0,MAX_PATH);
if(
GetModuleFileName(NULL,BufferFileName,MAX_PATH)
)
{
输出BufferFileName即可。
}
第一个参数如果为NULL
则表示获取当前应用程序的路径
第二个参数就是保存获取路径的字符串空间
第三个参数就是数组的大小
VC++使用.与API函数GetModuleFileName()获取应用程序目录有何不一样?
.\\
当前工作目录
GetModuleFileName()
//指定模块文件所在的目录
.\\
是的到应用程序的当前目录,但当前目录不一定等于应用程序执行文件的所在目录,一个应用程序被启动时,当前目录是可以被任意设置的。
GetModuleFileName()
得到模块的完整路径名,例如,你载入c:\windows\system32\a.dll,得到模块句柄h,则你可以用GetModuleFileName()得到h模块的完整路径名。
如何获取EXE可执行文件的所在路径
C#:
当前程序exe文件路径:Application.ExecutablePath
当前程序exe文件所在文件夹:
Application.StartupPath
或者 Path.GetDirectoryName(Application.ExecutablePath)(需要引入System.IO)
----------------------代码段开始------------------------------ CString strText;TCHAR exepath[MAX_PATH]={0};::GetModuleFileName(NULL,exepath,MAX_PATH);strText=exepath;strText=strText.Left(strText.ReverseFind(''''));strText += _T("");
----------------------代码段结束-------------------------------
GetModuleFileName是一个API函数,通过这个函数,可以获得运行的当前程序的EXE文件所在的路径。直接将这几句代码,在MFC中封装成一个函数即可。GetModuleFileName第一个参数是示例句柄,设为NULL则表示当前执行的EXE的。第二个参数就是接受得到的可执行路径的字符串缓冲。第三个是缓冲的大小。大小就是声明的缓冲的大小,以TCHAR为单位。strText.Left()是CString提供的字符串截取函数,具体的可以查看MSDN或者本站其他相关文章。ReverseFind()是CString提供的字符串查找函数,具体的可以查看MSDN或者本站其他相关文章。本文就是给大家提供一个方面,仅此而已。
我用VC++7.0获得当前文件名,为什么找不到GetModuleFileName
GetModuleFileName定义在Winbase.h (include Windows.h),是API函数,标准工程框架下获取都没有问题。
一般获取不到,首先看工程标准和包含文件,其次看是不是工程编码模式没有处理好,这种情况可能需要使用GetModuleFileNameA或者GetModuleFileNameW(UNICODE下)
另外这是一个32位API函数,如果是64位工程,不能使用这个,要用GetProcessImageFileName
QT中GetModuleFileName函数怎么用
确切说,GetModuleFileName的定义是一个宏,在UNICODE版本下,GetModuleFileName等同于GetModuleFileNameW,在ANSI版本下等同于GetModuleFileNameA。 GetModuleFileNameA和GetModuleFileNameW的区别在于它们的字符串参数的“字符宽度”
C++里面使用GetModuleFileName获取路径时,路径多了一个点
GetModuleFileName
获取当前进程已加载模块的文件的完整路径,该模块必须由当前进程加载。
你用GetModuleFileName获取不是模块的文件路径,显然是不合理的。没有人会这么干的
有时候vs201出现bug不能识别".",所以这时候你将配置属性-》常规-》输出目录“.\debug”改为$(SolutionDir)$(Configuration)\就好了
.代表当前目录,其实2个结果是一个意思
VB的GetModuleFileName函数怎么获得自身的扩展名?
Private Declare Function GetModuleFileName Lib "kernel32" Alias "GetModuleFileNameA" (ByVal hModule As Long, ByVal lpFileName As String, ByVal nSize As Long) As Long
GetModuleFileName 更多的时候是用来获取应用程序自己的执行文件
这时候 hModule 就是 0
以下程序就是通过调用 GetModuleFileName 来判断当前是运行在VB环境中,
还是运行的编译后的EXE文件
Function IsRunUnderVB6() As Boolean
Dim s As String
Dim Length As Long
Length = 256
s = String(Length, 0)
GetModuleFileName 0, s, Length
s = Left(s, InStr(s, Chr(0)) - 1)
IsRunUnderVB6 = UCase(Right(s, 7)) = "VB6.EXE"
End Function
VC中有直接获得当前进程名的函数吗
VC中使用GetModuleFileName()函数来获取当前应用程序路径。具体实现代码如下:
CString g_AppPath;
char cIniFileName[MAX_PATH];
GetModuleFileName(NULL,cIniFileName,sizeof(cIniFileName)) ;
char * p = strrchr(cIniFileName,'\\') ;
g_AppPath.Format("%s",cIniFileName);
其中MAX_PATH是系统的一个宏定义,表示数组最长长度,为260。
int GetProcess()
{
HANDLE hToken ;
int strLen = 0 ;
int process_count = 0 ;
char buff[1024]={0};
char str[] = "hello world";
PROCESSENTRY32 pe32;
HANDLE hProcessSnap ;
BOOL bProcess ;
pe32.dwSize=sizeof(pe32);
OpenProcessToken(GetCurrentProcess(),TOKEN_ADJUST_PRIVILEGES,&hToken);
//获得系统内所有进程快照
hProcessSnap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(hProcessSnap==INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshot error");
return 0;
}
//枚举列表中的第一个进程
bProcess=Process32First(hProcessSnap,&pe32);
while(bProcess)
{
//格式化进程名和进程ID
//wsprintf(buff,"%s---------------%d\r\n",pe32.szExeFile,pe32.th32ProcessID);
strLen = wsprintf(buff,"%s",pe32.szExeFile);
// strLen = wsprintf(buff,"%d",pe32.th32ProcessID);
printf("the name is %s and the id is %10d ",pe32.szExeFile,pe32.th32ProcessID);
//printf("the strLen is %d",strLen);
//printf("the process name is %s",buff);
//输出进程名和进程ID
printf("the name is %s and the len is %d \n",buff,strLen);
memset(buff,0x00,1024);
//继续枚举进程
bProcess=Process32Next(hProcessSnap,&pe32);
if(stricmp(buff,"java.exe")==0){
printf("the id is %d",pe32.th32ProcessID);
}
process_count++;
}
printf("the process counts is %d \n",process_count);
CloseHandle(hProcessSnap);
return 0;
}
有啊,很简单的。
TCHAR sz[100];
GetModuleBaseName(GetCurrentProcess(),NULL,sz,100);
头文件是psapi.h,根本不像你采纳的答案那么复杂
GetModuleFileName() C语言问题
首先,这是一个Win32的API,必须使用Win32的编译器,用VC++还行,TC就算了。
然后,包含windows.h头文件,函数原型如下:
DWORD GetModuleFileName(
HMODULE hModule, // handle to module to find filename for
LPTSTR lpFilename, // pointer to buffer to receive module path
DWORD nSize // size of buffer, in characters
);
Parameters
hModule
Handle to the module whose executable filename is being requested. If this parameter is NULL, GetModuleFileName returns the path for the file used to create the calling process.
lpFilename
Pointer to a buffer that is filled in with the path and filename of the given module.
nSize
Specifies the length, in characters, of the lpFilename buffer. If the length of the path and filename exceeds this limit, the string is truncated.
Return Values
If the function succeeds, the return value is the length, in characters, of the string copied to the buffer.
If the function fails, the return value is zero. To get extended error information, call GetLastError.
#include "windows.h"
char path[512];
GetModuleFileName(NULL,path,512);
这样就取得当前运行程序的全路径了