Visual C++ 2011-5-24
2011-05-24 20:21 Clingingboy 阅读(535) 评论(0) 编辑 收藏 举报
一.公共对话框
文档很详细
http://msdn.microsoft.com/en-us/library/ms646954(VS.85).aspx
二.获取操作系统版本
OSVERSIONINFO osvi;
ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
GetVersionEx(&osvi);
三.AfxIsValid函数'
1.AfxIsValidAddress
Tests any memory address to ensure that it is contained entirely within the program's memory space.
// Allocate a 5 character array, which should have a valid memory address.
char* arr = new char[5];
// Create a null pointer, which should be an invalid memory address.
char* null = (char*)0x0;
ASSERT(AfxIsValidAddress(arr, 5));
ASSERT(AfxIsValidAddress(null, 5));
2.AfxIsValidString
Use this function to determine whether a pointer to a string is valid.
// Create a character string which should be valid.
char str[12] = "hello world";
// Create a null pointer, which should be an invalid string.
char* null = (char*)0x0;
ASSERT(AfxIsValidString(str, 12));
ASSERT(!AfxIsValidString(null, 5));
四.钩子的类型
http://www.cppblog.com/sleepwom/archive/2010/02/03/107094.html
五.c的函数多参数类型
即用在函数中用…标识多个未知参数,然后用va_list,va_start,va_arg,va_end指令来读取数据.读取时的数据顺序和类型不可以错乱
void testit ( int i, ...)
{
va_list argptr;
va_start(argptr, i);
int n = va_arg( argptr, int );
char *s = va_arg( argptr, char* );
va_end(argptr);
}
int main()
{
testit( 0, 10,"aaa" );
}
参考:http://blog.csdn.net/mader/archive/2007/08/20/1751893.aspx
六.Shell Path函数
1.PathRemoveFileSpec
Removes the trailing file name and backslash from a path, if they are present.
#include <windows.h>
#include <iostream.h>
#include "Shlwapi.h"
void main( void )
{
// Path to include file spec.
char buffer_1[ ] = "C:\\TEST\\sample.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Print the path with the file spec.
cout << "The path with file spec is : " << lpStr1 << endl;
// Call to "PathRemoveFileSpec".
PathRemoveFileSpec(lpStr1);
// Print the path without the file spec.
cout << "\nThe path without file spec is : " << lpStr1 << endl;
}
OUTPUT:
==================
The path with file spec is : C:\TEST\sample.txt
The path without file spec is : C:\TEST
其他函数:http://msdn.microsoft.com/zh-cn/library/bb773559(en-us,VS.85).aspx
七.注册表读取项
1.用RegQueryInfoKey函数读取一个项的相关信息,大部分为输出变量,可以根据需要获取,不需要的设置为NULL
// Get the class name and the value count.
retCode = RegQueryInfoKey(
hKey, // key handle
achClass, // buffer for class name
&cchClassName, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&cbMaxSubKey, // longest subkey size
&cchMaxClass, // longest class string
&cValues, // number of values for this key
&cchMaxValue, // longest value name
&cbMaxValueData, // longest value data
&cbSecurityDescriptor, // security descriptor
&ftLastWriteTime); // last write time
2.遍历项键值
2.1遍历key
for (i=0; i<cSubKeys; i++)
{
cbName = MAX_KEY_LENGTH;
retCode = RegEnumKeyEx(hKey, i,
achKey,
&cbName,
NULL,
NULL,
NULL,
&ftLastWriteTime);
if (retCode == ERROR_SUCCESS)
{
_tprintf(TEXT("(%d) %s\n"), i+1, achKey);
}
}
2.2遍历值
for (i=0, retCode=ERROR_SUCCESS; i<cValues; i++)
{
cchValue = MAX_VALUE_NAME;
achValue[0] = '\0';
retCode = RegEnumValue(hKey, i,
achValue,
&cchValue,
NULL,
NULL,
NULL,
NULL);
if (retCode == ERROR_SUCCESS )
{
_tprintf(TEXT("(%d) %s\n"), i+1, achValue);
}
}
八.预处理命令的使用技巧
1.下面的Release只有动态的时候才知道,这犹如动态语言,但与动态语言不同的是,在编译过程中检测出了错误,这实际上是一个展开的过程的.
#define SAFE_RELEASE(p) { if(p) { p.Release(); (p)=NULL; } }
2.动态更改变量,AddNum给改变了i变量,那么i就是一个变量约定
#define AddNum() { i++;}
int main( void )
{
int i=0;
AddNum();
}
3.用宏调用函数,记得是函数,不是bool变量,这实质跟c#的委托是一个概念的用法
#define SAFE_FUN(p) { p(); }
bool test()
{
return 1;
}
int main( void )
{
SAFE_FUN(test);
}
4.用宏跳转
一般都不推荐使用挑战,但个人感觉这种跳转方式很好
#define GOTO_IF_(type, expr, func, e_val, s_val, var, label) \
do \
{ \
type temp = (expr); \
if( func(temp) ) \
{ \
var = e_val; \
goto label; \
} \
else var = s_val; \
} while ( false )
#define GOTO_IF_FALSE(expr) GOTO_IF_(bool, expr, !, E_FAIL, S_OK, hr, Exit)
bool test()
{
return 1;
}
int main( void )
{
HRESULT hr;
GOTO_IF_FALSE(test());
Exit:
return hr;
}
九.资源的加载
MAKEINTRESOURCE是加载本地程序的资源,可以理解为当调用LoadString时,第一个参数是NULL的,所以MAKEINTRESOURCE不能用于多国语言
//加载DLL
HINSTANCE hModule = LoadLibrary(_T("test.dll"));
if (hModule == NULL)
{
AfxMessageBox(_T("test.dll加载失败\n"));
return;
}
//加载字符串资源
CString strText = _T("");
if (::LoadString(hModule, 1000, strText.GetBuffer(256), 256) != 0)
{
//设置标题
SetWindowText(strText);
}
...
参考:http://www.cnblogs.com/liuweijian/archive/2011/01/20/1940665.html
http://blog.csdn.net/John_Yang/archive/2010/05/02/5549400.aspx
十.使用DLL中的MFC对话框
在DLL中必须调用以下代码
AFX_MANAGE_STATE(AfxGetStaticModuleState());