CString in a DLL Shared Data Segment

Posted on 1999-12-25
Medium Priority
Last Modified: 2013-11-20
I'm creating a DLL with a shared data segment using the #pragma directive.

I'm trying to share a CString object among other variables. The problem is that the CString object resets its contents everytime the DLL injects itself into a process's address space.

I would like the CString object (I've also tried a char array) to maintain its' contents from process to process.

Is there a way to do this?
Thanks in advance.
Question by:shark351
  • 3
  • 2
LVL 11

Accepted Solution

mikeblas earned 280 total points
ID: 2306115
You can't.

First: A CString copies its data as it constructs (or is assigned), and the constructor will run each time the DLL is inserted into a new process.

Second: and the data will be allocated on a process-local heap, and can't be shared.

You need to use a more sensible and appropriate interprocess communication mechanism.

..B ekiM

Author Comment

ID: 2306592
OK, as mentioned I also tried using a regular char array which obviously will not re-construct itself like a CString will.
1) Why is the char array being reset also?
2) What alternatives do I have to maintain a buffer from process to process?
3) You say that the data is allocated on a process local heap. That's fine but the global data is defined in the DLL module as SHARED,RWS. In Jeffery Richter's "Advanced Windows" he shows how to use this method to share data among all instances of a DLL. Why won't a char array work in this case?
LVL 11

Expert Comment

ID: 2306758
I didn't read in your message that you said a character array was failing, or that you wanted to use it.

1) The character array shouldn't be reset, if you've declared it correctly. You've probably not declared it correctly, though it's also possible that you're not properly testing the value of the array or that you're actually doing something to change the content of the array when you didn't expect it. You've not expressed any specific symptoms or shown any code. With nothing to go on, all I can do is guess.

2) Write a function in your DLL which manages a dynamically-allocated block of memory which is shared across processes (that is, allocated with the GMEM_SHARED flag). This access mechanism also gives you a convenient construct for gating access to the data for the multiple threads which will be touching it.

3) I didn't say a character array wouldn't work in this case. A character array isn't a CString; I was referring to CStrings, specifically. In my post, I never mentioned character arrays.

Apparently, you already have the sample in the Richter book--my book has a sample of this technique, too.  How shall we proceed? Do you want to post your project and have me diagnose it?

..B ekiM

Author Comment

ID: 2306896

I'll post the appropriate code below.
The GetBuffer() function is called by an application that I created which simply gets a copy of the shared DLL buffer.
KeyboardProc is the callback for the Keyboard Hook. It is in this function that I keep adding characters to the buffer.
You'll notice that there is a CString and a char array declared in the shared section. I've been experimenting with both types.
Please let me know if you see a problem.


DLL Header File:
#define LIBFUNC __declspec(dllexport)
// Exportable Functions
LIBFUNC void GetBuffer(char*);

// Implementation

BOOL UnsetKeyboardHook();

#pragma data_seg("SHARDATA")
static HWND g_hWnd=NULL;
static HHOOK g_hHookKeyboard=NULL;
static CString g_strBuffer;
static char g_szBuf[256];
#pragma data_seg()

// Instruct the Linker to make SHARDATA
// readable, writable & shared
#pragma comment(linker, "-SECTION:SHARDATA,RWS")

void GetBuffer(char* c)
      strcpy(c, g_szBuf);
LVL 11

Expert Comment

ID: 2307203
Yeah, you've coded it wrong. You've got:
#pragma data_seg("SHARDATA")
// ...
static char g_szBuf[256];
#pragma data_seg()

and that doesn't put anything in the SHARDATA section. Since g_szBuf is uninitialized, the compiler puts it into the .bss section with all the other uninitalized global data. You want to make sure everything you want to be in SHARDATA is initialized. For this example, you should've coded:

#pragma data_seg("SHARDATA")
// ...
static char g_szBuf[256] = { 0 };
#pragma data_seg()

The initialization makes g_szBuf appear in the SHARDATA section.

..B ekiM

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question