Wrox Programmer Forums
|
Visual C++ Questions specific to Microsoft's Visual C++. For questions not specific to this Microsoft version, use the C++ Programming forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual C++ section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 4th, 2003, 08:36 AM
Registered User
 
Join Date: Jun 2003
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default Shared data in DLL

I am employing a system-wide CBT hook that needs to respond to certain Windows messages and to inform a single instance of a 'Handler'. The hook works fine globally but of course each instance of the DLL (i.e. for each process it gets injected into) has its own 'Handler'.

I have tried to make the Handler object 'shared' through the following code:

#pragma comment(linker, "/SECTION:.SHARED,RWS")
#pragma data_seg(".SHARED")
HHOOK hHookCbt = 0;
Handler handler;
#pragma data_seg()


This doesn't seem to work though (I believe that objects may not be sharable, only primitive data types?) so perhaps file mapping might be the answer.

Does anybody have any ideas and/or suggestions on how to go about this please?

Thanks,

Chris.
 
Old June 5th, 2003, 08:58 AM
Registered User
 
Join Date: Jun 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What you mean by not working?
Could you post some compilable code?

Josué Andrade Gomes
 
Old March 30th, 2006, 08:06 AM
Registered User
 
Join Date: Mar 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

According to
http://msdn2.microsoft.com/en-US/library/h90dkhs0(VS.80).aspx

the data in shared section have to be initialized. It means STATICALLY assigned in source code, the compiler inserts this initialization directly to the compiled DLL. So you cannot share objects this way...



-----------------
http://www.ubytujnas.cz
 
Old April 18th, 2006, 11:29 AM
Registered User
 
Join Date: Apr 2006
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You can share objects in a shared section, but you have to be sneaky to do it by using placement new:

Code:
#pragma comment(linker, "/SECTION:.SHARED,RWS")
#pragma data_seg(".SHARED")
HHOOK hHookCbt = 0;
static BYTE buffer[sizeof(Handler)] = {0};
#pragma data_seg()
// each process gets its own copy of the pointer:
static Handler* pHandler = NULL;
...
// In the first process to load the dll, create the single shared handler using placement new:
pHandler = new (buffer) Handler;
// subsequent processes just cast the pointer:
pHandler = reinterpret_cast<Handler*>(buffer);
The destructor won't get called automatically using this mechanism, so if you need it to run, call it explicitly when the last process exits:
Code:
pHandler->~Handler();
Ian





Similar Threads
Thread Thread Starter Forum Replies Last Post
Missing System.Data.SQLClient dll mike_abc VB.NET 2 June 3rd, 2011 10:18 PM
Shared Data source Vs connection on report level mverma4you Reporting Services 0 June 6th, 2007 01:47 AM
Can't Reference System.Data.dll (ch 2, ex 1) Moonpile BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 January 8th, 2007 02:44 PM
xmlhttp requesting data from a dll in .net millennium Classic ASP XML 0 June 23rd, 2004 07:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.