一个更为简单易用的THUNK类

C++中的Thunk技术和它的使用场景 阅读详情

    利用THUNK技术回调类成员函数,是一项前几年很热门的技术,网上的资料和示例代码也很多,但这些代码普遍存在一些差强人意的地方,要么缺乏通用性,需要针对不同的回调函数作调整,要么使用起来不方便,需要在应用代码里写上三、四行以上不相关(甚至有些莫名其妙)的代码或宏,不便于代码的阅读和维护。

    针对这些缺点,我编写了一个更为简单易用的使用THUNK技术的回调函数类,类代码和示例如下:

  

    第一个应用实例:派生一个回调无参数类成员函数的定时器的类。

 

    由此实例可以看出,读者在使用CThunkBase基类时,只需要两行语句,一是public CThunkBase,二是GetFuncAddr。是不是简单明了啊?

    然后,我们再来看一下基于CThunkBase派生的一个简单的窗口类及其调用实例:

 

    同第一个派生类一样,也只需要两行代码,public CThunkBase和GetFuncAddr。其实,在多重派生里CThunkBase运行结果依然正确。

    由于水平有很,代码中难免存在漏洞,欢迎指正。

C++ Thunk技术(初学版) 今天看到一篇关于Thunk技术的文章,特转过来。这里想说的是:代码中的关键点为用指令jmp pFunc跳转到你想要执行的函数pFunc。指令“jmp xxxx”占5个字节,代码中用了个一字节对齐的结构体struct Thunk ,当然也可以用 unsigned char code[5]; 说另一个关键点就是地址计算了,jmp xxxx指令用了相对跳转地址,相对地址 = 要跳转函数的 阅读详情

相关推荐

Thunk技术

NULL 博文链接:https://mengdejun.iteye.com/blog/932762

C++ Thunk技术

一. 使用回调函数时遇到的问题 在使用Win32 API(如CreateThread等)时我们经常需要传入回调函数,这个回调函数不能是类的成员函数,只能是友元函数或静态函数。为了在回调函数中访问某个类的成员变量或函数,我们通常不得不通过某种方式将该类的This指针传入到回调函数中,从而实现在回调函数中访问类的公有的成员变量或函数。 如果有一种方式可以实现将类的成员函数作为回调函数来使用,那么就...

while(1) { smile(); } 5万+

Thunk 代码

Thunk,你懂的,这么简单,就没写示例了。

Thunk 回调函数实现面向对象

回调函数实现面向对象 什么是回调函数? 通常,Windows均要求我们将消息处理函数定义为一个全局函数,或者是一个类中的静态成员函数。并且该函数必须采用__stdcall的调用约定(Calling Convention) 在VC中函数前有callback,就认为是回调函数,回调函数是__stdcall调用约定,参数是从右边开始压入栈的,所以第一个参数就在栈的最上层 在使用VC开发的时候,回

嬴政 2263

Shopee App加密算法逆向实战:从libshpssdk.so到x-sap-ri的生成机制

本文详细解析了Shopee App核心加密参数x-sap-ri的生成机制。通过逆向分析libshpssdk.so动态库,揭示了其采用9套算法动态切换的复杂签名逻辑,并分享了从抓包定位、Frida动态调试到Unidbg模拟执行的全过程实战经验,为移动应用安全研究提供了完整的技术路径。

weixin_29323551的博客 1337

CANoe实战:手把手教你用J1939.dbc发送超8字节长帧报文(附完整CAPL代码)

本文详细解析了使用CANoe和J1939.dbc实现超8字节长帧报文的分包发送技术,提供完整的CAPL代码实现与优化策略。内容涵盖J1939.dbc配置、分包发送模块设计、接收端处理逻辑,并与ISO 15765-2协议进行深度对比,帮助开发者构建高效可靠的商用车通信解决方案。

weixin_29169899的博客 401

C++中的Thunk技术 / 非静态类成员函数作为回调函数 的实现方法

  原文:https://blog.twofei.com/616/   用我的理解通俗地解释一下什么是C++中的Thunk技术吧!  Thunk技术就是申请一段可执行的内存, 并通过手动构造CPU指令的形式来生成一个小巧的, 具有明确作用的代码块.  小巧? 具有明确作用? 你曾经初学C++时, 如果我没猜错的话, 肯定尝试过用C++封装一个窗口类(因为我也尝试过 :-) ),在封装窗口类的时候...

weixin_34144450的博客 641

VisionMaster 4.2.0安装

按照顺序安装。

XuXing_java的专栏 3931

C++多重继承的多态 - Thunk

1 class A {...}2 3 class B {...}4 5 class C: public A, public B {...}6 7 B* b = new C;8 9 b->foo(); //virtual function 在为line 9实现多态的时候,b指向的不是C对象的开头,而是其subobject B的开头。 因此在调用foo时,作为参数的this指针需...

weixin_30402085的博客 266

Label Studio + SAM2分割半自动标注与yolo11训练

Label Studio + SAM2分割半自动标注与yolo11训练

schFallingCloud 1417

Basler相机和pylon viewer 的使用

文章目录1 线路连接2 安装和使用pylon 1 线路连接 电源线接到电源插座,网线接到电脑(电脑没有网口的话,可用转换器转到type-c接口)。 2 安装和使用pylon 参考 Basler pylon安装及对网口相机基本设置 相机和电脑以太网IP设置参考:balser相机IP设置 关于相机参数和pylon viewer的详细设置,可以参考pylon Viewer安装路径下的文档(Documentation) ...

Deephome的博客 1万+

从一个简单异步打印时间手写题到thunk和generator

generator函数是ES6推出的新特性之一,在三年前我刚入门前端的时候,在看到generator的时候还是很迷糊,工作已经三年了用到的次数指可数,更多的时候只是看一下基础用法还有属性方法等。简单来说generator就是一个可以暂停函数内部执行的一个方法,这个方法返回一个迭代器对象,在函数内部有很多yield表达式,只有当调用了这个生成器方法返回的迭代器对象的next方法,生成器内部才会将指针指向下一个yield表达式。本文重点不是generator所以不过多展开了。

qq_45279180的博客 1211

第二大脑的觉醒:一个寄生在代码里的小生命,如何悄无声息地接管我的工作流

所有记忆都存在本地。

步子哥的博客 1069

打造自己的MFC:thunk技术实现窗口类的封装

MFC功能已经非常强大,自己做界面库也许没什么意思,但是这个过程中却能学到很多东西。比如说: 窗口类的封装,从全局窗口消息处理到窗口对象消息处理的映射方法:     对界面进行封装,一般都是一个窗口一个类,比如实现一个最基本的窗口类CMyWnd,你一定会把窗口过程作为这个类的成员函数,但是使用WINAPI创建窗口时必须注册类WNDCLASS,里面有个成员数据lpfnWndProc需要WND

把握自己。 1577

高通平台WIFI学习-- 基于6GHZ Discovery (STA Scanning)讲解

本文介绍了6GHz频段的网络扫描优化方案。主要内容包括:1)带内发现机制,将6GHz信道分为优先扫描信道(PSC)和非PSC信道,规范了STA发送探测请求的条件及等待时间;2)带外发现机制,通过RNR信息元素实现跨频段AP发现,减少6GHz频段干扰;3)具体扫描流程示例,包括DBS扫描和敏捷扫描的顺序安排。方案通过优化探测请求发送规则和跨频段信息共享,有效降低了信道污染和扫描延迟,日志数据验证了6GHz信道扫描的具体实现过程。

专业开发者的博客,一个从事Android WiFi和蓝牙的工程师 273

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

简单说明redux-thunk的作用

是什么 redux-thunk是react的一个applyMiddleware(中间件),你肯定经常在入口js文件中看到如下代码 const store = createStore(todoApp,applyMiddleware(thunk) 有什么用 一个action中进行多次的dispatch 一个action中做其他任何函数能做的事情(reducer因为必须是 pure function所以不能做很多事情) import {SHOWALL,SHOWNOTHING,ADDTODO} from '.

qq_42339350的博客 866

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

redux-thunk的简单使用

thunk 我们常常需要在redux中实现异步的数据获取等操作,这个时候就可以用到thunk 1、安装thunk yarn add redux-thunk 2、创建store的时候引入中间件和配置 import {createStore,applyMiddleware} from 'redux' import reducer from './reducer' import thunk from 'redux-thunk' let middleWares = applyMiddleware(thunk)

Angeladaddy007的博客 1023

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

c++ thunk

<br />之前参加了一次面试,问到了SetTimer中回调设计成类成员函数的问题.后来想写一下,但是一直没有找到相关文章.后来lori哥指点,原来这就是C++ Thunk技术.<br /> <br />Thunk : 将一段机器码对应的字节保存在一个连续内存结构里, <br />然后将其指针强制转换成函数. 即用作函数来执行.<br /><br />这份代码用于将C语方的回调函数转换为具有this指针的C++成<br />员函数.<br /><br />在Windows系统编程中, 可以将一些系统回函数封

Johny的专栏 7362

Thunk——形实转换程序

今天,第一次玩博客,先转载一篇优秀的文章,分享给大家。以后会陆续地将收集到的优秀文章转载到这儿,和大家一起学习。等有能力了,再贴出自己的原创作品。 今天的主题是Thunk——形实转换程序。 研究Thunk的起因,是因为在IDA中时常会看到Attributes : thunk,它经常被标准在某些函数的头部。 至于Thunk出现的原因及作用,下面这篇文章已经讲述的十分详细

实践是检验真理的唯一标准&贵在坚持 1987

Redux thunk | 什么是thunk?在React项目中怎么使用Redux-Thunk?

Redux-thunk 什么是thunk? thunk可以理解为函数的另一种称呼,但不是以前我们说的简单函数。Thunk代表的是一个被其他函数返回的函数。 function wrapper_function() { // this one is a "thunk" because it defers work for later: return function t...

qq_35536883的博客 1388

Thunk技术封装好的win32项目窗口类

由于windows API写窗口有点麻烦而单调,而

hhhhhhhhhhkkkkkkkkkk的专栏 1784

c++ THUNK技术

这里想说的是:代码中的关键点为用指令jmp pFunc跳转到你想要执行的函数pFunc。 指令“jmp xxxx”占5个字节,代码中用了个一字节对齐的结构体struct Thunk , 当然也可以用 unsigned char code[5]; 说另一个关键点就是地址计算了,jmp xxxx指令用了相对跳转地址, 相对地址 = 要跳转函数的地址 - “jmp xxxx”指令的下一

ghevinn欢迎您光临 2450

C++ 通过Thunk在WNDPROC中访问this指针实现细节

本文代码使用了一些C++11特性,需要编译器支持。本文仅讨论x86_64平台的相关实现,x86平台理论上只需修改 thunk 相关机器码即可。 THUNK的原理参见之前的一篇博文《C++ 通过Thunk在WNDPROC中访问this指针》 首先定义我们的window类,该类实现对一个Win32窗口句柄的封装。 该类将在构造函数中创建窗口,在析构时销毁窗口; 窗口的消息过程函数(Window

Bad Alloc 1665

Thunk 应用

Thunk 应用 作者:RobertBaker 在网上看到很多 Thunk 的介绍,但是很少有几个实际的 Demo,这里用一个实例说明Thunk的用法。 以下是所使用的 Thunk。(CAuxStdThunk, 从网上找的,不知道作者,如有冒犯还请原谅。) // AUX Thunk, 全局函数 -> 类成员函数 #pragma pack(push, 1) template cla

RobertBaker的专栏 1567

C++ 通过Thunk在WNDPROC中访问this指针

本文基本只讨论原理,具体实现请参见后续文章《C++ 通过Thunk在WNDPROC中访问this指针实现细节》 当注册窗口类时,WNDCLASSEX结构的lpfnWndProc成员应设置为窗口过程函数的地址,这是一个C风格的函数指针,所以我们只能使用全局或静态函数的地址,这在我们将窗口封装为C++类时会很麻烦,因为我们无法在一个全局或静态的WindowProc函数中直接访问类实例,这就需要一些手

Bad Alloc 1468
上一篇: VC如何将资源中包含的位图释放成文件
下一篇: 如何用VB实现半透明控件
lyserver
博客等级 码龄23年 492粉丝 91原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值