2019年8月

最近做一个事情,实现一个流程交互,其中主交互流程函数中,涉及较多的内存申请,

而健康的函数,都是在函数退出前将手动申请不再需要的内存释放掉,

使用很多方法,都避免不了较多的出错分支时,一堆的if free/delete,代码长而且不好管理

因此,利用C++对象离开作用域会自动调用析构函数的特点,在这儿实现了两个自动释放内存的动态内存申请类

第一个类,只管理内存,不并管理对象

#include <vector>

classXAutoFreeMem
{
protected:
std::vector
<void*>vec_memorys_;public:
XAutoFreeMem::XAutoFreeMem() {};
virtual XAutoFreeMem::~XAutoFreeMem()
{
//释放对象时,释放管理的内存 for(auto item : vec_memorys_){free(item);
}
}
//通过此接口来申请内存 void* malloc_mem(unsigned intnsize)
{
void* ptr = malloc(nsize);if (nullptr !=ptr) {
vec_memorys_.push_back(ptr);
}
returnptr;
}
};

第二个类,能够同时支持内存管理、对象管理

typedef void (*delete_obj_func)(void*);class XAutoFreeObject : publicXAutoFreeMem
{
private:

typedef
structobject_manager_st
{
void*obj_this;
delete_obj_func delete_ptr;
}object_manager_st;
protected:
template
<typename T> static void free_object(T*p_this)
{
deletep_this;
}
template
<typename T> static void free_objects(T*p_this)
{
delete[]p_this;
}
protected:
std::vector
<object_manager_st>vec_objects_;public:
XAutoFreeObject::XAutoFreeObject() {};
virtual XAutoFreeObject::~XAutoFreeObject()
{
//释放对象时,释放管理的对象 for(auto item : vec_objects_){
(
*item.delete_ptr)(item.obj_this);
}
}
//对象//通过此接口来创建对象 template<typename T> void new_object(T**ppObj)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT;if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
*ppObj = (T*)(stObjMan.obj_this);return;
}
//通过此接口来创建对象 template<typename T, typename P> void new_object_with_param(T**ppObj, P param)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT(param);if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr = & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
*ppObj = (T*)(stObjMan.obj_this);return;
}
//通过此接口来创建对象,这几个接口使用会麻烦一些,使用示例:std::string* pstr = stAutoManager.new_object<std::string> (); template<typename T>T*new_object()
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT;if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
return (T*)(stObjMan.obj_this);
}
//通过此接口来创建对象 template<typename T, typename P>T*new_object_with_param(P param)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT(param);if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr = & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
return (T*)(stObjMan.obj_this);
}
//对象数组//通过此接口来创建对象数组 template<typename T> void new_objects(T** ppObj, intnum)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT[num];if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr =(delete_obj_func) & free_objects<T>;//保存之 vec_objects_.push_back(stObjMan);
}
*ppObj = (T*)(stObjMan.obj_this);return;
}
//通过此接口来创建对象数组 template<typename T, typename P> void new_objects_with_param(T** ppObj, intnum, P param)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT[num](param);if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr = & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
*ppObj = (T*)(stObjMan.obj_this);return;
}
//通过此接口来创建对象数组 template<typename T>T* new_objects(intnum)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT[num];if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr =(delete_obj_func) & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
return (T*)(stObjMan.obj_this);
}
//通过此接口来创建对象数组 template<typename T, typename P>T* new_objects_with_param(intnum, P param)
{
object_manager_st stObjMan;
stObjMan.obj_this
= newT[num](param);if (nullptr !=stObjMan.obj_this) {//取得函数指针 stObjMan.delete_ptr = & free_object<T>;//保存之 vec_objects_.push_back(stObjMan);
}
return (T*)(stObjMan.obj_this);
}
};

调用示例如下:

int main(int argc, char*argv[])
{
//cwSL3D_test_sum();//测试能否成功调用所有接口 XAutoFreeObject stAutoManager;char* strMem = (char*)stAutoManager.malloc_mem(100);

std::
string* pstr = stAutoManager.new_object<std::string>();

std::
string* pstr2 =nullptr;
stAutoManager.new_object(
&pstr2);
{
std::vector
<int>* pvec =nullptr;
stAutoManager.new_object(
&pvec);

std::vector
<int>* pvec2 =nullptr;
stAutoManager.new_objects(
&pvec, 2);
}
return 0;
}

 

头文件:CPictureEx用于显示一个等待动画

#pragma once#include"afxwin.h"#include"resource.h"#include"PictureEx.h"#include<thread>

//CWaitDlg 对话框
classCWaitSingleEvent {public:
CWaitSingleEvent()
: handle_(nullptr){
handle_
=CreateEvent(nullptr, TRUE, FALSE, nullptr);
}
~CWaitSingleEvent(){if(handle_) {
CloseHandle(handle_);
}
}
operator bool()const{return handle_ !=nullptr;
}
int Wait(constDWORD wait_time) {
DWORD retval
= 0;if(handle_) {
retval
=WaitForSingleObject(handle_, wait_time);
ResetEvent(handle_);

}
returnretval;
}
voidNotify() {if(handle_) {
SetEvent(handle_);
}
}
protected:
CWaitSingleEvent(
const CWaitSingleEvent&rhs){

}

CWaitSingleEvent
& operator=(const CWaitSingleEvent&rhs){return *this;
}
private:
HANDLE handle_;
};

typedef UINT32 (CDialogEx::
*WaitToExec)(void*param);class CWaitDlg : publicCDialogEx
{
DECLARE_DYNAMIC(CWaitDlg)
public:
CWaitDlg(CWnd
* pParent = NULL); //标准构造函数 virtual ~CWaitDlg();//对话框数据 enum { IDD =IDD_DIALOG4 };//设置执行等待函数 void SetWaitToExec(CWnd* pParent, void* pInnerParam, CDialogEx*pThis, WaitToExec pWaitToExec);private:void*exec_inner_param_;
CDialogEx
*dialog_this;
WaitToExec wait_to_exec_func_pt_;
volatile boolis_quit_thread_;
std::thread
*wait_to_exec_thread_;
CWaitSingleEvent wait_to_exec_condition_;
voidwait_to_exec_callback();protected:virtual void DoDataExchange(CDataExchange* pDX); //DDX/DDV 支持 DECLARE_MESSAGE_MAP()public:virtualBOOL OnInitDialog();
CPictureEx m_show_gif_;
virtual voidOnCancel();virtual voidOnOK();
};

代码实现:

//WaitDlg.cpp : 实现文件//#include"stdafx.h"#include"WaitDlg.h"#include"afxdialogex.h"

//CWaitDlg 对话框
IMPLEMENT_DYNAMIC(CWaitDlg, CDialogEx)

CWaitDlg::CWaitDlg(CWnd
* pParent /*=NULL*/)
: CDialogEx(CWaitDlg::IDD, pParent), wait_to_exec_func_pt_(nullptr), dialog_this(nullptr)
{
is_quit_thread_
= false;
exec_inner_param_
=NULL;
wait_to_exec_thread_
=nullptr;
}

CWaitDlg::
~CWaitDlg()
{
is_quit_thread_
= true;
wait_to_exec_condition_.Notify();
if (nullptr !=wait_to_exec_thread_){if(wait_to_exec_thread_->joinable()) {
wait_to_exec_thread_
->join();
}

wait_to_exec_thread_
=nullptr;
}
}
void CWaitDlg::DoDataExchange(CDataExchange*pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SHOW_GIF, m_show_gif_);
}


BEGIN_MESSAGE_MAP(CWaitDlg, CDialogEx)
END_MESSAGE_MAP()
//CWaitDlg 消息处理程序 BOOL GetResGifSize(long nResId, LPCTSTR name, long *lnWidth, long *lnHeight)
{
HRSRC hRsrc
=FindResource(NULL, MAKEINTRESOURCE(nResId), name);if (NULL ==hRsrc) {returnFALSE;
}

DWORD dwSize
=SizeofResource(NULL, hRsrc);
HGLOBAL hGlobal
=LoadResource(NULL, hRsrc);if (NULL ==hGlobal) {
CloseHandle(hRsrc);
returnFALSE;
}

unsigned
char* pBuffer = (unsigned char*)LockResource(hGlobal);if (NULL ==pBuffer) {
CloseHandle(hRsrc);
FreeResource(hGlobal);
returnFALSE;
}
//判断是否为GIF文件 if(pBuffer[0] != 0x47 && pBuffer[1] != 0x49 && pBuffer[2] != 0x46 && pBuffer[3] != 0x38){returnFALSE;
}
//读取宽高 for(DWORD i = 4; i < dwSize ; i++)
{
if(pBuffer[i] == 0x00 && pBuffer[i+1] == 0x2c)
{
*lnWidth = (pBuffer[i+7]<<8) | pBuffer[i+6];*lnHeight = (pBuffer[i+9]<<8) | pBuffer[i+8];
UnlockResource(hGlobal);
FreeResource(hGlobal);
returnTRUE;
}
}

UnlockResource(hGlobal);
FreeResource(hGlobal);
returnFALSE;
}

BOOL CWaitDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
//设置窗口大小 long nGifWidth = 75;long nGifHeight = 75;if (!GetResGifSize(IDR_WAIT_GIF_1, _T("GIF"), &nGifWidth, &nGifHeight)){
MessageBox(_T(
"内部错误"),_T("错误"), MB_OK |MB_ICONERROR);
}

RECT rt;
GetClientRect(
&rt);//判断窗口是否太大,或者太小,放不下 if ( (rt.right - rt.left - nGifWidth > 64) || (rt.bottom - rt.top - nGifHeight > 64) ||(rt.right- rt.left < nGifWidth) || (rt.bottom - rt.top <nGifHeight) ) {
RECT rtWin;
GetWindowRect(
&rtWin);//调整窗口大小 rtWin.right = rtWin.left + nGifWidth + 64;
rtWin.bottom
= rtWin.top + nGifHeight + 64;
MoveWindow(
&rtWin);//重新获取客户区大小 GetClientRect(&rt);
}

CenterWindow();
//计算位置 rt.left = (rt.right - rt.left - nGifWidth)/2;
rt.right
= rt.left +nGifWidth;
rt.top
= (rt.bottom - rt.top - nGifHeight)/2;
rt.bottom
= rt.left +nGifHeight;//转化成屏幕坐标//ClientToScreen(&rt); m_show_gif_.MoveWindow(&rt, TRUE);

SetBackgroundColor(RGB(
240,240,240), TRUE);
m_show_gif_.SetBkColor(RGB(
240,240,240));if (m_show_gif_.Load(MAKEINTRESOURCE(IDR_WAIT_GIF_1),_T("GIF"))){
m_show_gif_.Draw();
}
return TRUE; //return TRUE unless you set the focus to a control//异常: OCX 属性页应返回 FALSE }voidCWaitDlg::wait_to_exec_callback()
{
if(is_quit_thread_) {return;
}
while (!is_quit_thread_){//等待事件是否过来 wait_to_exec_condition_.Wait(INFINITE);if(is_quit_thread_) {break;}//判断参数是否OK if ((nullptr != dialog_this) && (nullptr !=wait_to_exec_func_pt_)){
(dialog_this
->*wait_to_exec_func_pt_)(exec_inner_param_);//执行完一次,那就隐藏一下 if (NULL !=m_hWnd)
{
ShowWindow(SW_HIDE);
}
}
}
}
//设置执行等待函数 void CWaitDlg::SetWaitToExec(CWnd* pParent, void* pInnerParam, CDialogEx*pThis, WaitToExec pWaitToExec)
{
exec_inner_param_
=pInnerParam;
dialog_this
=pThis;
wait_to_exec_func_pt_
=pWaitToExec;//通知线程执行 is_quit_thread_ = false;if (wait_to_exec_thread_ ==nullptr) {
wait_to_exec_thread_
= newstd::thread;
}
if(!wait_to_exec_thread_->joinable()) {*wait_to_exec_thread_ = std::move(std::thread(&CWaitDlg::wait_to_exec_callback, this));
}
//显示本窗口 if (NULL ==m_hWnd) {
Create(IDD_DIALOG4, pParent);
}

ShowWindow(SW_SHOW);
//触发事件 wait_to_exec_condition_.Notify();
}
voidCWaitDlg::OnCancel()
{
//TODO: 在此添加专用代码和/或调用基类//CDialogEx::OnCancel(); }voidCWaitDlg::OnOK()
{
//TODO: 在此添加专用代码和/或调用基类//暂时允许关闭 CDialogEx::OnOK();
}

使用示例:

typedef structset_xxx_info_st {intindex;
CString strXxxInfo;
}set_xxx_info_st;
voidCXXXParamDlg::OnClickedBtnWriteXxx()
{
CString strXxxC
= _T("strXxxC");//必须使用堆内存,调用会立即返回,但对象的使用,却在线程中 set_xxx_info_st* inn_param = newset_xxx_info_st;
inn_param
->index = m_set_cert_index_combo_.GetCurSel() + 1;
inn_param
->strXxxInfo.Append(strXxxC);//调用线程接口去获取 m_wait_dlg_.SetWaitToExec(GetParent(), inn_param, this, (WaitToExec)&CXXXParamDlg::WaitToExecSetXXXInfo);
}

UINT32 CXXXParamDlg::WaitToExecSetXXXInfo(
void*inn_param)
{
UINT32 ulRet
= -1;

set_xxx_info_st
* set_param = (set_xxx_info_st*)(inn_param);//禁止父窗口控件 GetParent()->GetParent()->EnableWindow(FALSE);//设置进去 ExecSetXXX(set_param->index, set_param->strXxxInfo);//禁止父窗口控件 GetParent()->GetParent()->EnableWindow(TRUE);if (nullptr !=set_param) {deleteset_param;
}
return 0;
}

 

#ifndef __03022006__WIN32INPUTBOX__#define __03022006__WIN32INPUTBOX__

/*This library is (c) Elias Bachaalany aka lallous <lallousx86@yahoo.com>
You may use this library under the following license agreement:

The zlib/libpng License.
---------------------------
This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.

Permission is granted to anyone to use this software for any purpose, including commercial applications,
and to alter it and redistribute it freely, subject to the following restrictions:

1. The origin of this software must not be misrepresented;
you must not claim that you wrote the original software.
If you use this software in a product, an acknowledgment in the product
documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such,
and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/#include<windows.h>#include<tchar.h> classCWin32InputBox;//Structure used to orient the inputbox behavior structWIN32INPUTBOX_PARAM
{
friend
classCWin32InputBox;// IN OPTIONAL boolbMultiline;//Pass this as none zero so to use this memory dlg template IN OPTIONAL LPVOID DlgTemplateData;//Pass this as none ZERO so to load DLGTEMPLATE from resources IN OPTIONAL LPCTSTR DlgTemplateName;//passing both "DlgTemplateName" and "DlgTemplateData" ZERO will cause//the dialog to use his default embedded resource//Center on monitor or owner window? IN OPTIONAL boolbCenter;//Want to add more styles to the dialog? IN OPTIONAL DWORD dwStylesPlus, dwStylesMinus;
IN OPTIONAL DWORD dwExStylesPlus, dwExStylesMinus;

IN LPCTSTR szTitle, szPrompt;
//Return buffer OUT LPTSTR szResult;
IN DWORD nResultSize;
//Owner window HWND hwndOwner;
HINSTANCE hInstance;
shortxPos, yPos;

WIN32INPUTBOX_PARAM();
private:
HWND hDlg;
};
classCWin32InputBox
{
private:
WIN32INPUTBOX_PARAM
*_param;staticLRESULT CALLBACK DlgProc(HWND, UINT, WPARAM, LPARAM);
HWND _hwndEditCtrl;
voidInitDialog();void SetParam(WIN32INPUTBOX_PARAM *);
WIN32INPUTBOX_PARAM
*GetParam();public:

CWin32InputBox(WIN32INPUTBOX_PARAM
*);~CWin32InputBox();static INT_PTR InputBoxEx(WIN32INPUTBOX_PARAM *);staticINT_PTR InputBox(
LPCTSTR szTitle,
LPCTSTR szPrompt,
LPTSTR szResult,
DWORD nResultSize,
bool bMultiLine = false,
HWND hwndParent
= 0);
};
#endif
#include "Win32InputBox.h"#include<stdio.h>

#pragma warning (disable: 4312)

/*History
----------
03/02/2006
- Initial version development

03/04/2006
- Lessened the complexity of the class, made it less generic (since its purpose is to be simple)
- Updated the dialog template and made OK button as default button
*/typedefstruct_MSDN_DLGTEMPLATEEX
{
WORD dlgVer;
WORD signature;
DWORD helpID;
DWORD exStyle;
DWORD style;
WORD cDlgItems;
shortx;shorty;shortcx;shortcy;
BYTE _rest[
1]; //rest of structure } MSDN_DLGTEMPLATEEX;static bool IsDlgTemplateExtended(DLGTEMPLATE *dlgTemplate)
{
MSDN_DLGTEMPLATEEX
*dgExTemplate = (MSDN_DLGTEMPLATEEX *) dlgTemplate;//MSDN excerpt://* dlgVer//Specifies the version number of the extended dialog box template. This member must be 1.//* signature//Indicates whether a template is an extended dialog box template.//If signature is 0xFFFF, this is an extended dialog box template.//In this case, the dlgVer member specifies the template version number.//If signature is any value other than 0xFFFF, this is a standard dialog box template that uses the DLGTEMPLATE and DLGITEMTEMPLATE structures. return (dgExTemplate->dlgVer == 1) && (dgExTemplate->signature == 0xFFFF);
}
//Use alignment if supported by the compiler #ifdef _MSC_VER#if _MSC_VER > 1200__declspec(align(4))#endif #endif //per the MSDN, the DLGTEMPLATE must be DWORD aligned//this was generated by the DlgResToDlgTemplate tool static unsigned char definputbox_dlg[] ={0x01,0x00,0xff,0xff,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xc8,0x00,0xc8,0x00,0x06,0x00,0x16,0x00,0x11,0x00,0xe7,0x00,0x6d,0x00,0x00,0x00,0x00,0x00,0x57,0x00,0x69,0x00,0x6e,0x00,0x33,0x00,0x32,0x00,0x49,0x00,0x6e,0x00,0x70,0x00,0x75,0x00,0x74,0x00,0x42,0x00,0x6f,0x00,0x78,0x00,0x00,0x00,0x08,0x00,0xbc,0x02,0x00,0x00,0x4d,0x00,0x53,0x00,0x20,0x00,0x53,0x00,0x68,0x00,0x65,0x00,0x6c,0x00,0x6c,0x00,0x20,0x00,0x44,0x00,0x6c,0x00,0x67,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x02,0x50,0x06,0x00,0x04,0x00,0x9d,0x00,0x21,0x00,0xe8,0x03,0x00,0x00,0xff,0xff,0x82,0x00,0x50,0x00,0x72,0x00,0x6f,0x00,0x6d,0x00,0x70,0x00,0x74,0x00,0x3a,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x81,0x50,0x06,0x00,0x25,0x00,0xd8,0x00,0x0e,0x00,0xe9,0x03,0x00,0x00,0xff,0xff,0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x84,0x10,0xa1,0x50,0x06,0x00,0x37,0x00,0xd8,0x00,0x31,0x00,0xea,0x03,0x00,0x00,0xff,0xff,0x81,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x03,0x50,0xab,0x00,0x04,0x00,0x33,0x00,0x0e,0x00,0x01,0x00,0x00,0x00,0xff,0xff,0x80,0x00,0x4f,0x00,0x4b,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x50,0xab,0x00,0x15,0x00,0x33,0x00,0x0e,0x00,0x02,0x00,0x00,0x00,0xff,0xff,0x80,0x00,0x43,0x00,0x41,0x00,0x4e,0x00,0x43,0x00,0x45,0x00,0x4c,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x40,0x00,0x00,0x27,0x00,0x08,0x00,0x08,0x00,0xff,0xff,0xff,0xff,0xff,0xff,0x82,0x00,0x00,0x00,0x00,0x00};static LPCTSTR definputbox_buttonnames[] = { _T("OK"), _T("CANCEL") };static const INT_PTR definputbox_buttonids[] ={ IDOK, IDCANCEL };static constINT
definputbox_id_prompt
= 1000,
definputbox_id_edit1
= 1001,
definputbox_id_edit2
= 1002;

WIN32INPUTBOX_PARAM::WIN32INPUTBOX_PARAM()
{
bMultiline
= false;
hwndOwner
= 0;
DlgTemplateName
= 0;
hInstance
= (HINSTANCE) ::GetModuleHandle(0);
DlgTemplateData
=definputbox_dlg;

bCenter
= true;

dwStylesPlus
= 0;
dwExStylesPlus
= 0;
dwStylesMinus
= 0xFFFFFFFF;
dwExStylesMinus
= 0xFFFFFFFF;

xPos
= yPos = -1;

szResult
= 0;
nResultSize
= 0;
}

CWin32InputBox::CWin32InputBox(WIN32INPUTBOX_PARAM
*param)
{
_param
=param;
}

CWin32InputBox::
~CWin32InputBox()
{

}
void CWin32InputBox::SetParam(WIN32INPUTBOX_PARAM *param)
{
_param
=param;
}

WIN32INPUTBOX_PARAM
*CWin32InputBox::GetParam()
{
return_param;
}

INT_PTR CWin32InputBox::InputBoxEx(WIN32INPUTBOX_PARAM
*param)
{
//Check mandatory parameters if (param->szResult == 0)
{
::SetLastError(ERROR_INVALID_PARAMETER);
return 0;
}

LPDLGTEMPLATE dlgTemplate;
if (param->DlgTemplateName != 0)
{
HMODULE hModule
= (HMODULE)param->hInstance;
HRSRC rcDlg
= ::FindResource(hModule, MAKEINTRESOURCE(param->DlgTemplateName), RT_DIALOG);if (rcDlg ==NULL)return 0;

HGLOBAL hglobalDlg
=::LoadResource(hModule, rcDlg);if (hglobalDlg ==NULL)return 0;

dlgTemplate
=(LPDLGTEMPLATE) hglobalDlg;
}
else if (param->DlgTemplateData != 0)
{
dlgTemplate
= (LPDLGTEMPLATE) param->DlgTemplateData;
}

MSDN_DLGTEMPLATEEX
*dlgTemplateEx =IsDlgTemplateExtended((LPDLGTEMPLATE) dlgTemplate)? (MSDN_DLGTEMPLATEEX *) dlgTemplate : 0;if (dlgTemplateEx != 0)
{
dlgTemplateEx
->exStyle |= param->dwExStylesPlus;
dlgTemplateEx
->style |= param->dwStylesPlus;
dlgTemplateEx
->exStyle &= param->dwExStylesMinus;
dlgTemplateEx
->style &= param->dwStylesMinus;if (param->bCenter)
dlgTemplateEx
->style |=DS_CENTER;if (param->xPos != -1)
dlgTemplateEx
->x = param->xPos;if (param->yPos != -1)
dlgTemplateEx
->y = param->yPos;
}
else{
dlgTemplate
->dwExtendedStyle |= param->dwExStylesPlus;
dlgTemplate
->style |= param->dwStylesPlus;
dlgTemplate
->dwExtendedStyle &= param->dwExStylesMinus;
dlgTemplate
->style &= param->dwStylesMinus;if (param->bCenter)
dlgTemplate
->style |=DS_CENTER;if (param->xPos != -1)
dlgTemplate
->x = param->xPos;if (param->yPos != -1)
dlgTemplate
->y = param->yPos;
}

CWin32InputBox inputbox(param);
//Resize dialog and SHOW or HIDE multiline INT_PTR r = ::DialogBoxIndirectParam(param->hInstance, dlgTemplate, param->hwndOwner, (DLGPROC)DlgProc, (LPARAM)&inputbox);returnr;
}

INT_PTR CWin32InputBox::InputBox(
LPCTSTR szTitle,
LPCTSTR szPrompt,
LPTSTR szResult,
DWORD nResultSize,
boolbMultiLine,
HWND hwndParent)
{
WIN32INPUTBOX_PARAM param;

param.szTitle
=szTitle;
param.szPrompt
=szPrompt;
param.szResult
=szResult;
param.nResultSize
=nResultSize;
param.bMultiline
=bMultiLine;return InputBoxEx(&param);
}
voidCWin32InputBox::InitDialog()
{
//Set the button captions for (size_t i=0;i<sizeof(definputbox_buttonids)/sizeof(definputbox_buttonids[0]);i++)
::SetDlgItemText(_param
->hDlg, (int) definputbox_buttonids[i], definputbox_buttonnames[i]);//Set other controls ::SetWindowText(_param->hDlg, _param->szTitle);
::SetDlgItemText(_param
->hDlg, definputbox_id_prompt, _param->szPrompt);

HWND hwndEdit1
= ::GetDlgItem(_param->hDlg, definputbox_id_edit1);
HWND hwndEdit2
= ::GetDlgItem(_param->hDlg, definputbox_id_edit2);if (_param->bMultiline)
_hwndEditCtrl
=hwndEdit2;else_hwndEditCtrl=hwndEdit1;

::SetWindowText(_hwndEditCtrl, _param
->szResult);

RECT rectDlg, rectEdit1, rectEdit2;

::GetWindowRect(_param
->hDlg, &rectDlg);
::GetWindowRect(hwndEdit1,
&rectEdit1);
::GetWindowRect(hwndEdit2,
&rectEdit2);if (_param->bMultiline)
{
::ShowWindow(hwndEdit1, SW_HIDE);
::SetWindowPos(
hwndEdit2,
HWND_NOTOPMOST,
rectEdit1.left
-rectDlg.left,
(rectEdit1.top
- rectDlg.top) - (rectEdit1.bottom -rectEdit1.top),0,0,
SWP_NOSIZE
|SWP_NOZORDER);

::SetWindowPos(
_param
->hDlg,
HWND_NOTOPMOST,
0,0,
rectDlg.right
-rectDlg.left,
rectDlg.bottom
- rectDlg.top - (rectEdit1.bottom -rectEdit1.top),
SWP_NOMOVE);

}
else{
::SetWindowPos(
_param
->hDlg,
HWND_NOTOPMOST,
0,0,
rectDlg.right
-rectDlg.left,
rectEdit1.bottom
- rectDlg.top + 5,
SWP_NOMOVE);

::ShowWindow(hwndEdit2, SW_HIDE);
}
}
//Message handler for about box. LRESULT CALLBACK CWin32InputBox::DlgProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
CWin32InputBox
*_this = (CWin32InputBox *) ::GetWindowLong(hDlg, GWL_USERDATA);
WIN32INPUTBOX_PARAM
*param = _this ? _this->GetParam() : 0;switch(message)
{
caseWM_INITDIALOG:
{
::SetWindowLong(hDlg, GWL_USERDATA, (LONG) lParam);

_this
= (CWin32InputBox *) lParam;
_this
->_param->hDlg =hDlg;
_this
->InitDialog();returnTRUE;
}
caseWM_COMMAND:
{
#ifdef _MY_DEBUG
CHAR buf[
1024];static int i=0;
sprintf(buf,
"WM_COMMAND: %09d wParam=%08X lParam=%08X\n", i++, wParam, lParam);
OutputDebugString(buf);
#endifINT_PTR buttonId=LOWORD(wParam);for (size_t i=0;
i
<sizeof(definputbox_buttonids)/sizeof(definputbox_buttonids[0]);
i
++)
{
if (buttonId ==definputbox_buttonids[i])
{
::GetWindowText(
_this
->_hwndEditCtrl,
_this
->_param->szResult,
_this
->_param->nResultSize);

::EndDialog(hDlg, buttonId);
returnTRUE;
}
}
}
break;
}
returnFALSE;
}

 

代码来源:https://www.codeproject.com/Articles/13330/Using-Dialog-Templates-to-create-an-InputBox-in-C

#include <windows.h>#include<stdio.h>

int main(int argc, char *argv[])
{
printf(
"DlgResToDlgTemplate v1.0 (c) <lallousx86@yahoo.com>\n\n");if (argc < 4)
{
printf(
"usage: %s MODULE_FILE DLG_ID FILE.H\n", argv[0]);return -1;
}
int dlgID = atoi(argv[2]);

HMODULE hModule;
hModule
= ::LoadLibraryEx(argv[1], 0, LOAD_LIBRARY_AS_DATAFILE);if (hModule ==NULL)
{
printf(
"Could not load module '%s'\n", argv[1]);return -1;
}

HRSRC hrsrc
=::FindResource(hModule, (LPCTSTR) dlgID, RT_DIALOG);if (hrsrc ==NULL)
{
printf(
"Cannot find dialog resource with id '%d'\n", dlgID);return -2;
}

HGLOBAL hglobalRsrc
=::LoadResource(hModule, hrsrc);if (hglobalRsrc ==NULL)
{
printf(
"Cannot load resource!\n");return -3;
}
char *buf = (char *)::LockResource(hglobalRsrc);if (buf ==NULL)
{
printf(
"Cannot lock resource!\n");return -4;
}

DWORD sz
=::SizeofResource(hModule, hrsrc);

FILE
*fp = fopen(argv[3], "wb");if (fp ==NULL)
{
printf(
"Could not open '%s' for writing\n", argv[3]);return -5;
}
char temp[100];

sprintf(temp,
"static unsigned char dlg_%d[] = \n{\n\t", dlgID);
fwrite(temp,
1, strlen(temp), fp);

sz
--;for (DWORD i=0;i<=sz;i++)
{
sprintf(temp,
"0x%02x", (unsigned char)(buf[i] & 0xFF));
fwrite(temp,
1, strlen(temp), fp);//don't append a comma if it was last byte if (i !=sz)
{
fwrite(
",", 1, 1, fp);
}
if (i && (i % 16 == 0))
fwrite(
"\n\t", 1, 2, fp);
}

strcpy(temp,
"\n};\n");
fwrite(temp,
1, strlen(temp), fp);

fclose(fp);
::FreeLibrary(hModule);

printf(
"generated %s (%d bytes) successfully!\n", argv[3], sz);return 0;
}

 

1.生成KEY:
gmssl sm2 -genkey -out 01.root.pem
gmssl genrsa -out 01.root_plain.key 2048
gmssl genrsa -out 01.root_plain.key 1024

2.生成CA的自签名证书
gmssl req -new -x509 -key 01.root.pem -out 01.root.crt
gmssl req -new -x509 -days 365 -key 01.root_plain.key -out 01.root.crt

3.根据私钥生成证书签名请求
gmssl req -new -key 02.first_node.pem -out 02.first_level.csr
gmssl req -new -key 02.firstnode_plain.key -out 02.first_level.csr

4.用一开始生成的CA进行签名(一级证书用根证书签名,二级证书用一级证书签名)
gmssl ca -in 02.first_level.csr -out 02.first_level.crt -cert 01.root.crt -keyfile 01.root.pem
gmssl ca -in 02.first_level.csr -out 02.first_level.crt -cert 01.root.crt -keyfile 01.root_plain.key

5.查看自签名的证书
gmssl x509 -in 01.root.crt -noout -text

6.验证证书是否合法
6.1 一级证书的验证
gmssl verify -verbose -x509_strict -CAfile 01.root.crt 02.first_level.crt
6.2 二级直至多级证书的验证