DLL中的共享字符串 [英] Shared string inside DLL

查看:82
本文介绍了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)。


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

解决方案

Lindsay写道:

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

声明字符串:

#pragma bss_seg(" MYBSS")
WCHAR * wFilenames;
#pragma bss_seg()
#pragma comment(链接器,&/; SECTION: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" ;;" ;);
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


Lindsay写道:

我需要我的dll的所有实例之间共享的字符串。另外,我需要
将字符串附加到dll的每个新实例。
[...]
我很难过,但我确定我错过了但是我不知道是什么。




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

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

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

其他进程的存在。


V


是的,我确实知道。但是我的问题并不是关于DLL的问题,而是因为你知道为什么我这样做了。我只想知道

为什么我附加字符串的例行工作无效。


" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:Mg ************* @ newsread1.mlpsca01.us.to.veri o.net ...

Lindsay写道:

我需要在我的dll的所有实例之间共享一个字符串。另外,我需要在dll的每个新实例附加字符串。
[...]
我很难过,但我确定我错过了但是我不知道是什么。



你已经错过了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.

解决方案

Lindsay wrote:

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


Lindsay wrote:

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.
[...]
I''m stumped but I''m sure I''ve just missed something but I don''t know what.



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


Yes I did know that. But my question was not so much about the DLL as that
was mentioed just so you knew why I was doing this. I only wanted to know
why my routine for appending the string was not working.

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:Mg*************@newsread1.mlpsca01.us.to.veri o.net...

Lindsay wrote:

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.
[...]
I''m stumped but I''m sure I''ve just missed something but I don''t know
what.



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中的共享字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆