1、C中调用C+中的代码这样定义会是安全的。一般的考虑跨平台使用方法如下:#ifdefined(_cplusplus)|defined(c_plusplus) /跨平台定义方法extern #endif/. 正常的声明段#ifdefined(_cplusplus)|defined(c_plusplus)#endif 简单的用在windows下可以如下定义:#ifdef _cplusplusextern /. 正常的声明段#endiftypedef int LONG32, *PLONG32;typedef int INT32, *PINT32;这跟宏定义差不多,学名是“类型定义”。C+为了跟C区别就
2、定义了很多这类大写的类型名LONG UINT INT等等。当然同时支持C的小写类型名。Int long等。例如:typedef unsigned int ULONG32, *PULONG32;typedef unsigned int DWORD32, *PDWORD32;typedef unsigned int UINT32, *PUINT32;另:对于32,64位数据类型,目前多数是32位使用的比较多。64位基本上没有。因为32位基本上满足了我们的需求。64位的宏开关是这样写的:#ifdef _WIN64 typedef _int64 INT_PTR, *PINT_PTR;typedef u
3、nsigned _int64 UINT_PTR, *PUINT_PTR;指针类型和整形类型之间的转换函数定义:_inlineunsigned longHandleToUlong( void *h ) return(unsigned long) h );PtrToUlong( void *p return(unsigned long) p );unsigned shortPtrToUshort( return(unsigned short) p );longPtrToLong( return(long) p );shortPtrToShort( return(short) p );#endif#
4、pragma warning(3:4311) / type cast truncation#elsetypedef long INT_PTR, *PINT_PTR;typedef unsigned long UINT_PTR, *PUINT_PTR;#define MAXINT_PTR (0x7fffffffL) 指针类型的数据最大值#define MININT_PTR (0x80000000L) 指针类型的数据最小值#define MAXUINT_PTR (0xffffffffUL) 指针类型的数据最大值typedef unsigned short UHALF_PTR, *PUHALF_PT
5、R; short短无符号整形定义的短指针类型(小指针)typedef short HALF_PTR, *PHALF_PTR;short短有符号整形定义的短指针类型(小指针)#define MAXUHALF_PTR 0xffff 短指针类型的最大值#define MAXHALF_PTR 0x7fff 短指针类型的最大值#define MINHALF_PTR 0x8000 短指针类型的最小值/函数形式的宏定义#define HandleToUlong( h ) (ULONG) (h) ) #define PtrToUlong( p ) (ULONG) (p) )#define PtrToLong(
6、 p ) (LONG) (p) )#define PtrToUshort( p ) (unsigned short) (p) )#define PtrToShort( p ) (short) (p) )inline 的函数必须和函数定义一起用才起作用, 而且调用方必须能够看到这个函数的定义, 如:/ a.hinline void foo(void);/ a.cinline void foo(void)/ b.c#include a.hvoid bar(void)foo();这种用法 inline 是不起任何作用的, 只有这样:这样才能真正的内联; 内联是在编译期间处理的, 第一种情况, 如果编
7、译器编译 b.c 的时候, 只看到 foo 的声明, 而看不到其定义, 编译器就没办法把 foo 函数内联到调用处!所谓的内联就是把函数里面的代码直接拷贝到执行的代码里面再编译,而不是按着函数实现体来编译。(以上是basetsd.h文件。Stdafx.h文件是用来包含标准头文件的,一般C+的函数找不到时就是因为头文件没有包含进来。)总结:宏定义本身其实就是替换掉代码里的宏名为定义体。只是置换的作用。函数形式的宏定义用来实现复杂的一些代码形式。比如参数类型的互换,C+重载功能的宏实现等。 VC+教材第二课MFC类库介绍CWinApp类:应用程序入口类,一般从这里开始执行代码。先简单介绍一下C+类
8、头文件H和实现体文件CPP基本结构。1,头文件里有针对类的宏定义/ test.h : main header file for the TEST application/#if !defined(AFX_TEST_H_67369212_1733_4E81_8D0C_4EB216B09392_INCLUDED_)#define AFX_TEST_H_67369212_1733_4E81_8D0C_4EB216B09392_INCLUDED_#if _MSC_VER 1000#pragma once#endif / _MSC_VER #ifndef _AFXWIN_H_ #error includ
9、e stdafx.h before including this file for PCHresource.h / main symbols/ CTestApp:/ See test.cpp for the implementation of this classclass CTestApp : public CWinApppublic: CTestApp();/ Overrides / ClassWizard generated virtual function overrides /AFX_VIRTUAL(CTestApp) public: virtual BOOL InitInstanc
10、e(); /AFX_VIRTUAL/ Implementation /AFX_MSG(CTestApp) / NOTE - the ClassWizard will add and remove member functions here. / DO NOT EDIT what you see in these blocks of generated code ! /AFX_MSG DECLARE_MESSAGE_MAP();/AFX_INSERT_LOCATION/ Microsoft Visual C+ will insert additional declarations immedia
11、tely before the previous line.#endif / !2,实现体文件/ test.cpp : Defines the class behaviors for the application.stdafx.htest.htestDlg.h#ifdef _DEBUG#define new DEBUG_NEW#undef THIS_FILEstatic char THIS_FILE = _FILE_;/ CTestAppBEGIN_MESSAGE_MAP(CTestApp, CWinApp) /AFX_MSG_MAP(CTestApp) / NOTE - the Class
12、Wizard will add and remove mapping macros here. / DO NOT EDIT what you see in these blocks of generated code! ON_COMMAND(ID_HELP, CWinApp:OnHelp)END_MESSAGE_MAP()/ CTestApp constructionCTestApp:CTestApp() / TODO: add construction code here, / Place all significant initialization in InitInstance/ The
13、 one and only CTestApp objectCTestApp theApp;/ CTestApp initializationBOOL CTestApp:InitInstance() if (!AfxSocketInit() AfxMessageBox(IDP_SOCKETS_INIT_FAILED); return FALSE; AfxEnableControlContainer(); / Standard initialization / If you are not using these features and wish to reduce the size / of
14、your final executable, you should remove from the following / the specific initialization routines you do not need.#ifdef _AFXDLL Enable3dControls(); / Call this when using MFC in a shared DLL Enable3dControlsStatic(); / Call this when linking to MFC statically CTestDlg dlg; m_pMainWnd = &dlg; int n
15、Response = dlg.DoModal(); if (nResponse = IDOK) / TODO: Place code here to handle when the dialog is / dismissed with OK else if (nResponse = IDCANCEL) / dismissed with Cancel / Since the dialog has been closed, return FALSE so that we exit the / application, rather than start the applications messa
16、ge pump. return FALSE;3,CWinApp的InitInstance函数:应用程序的事例初始化函数,包括初始化代码和初期窗口显示代码。/建立模式对话框这是对话框模式的代码。 CSingleDocTemplate* pDocTemplate;/文档模版的创建 pDocTemplate = new CSingleDocTemplate( IDR_MAINFRAME, RUNTIME_CLASS(CViewDoc), RUNTIME_CLASS(CMainFrame), / main SDI frame window RUNTIME_CLASS(CViewView); AddDo
17、cTemplate(pDocTemplate);/解析SHELL命令行 / Parse command line for standard shell commands, DDE, file open CCommandLineInfo cmdInfo; ParseCommandLine(cmdInfo);/执行SHELL命令行 / Dispatch commands specified on the command lineProcessShellCommand(cmdInfo)/显示窗口 / The one and only window has been initialized, so show and update it. m_pMainWnd-ShowWindow(SW_SHOW);UpdateWindow();这是文档模式的代码。
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1