MFC常用函数

1 GetModuleFileName()

函数原型

1

2

3

4

5

DWORDGetModuleFileName(

HMODULEhModule,

LPTSTRlpFilename,

DWORDnSize

);

函数参数

HMODULE hModule 装载一个程序实例的句柄。如果该参数为NULL,该函数返回该当前 应用程序全路径。

LPTSTR  lpFileName 是你存放返回的名字的内存块的指针,是一个输出参数

DWORD nSize,装载到缓冲区lpFileName的最大值

函数返回值

如果返回为成功,将在lpFileName的 缓冲区当中返回相应模块的路径,如果所设的nSize过小,那么返回仅按所设置缓冲区大小返回相应字符串内容。

如果函数失败,返回值将为0,利用GetLastError可获得异常代码。

头文件
windows.h

2 GetPrivateProfileString()

函数原型

DWORD GetPrivateProfileString(lpszSection, lpszKey, lpszDefault,lpReturnedString, cchReturnBuffer, lpszFile)

Retrieves a string from the specified section in an initialization file.

从一个初始化文件中的指定的小节中获取一个字符串

函数参数

参数 类型及说明

DWORD WINAPI GetPrivateProfileString(
  _In_   LPCTSTR lpAppName,
  _In_   LPCTSTR lpKeyName,
  _In_   LPCTSTR lpDefault,
  _Out_  LPTSTR lpReturnedString,
  _In_   DWORD nSize,
  _In_   LPCTSTR lpFileName
);
lpAppName  [in]    The name of the section containing the key name. If this parameter is  NULL , the  GetPrivateProfileString  function copies all section names in the file to the supplied buffer.
lpKeyName  [in]     The name of the key whose associated string is to be retrieved. If this parameter is NULL, all key names in the section specified by the lpAppName parameter are copied to the buffer specified by the lpReturnedString parameter.
lpDefault [in]          A default string. If the lpKeyName key cannot be found in the initialization file, GetPrivateProfileString copies the default string to the lpReturnedString buffer. If this parameter is NULL, the default is an empty string, "".

Avoid specifying a default string with trailing blank characters. The function inserts a null character in the lpReturnedString buffer to strip any trailing blanks.

lpReturnedString [out]     A pointer to the buffer that receives the retrieved string.

nSize [in]   The size of the buffer pointed to by the lpReturnedString parameter, in characters.

lpFileName [in]    The name of the initialization file. If this parameter does not contain a full path to the file, the system searches for the file in the Windows directory.

函数返回值

 The return value is the number of characters copied to the buffer, not including the terminating   null  character.

If neither lpAppName nor lpKeyName is NULL and the supplied destination buffer is too small to hold the requested string, the string is truncated and followed by anull character, and the return value is equal to nSize minus one.

If either lpAppName or lpKeyName is NULL and the supplied destination buffer is too small to hold all the strings, the last string is truncated and followed by two nullcharacters. In this case, the return value is equal to nSize minus two.

In the event the initialization file specified by lpFileName is not found, or contains invalid values, this function will set errorno with a value of '0x2' (File Not Found). To retrieve extended error information, call GetLastError.

实例
函数返回值为string的长度(long型),而从ini 文件获得的 字符串则保留在目的缓冲器中DWORD GetPrivateProfileString(
LPCTSTR lpAppName, //配置 文件的section名
LPCTSTR lpKeyName, //配置 文件的key名
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);
其中各参数的意义:
前二个参数与   WritePrivateProfileString中的意义一样.
lpDefault : 如果INI 文件中没有前两个参数指定的 字段名或键名,则将此值赋给 变量.
lpReturnedString : 接收INI 文件中的值的CString对象,即目的缓存器.
nSize : 目的缓存器的大小.
lpFileName : 是完整的INI 文件名.
下面是一个常见的出错原因:
GetPrivateProfileString怎么总是读不出来
--------------------------------------------------------------------------------
*.INI内容
[NETWORK]
ServerIP=100.100.100.53
程序:
main()
{
char ip[16];
DWORD num=0;
num=GetPrivateProfileString("NETWORK","ServerIP","", ip,sizeof(ip), "Server.ini");
cout<<num<<endl<<ip<<endl;
}
--------
num=GetPrivateProfileString("NETWORK","ServerIP","",  ip,sizeof(ip),  "Server.ini");
Server.ini这个 文件放在哪里的?要放在与应用程序相同的目录下应该用".\\server.ini"
你看看是不是没有找到这个INI 文件
-----------
VC中调试时,server.ini放在工程目录中;程序单独运行时,则需要放在跟exe同一个目录中。
因为从VC里启动程序,VC将程序的工作目录初始化为工程目录,而不是debug或release目录本身。
 

转载于:https://my.oschina.net/u/1024767/blog/358122

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值