QT跟VC++结合来进行插件的验证机制

欢迎技术交流和帮助,提供IT相关服务,如果有需要,请联系博主QQ: 503587937

由于最近公司要开发一个以C++插件机制为主的,主要有一个问题就是C++的二进制兼容性的问题。
一旦类使用虚函数,只要随便改动下增删查改下头文件的虚函数,就会导致程序在跑的时候进行乱跳,因为这个时候exe跟dll里面的vtable模型是不一致的

刚好程序是使用QT开发了,所以就用了两种方式来保证头文件一致才能进行程序的加载

1. 利用QT的MOC机制
QT的MOC机制里面有一个Q_INVOKABLE的宏定义,可以让moc生成类成员函数的相关信息,
然后利用QObject里面的method来获取对应的函数参数和函数顺序
用于保证虚函数的顺序,参数一致
具体查看QObject的相关文档
http://doc.qt.io/qt-5/qobject.html
http://doc.qt.io/qt-5/qmetaobject.html
http://doc.qt.io/qt-5/qmetamethod.html
从qobject里面或者metaobject再获取metamethod,即可获取指定的函数信息

2. 遍历vtable,保证虚函数的个数一致。如何获取vtable的在网上有很多文章,下面主要是针对VS2015的编译器代码

下面代码主要做的事情:
1. 保证传入的程序有虚函数
2. 保证T不指针类型
3. 获取vtable
4. 遍历vtable一直遇到0

template <class T>
int vtableLength(T &object)
{
    static_assert(std::is_polymorphic<T>::value,
                  "at least has one virtual function");
    static_assert(!std::is_pointer<T>::value,
                  "type not allow pointer");

    int* vptr = (int*)&object;
    int* vtable = (int*)*vptr;
    int len = 0;
    for (int i = 0; vtable[i] != 0; i++)
    {
        len++;
    }
    return len;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你的程序加密过OD MD5值支持二次开发使用 防破解验证也可实现一机一码 VC++ 开发 STARTUPINFO startup; PROCESS_INFORMATION process; CString g_strCompanyName1 = ""; CString g_strCompanyName2 = ""; CString g_strCompanyName3 = ""; CString g_strCompanyName = "**"; BOOL CTaiShanApp::InitInstance() { AfxEnableControlContainer(); //#ifdef ZJH m_gMessageID = ::RegisterWindowMessage("WsSendMessageHqData"); CFileFind fnd; if(S_OK != ::CoInitialize (NULL)) return FALSE; // ReadDiskIDPartCwd(); // if(!FyRegister::IsValidUser()) // return FALSE; memset( &startup, 0, sizeof( startup ) ); startup.cb = sizeof( startup ); memset( &process, 0, sizeof( process ) ); if(fnd.FindFile ("WsSendMessageShare.exe")) { m_gbUseExe = true; } hAppMutex=::CreateMutex(NULL,TRUE,m_pszExeName); if(GetLastError() == ERROR_ALREADY_EXISTS) { CWnd *pPrevWnd = CWnd::GetDesktopWindow()->GetWindow(GW_CHILD); while(pPrevWnd) { if(::GetProp(pPrevWnd->GetSafeHwnd(),m_pszExeName)) { if(pPrevWnd->IsIconic()) pPrevWnd->ShowWindow(SW_RESTORE); pPrevWnd->SetForegroundWindow(); pPrevWnd->GetLastActivePopup()->SetForegroundWindow(); return false; } pPrevWnd = pPrevWnd->GetWindow(GW_HWNDNEXT); } return false; } //#endif #ifndef _NET_AUTHEN HMODULE hModule; hModule = LoadLibrary("ide21201.dll"); if (hModule==NULL) { AfxMessageBox("Can't find ide21201.dll"); return FALSE; } char *(WINAPI * GetIdeSerial)(); GetIdeSerial = (char *(WINAPI *)())GetProcAddress(hModule, "GetIdeSerial"); if (GetIdeSerial==NULL) { AfxMessageBox("Can't find GetIdeSerial in ide21201.dll"); return FALSE; } CString strSerialNumber;// = SERIAL_NUMBER; strSerialNumber = GetIdeSerial(); strSerialNumber.TrimLeft(" "); if (strSerialNumber.Compare(SERIAL_NUMBER)!=0) { AfxMessageBox("序列号错误"); return FALSE; } #else CDlgLogin dlgLogin; int nResponse = dlgLogin.DoModal(); if (nResponse!=1) return FALSE; #endif /* CDialogShowInformation dlg; dlg.DoModal();*/ int nResult; m_bAppAuthorized=TRUE; // Standard initialization // If you are not using these features and wish to reduce the size // of your final executable, you should remove from the following // the specific initialization routines you do not need. CTaiTestSplash *m_splash; BOOL SplashOpen=FALSE; m_splash = new CTaiTestSplash; SplashOpen=m_splash->Create(); if( SplashOpen ) m_splash->ShowWindow(SW_SHOW); DWORD Currenttime=GetTickCount(); BeginWaitCursor(); #ifdef TEST_USER1 t = CTime::GetCurrentTime(); CTime t2 = g_timeUseEnd; if(t >= t2) { // AfxMessageBox("试用期已过,若想继续使用,请购买正式版!",MB_OK | MB_ICONSTOP); return false; } else 以上为部分代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值