场景: 我有一个 C++ DLL。 在这个 DLL 中,我创建了一个工作线程。 在工作线程中,我有一个循环等待用户通过 USB 硬件设备输入。 只有当 USB 设备上的用户输入满足某些条件时,循环才会结束。 另外,我需要将USB设备的用户使用反馈实时反馈到屏幕上。 它使用 Delphi GUI 进行反馈。
当用户使用USB 设备时,Windows 系统会产生一个回调函数。此回调函数写在同一个 C++ DLL 文件中,并作为参数传入 USB 设备的初始化函数。
我在 DLL 中使用一个全局变量作为标志来确定何时必须退出此循环。
我还从 Delphi DLL 加载这个 C++ DLL。 Delphi DLL -> C++ DLL 反馈显示来自Delphi DLL。
基本上,我现在面临的问题是函数ptr,funcptr,根本调用不了。屏幕上没有实时反馈。这是 Delphi DLL 中的一个函数。 这是代码行:
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
有人对此有解决方案吗?
我是新手,非常感谢任何答案。感谢您的帮助。
//Global variable
BOOL flag = TRUE;
//A function type in Delphi calling app
typedef void (__stdcall *FUNCPTR)(PCHAR);
//Functions start here.....
DWORD WINAPI ThreadProc(LPVOID lpParameter)
{
do {} while (flag);
}
function_1st_CalledFromDelphiDLL(FUNCPTR funcPtr)
{
Initialize_USBDevice(handleUSBDeviceEvent_callback, funcPtr);
}
function_2nd_CalledFromDelphiDLL()
{
DWORD threadID;
HANDLE hWorkerThread;
hWorkerThread = CreateThread(NULL,0,ThreadProc, 0, 0 , &threadID);
if (hWorkerThread!=NULL)
{
WaitForSingleObject(hWorkerThread, 30000);
}
}
//This is the callback function, called by Windows system when user meddles with the USB device
handleUSBDeviceEvent_callback(void *funcPtr)
{
flag = FALSE; //so loop in ThreadProc can exit
//The following code cannot be executed at all. Even when i Try MessageBox( NULL,L"msg",NULL,NULL), the message box doesn't popup too. But, I can write something to a filestream here.
(*(reinterprete_cast<FUNCPTR>(funcPtr)))("this is the feedback msg displayed on Delphi GUI");
}
最佳答案
首先,我不建议使用变量在线程之间进行通信。为了您的目的,使用一个事件。
你的动态链接库:
HANDLE _exitNow = NULL;
HANDLE _queueLock = NULL; // for part 2 below
// call this from your main at start
bool DLL_Initialize()
{
_exitNow = CreateEvent(...);
_queueLock = CreateMutex(...);
... initialize your device, add the callback ...
}
// call this from your main at exit
void DLL_Shutdown()
{
SetEvent(_exitNow);
}
// your worker thread
void DLL_Worker()
{
// use options so WaitFor...() does not block
int result = WaitForSingleObject(_exitNow, ...);
if(result indicates _exitNow was fired)
{
CloseHandle(_exitNow);
... shutdown sequence, unhook your callback, deinit your device ...
CloseHandle(_queueLock);
}
}
这会处理 init/shutdown/worker 位。现在是困难的部分。
首先,您无法从工作线程中操作 UI 位。我不记得确切的原因——它与主线程拥有的 Windows 消息队列的所有权有关。如果您所要做的只是显示一些应该更新的内容,您应该执行以下操作:
假设以上,其余代码变为...(请注意,我已经有一段时间没有这样做了,所以名称和声明可能略有偏差,但原理是一样的)
你的主程序:
// double check how to do this exactly. I haven't done this in a long time.
const CUSTOM_WINDOW_EVENT = WINDOWS_CUSTOM_MESSAGE + [SOMETHING];
// check for proper syntax
function Form.CustomHandler: Integer; handles CUSTOM_WINDOW_EVENT;
var
S: String;
begin
S := GetDataFromDLL();
... update display based on S ...
end;
你的动态链接库:
const CUSTOM_WINDOW_EVENT = WINDOWS_CUSTOM_MESSAGE + [SOMETHING];
TQueue queue; // find a suitable type. std::queue<> works fine
// delphi will call this
<String-type> DLL_GetStatus()
{
... wait on mutex using WaitForSingleObject() ...
... read one entry from queue ...
... release mutex ...
... return it ...
}
void PutStatus(String statusData)
{
... wait on mutex using WaitForSingleObject() ...
... write to queue ...
... release mutex ...
... push the custom message to the windows message queue, use PostMessage() IIRC ...
}
<whatever> handleUSBDeviceEvent_callback(void *funcPtr)
{
... read device, compose status data ...
PutStats(statusData);
}
我是靠内存完成所有这些工作的,所以我敢肯定会有什么地方不对劲。希望您无论如何都能掌握这些原则。
关于c++ - 从工作线程调用主线程回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3350375/
我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-
我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我花了三天的时间用头撞墙,试图弄清楚为什么简单的“rake”不能通过我的规范文件。如果您遇到这种情况:任何文件夹路径中都不要有空格!。严重地。事实上,从现在开始,您命名的任何内容都没有空格。这是我的控制台输出:(在/Users/*****/Desktop/LearningRuby/learn_ruby)$rake/Users/*******/Desktop/LearningRuby/learn_ruby/00_hello/hello_spec.rb:116:in`require':cannotloadsuchfile--hello(LoadError) 最佳
我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re
关闭。这个问题需要detailsorclarity.它目前不接受答案。想改进这个问题吗?通过editingthispost添加细节并澄清问题.关闭8年前。Improvethisquestion在首页我有:汽车:VolvoSaabMercedesAudistatic_pages_spec.rb中的测试代码:it"shouldhavetherightselect"dovisithome_pathit{shouldhave_select('cars',:options=>['volvo','saab','mercedes','audi'])}end响应是rspec./spec/request
在Rails4.0.2中,我使用s3_direct_upload和aws-sdkgems直接为s3存储桶上传文件。在开发环境中它工作正常,但在生产环境中它会抛出如下错误,ActionView::Template::Error(noimplicitconversionofnilintoString)在View中,create_cv_url,:id=>"s3_uploader",:key=>"cv_uploads/{unique_id}/${filename}",:key_starts_with=>"cv_uploads/",:callback_param=>"cv[direct_uplo
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser
我正在尝试用ruby中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了