本文介绍了DLL中的共享字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的dll的所有实例之间共享一个字符串。另外,我需要

来附加每个新的dll实例的字符串。

声明字符串:


# pragma bss_seg(" MYBSS")

WCHAR * wFilenames;

#pragma bss_seg()

#pragma comment(链接器," /部分:MYBSS,rws")


有一个例程,每个实例只调用一次,它接收

另一个字符串附加到wFilenames:


QUICKEDITDLL bool STDCALL AddFilename(LPWSTR fn)

{

int s =(lstrlenW((LPCWSTR)wFilenames)+ lstrlenW((LPCWSTR) )fn)+2)* sizeof(WCHAR);

//加2来实现分号和空字符

WCHAR * nS =(WCHAR *)malloc( s);

ZeroMemory(nS,s);

lstrcpyW(nS,wFilenames);

//免费(wFilenames);

wFilenames =(WCHAR *)realloc(wFilenames,s);

ZeroMemory(wFilenames,s);

lstrcpyW(wFilenames,nS);

lstrcatW(wFilenames,fn);

lstrcatW(wFilenames,L& quot ;;");

MessageBoxW(NULL,wFilenames,NULL,MB_OK);

free(nS);

返回true;

}


wFilenames只持有最后一个字符串(fn)。


我很难过但是我肯定我错过了一些东西,但我不知道是什么。

解决方案




嗯,这是用于clc ++的OT(特定于Win32)但是:我相信

的问题是那个,当你的指针wFilenames被共享时,它b / b
指向的内存不是。你需要做更多的事情:


#pragma bss_seg(" MYBSS")

WCHAR wFilenames [65536]; //< - 或一些适当的空间

#pragma bss_seg()

#pragma comment(链接器,&/; SECTION:MYBSS,rws")


-

Mike Smith





你已经错过了C ++中没有DLL的事实(不知怎的,我想是

你实际上知道,这不是你第一次来这里)。请在

新闻组中询问您的操作系统或编译器(或两者)。 AFAIK,它通常是可能的b $ b,但它涉及的是其标准格式的C ++中没有的机制。在
C ++中没有定义进程之间的内存共享,因为C ++程序模型不承认

其他进程的存在。


V




你已经错过了C ++中没有DLL的事实(不知何故,我认为你/你实际上知道,这不是你第一次来这里。请在
新闻组中询问您的操作系统或编译器(或两者)。 AFAIK,它通常是可能的,但它涉及其标准形式的C ++中没有的机制。在C ++中没有定义进程之间的内存共享,因为C ++程序模型不承认存在其他进程。

V



I need a string to be shared between all instances of my dll. Also, I need
to append the string with each new instance of the dll.
The string is declared:

#pragma bss_seg("MYBSS")
WCHAR* wFilenames;
#pragma bss_seg()
#pragma comment (linker,"/SECTION:MYBSS,rws")

There is a routine which is called once only per instance which recieves
another string to append to wFilenames:

QUICKEDITDLL bool STDCALL AddFilename(LPWSTR fn)
{
int s=(lstrlenW((LPCWSTR)wFilenames)+lstrlenW((LPCWSTR )fn)+2)*sizeof(WCHAR);
// Plus 2 to accomedate a semicolon and a null char
WCHAR* nS=(WCHAR*)malloc(s);
ZeroMemory(nS,s);
lstrcpyW(nS,wFilenames);
//free(wFilenames);
wFilenames=(WCHAR*)realloc(wFilenames,s);
ZeroMemory(wFilenames,s);
lstrcpyW(wFilenames,nS);
lstrcatW(wFilenames,fn);
lstrcatW(wFilenames,L";");
MessageBoxW(NULL,wFilenames,NULL,MB_OK);
free(nS);
return true;
}

wFilenames only ever holds the last string ( fn ).

I''m stumped but I''m sure I''ve just missed something but I don''t know what.

解决方案



Well, this is OT for c.l.c++ (being Win32-specific) but: I believe the
problem is that, while your pointer wFilenames is shared, the memory it
points to is not. You need to do something more like:

#pragma bss_seg("MYBSS")
WCHAR wFilenames[65536]; // <- or some appropriate amount of space
#pragma bss_seg()
#pragma comment (linker,"/SECTION:MYBSS,rws")

--
Mike Smith




You''ve missed the fact that there are no DLLs in C++ (and somehow I think
you actually knew that, it''s not your first time here). Please ask in the
newsgroup that deals with your OS or your compiler (or both). AFAIK, it
is generally possible but it involves mechanisms not available in C++ in
its standard form. Memory sharing between processes is not defined in
C++, since the C++ program model does not acknowledge the existence of
other processes.

V




You''ve missed the fact that there are no DLLs in C++ (and somehow I think
you actually knew that, it''s not your first time here). Please ask in the
newsgroup that deals with your OS or your compiler (or both). AFAIK, it
is generally possible but it involves mechanisms not available in C++ in
its standard form. Memory sharing between processes is not defined in
C++, since the C++ program model does not acknowledge the existence of
other processes.

V



这篇关于DLL中的共享字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 15:04