SNBs 
Author Message
 SNBs

Hi,

Does anyone know how to correctly allocate the memory associated with
SNBs?  I'm trying to use the CopyTo command, but there is very little
help that I have found either on the web or on MSDN (July - 2000) in
regard to the SNB parameter.  The way the documentation reads is that
you have an array of pointers to strings (wide char I presume) followed
by a NULL pointer followed by the actual strings.  Would the following
be correct?  Please bear in mind that I know the actual pointer values
may not be exactly correct.  I'm looking more for the procedure of how
to do it right now.

Memory Location       Value
0x10000               0x1000c
0x10004               0x10024
0x10008               NULL
0x1000c               l"Some string"
0x10024               l"Another string"

Thanks,
Brian

Sent via Deja.com http://www.*-*-*.com/
Before you buy.



Wed, 26 Feb 2003 07:20:02 GMT  
 SNBs

What is "SNB" anyway?

--
Dan Evens
Standard disclaimers etc. No spam please.


Quote:
> Does anyone know how to correctly allocate the memory associated with
> SNBs?

[snip]


Fri, 28 Feb 2003 23:00:47 GMT  
 SNBs

Greets,

    That appears to be proper way of constructing the SNB.  The SNB (String
Name Block for Dan's reference) is one large contiguous chunk of memory (at
least for the efficient purposes of performing one allocation when
marshalling them) that initially contains a list of pointer addresses,
followed by a NULL pointer, at which point the remainder of the memory in
the SNB contains the actual string data.

Regards,

Joe

Quote:

> Hi,

> Does anyone know how to correctly allocate the memory associated with
> SNBs?  I'm trying to use the CopyTo command, but there is very little
> help that I have found either on the web or on MSDN (July - 2000) in
> regard to the SNB parameter.  The way the documentation reads is that
> you have an array of pointers to strings (wide char I presume) followed
> by a NULL pointer followed by the actual strings.  Would the following
> be correct?  Please bear in mind that I know the actual pointer values
> may not be exactly correct.  I'm looking more for the procedure of how
> to do it right now.

> Memory Location       Value
> 0x10000               0x1000c
> 0x10004               0x10024
> 0x10008               NULL
> 0x1000c               l"Some string"
> 0x10024               l"Another string"

> Thanks,
> Brian

> Sent via Deja.com http://www.deja.com/
> Before you buy.



Fri, 28 Feb 2003 23:17:34 GMT  
 SNBs
Thanks for the response.  It is the proper way of setting it up, but it
was a royal pain to do dynamically!  Chances are good that if you don't
want to use a single allocated block of memory, it will work just fine
by having an array of pointers to WCHAR blocks of memory separately
allocated.  The implementation would be much simpler.  By keeping
everything in one block, you have the wonderful fortune of keeping
track of all those pointer locations when adding new items.  I used
realloc.  When this fella is used the starting address of the newly
allocated block isn't necessarily going to be what it was to start.
So, all your pointers after the realloc call could be pointing to the
wrong place.

Here's what I wound up doing if anyone is interested.  I intentionally
left out the constructor/destructor and data initialization routines.

I have no doubt that this could have been coded cleaner.  I was in a
hurry.

class CDlg
{
  void TestSNB();
  bool AddSNB(WCHAR *text);

  BYTE *snb_;
  long snbSize_;
  int snbEntries_;

Quote:
}

void CDlg::TestSNB()
{
  if (snb_)
    free(snb_);

  snb_ = NULL;

  snb_ = (BYTE*)realloc(snb_, sizeof(char *));
  ASSERT(snb_);
  // snb_ can easily be type cast to (SNB)snb_

  if (!snb_)
    return ERROR;

  // start off with an empty SNB block
  memset(snb_, 0, sizeof(char *));
  snbSize_ = sizeof(char *);
  snbEntries_ = 0;

  AddSNB(L"TestTextOne");
  AddSNB(L"Test Text Two");

Quote:
}

bool CDlg::AddSNB(WCHAR *text)
{
  DWORD offset;
  DWORD origOffset = (DWORD)snb_;

  int i;

  if (!text)
    return false;

  // determine offset for new string

  offset = snbSize_ + sizeof(char *);

  // set up size of new block of memory.
  snbSize_ += sizeof(char *) + (wcslen(text) + 1 ) * sizeof(WCHAR);

  snb_ = (BYTE*)realloc(snb_, snbSize_);

  snbEntries_++;

  long copyToOffset = snbEntries_ * sizeof(char *);
  long copySize = snbSize_ - copyToOffset - sizeof(char *);

  memcpy(snb_ + copyToOffset, snb_ + copyToOffset - sizeof(char *),
         copySize);

  // modify all the existing pointers to point to the correct text.
  // realloc may have changed the location of the text.
  for (i=0; i<snbEntries_ - 1; i++)
  {
    DWORD *snbEntryOffset = (DWORD *)(snb_ + (i * sizeof(char *)));
    *snbEntryOffset = abs(origOffset - *snbEntryOffset);
    *snbEntryOffset += sizeof(char *);
    *snbEntryOffset += (DWORD)snb_;
    memcpy((snb_ + (i * sizeof(char *))), snbEntryOffset,
           sizeof(DWORD));
  }

  offset += (DWORD)snb_;

  memcpy((snb_ + ((snbEntries_ - 1) * sizeof(char *))), &offset,
         sizeof(DWORD));

  wcscpy((WCHAR*)(offset), text);

  return true;

Quote:
}



Quote:

> Greets,

>     That appears to be proper way of constructing the SNB.  The SNB
(String
> Name Block for Dan's reference) is one large contiguous chunk of
memory (at
> least for the efficient purposes of performing one allocation when
> marshalling them) that initially contains a list of pointer addresses,
> followed by a NULL pointer, at which point the remainder of the
memory in
> the SNB contains the actual string data.

> Regards,

> Joe




- Show quoted text -

Quote:
> > Hi,

> > Does anyone know how to correctly allocate the memory associated
with
> > SNBs?  I'm trying to use the CopyTo command, but there is very
little
> > help that I have found either on the web or on MSDN (July - 2000) in
> > regard to the SNB parameter.  The way the documentation reads is
that
> > you have an array of pointers to strings (wide char I presume)
followed
> > by a NULL pointer followed by the actual strings.  Would the
following
> > be correct?  Please bear in mind that I know the actual pointer
values
> > may not be exactly correct.  I'm looking more for the procedure of
how
> > to do it right now.

> > Memory Location       Value
> > 0x10000               0x1000c
> > 0x10004               0x10024
> > 0x10008               NULL
> > 0x1000c               l"Some string"
> > 0x10024               l"Another string"

> > Thanks,
> > Brian

> > Sent via Deja.com http://www.deja.com/
> > Before you buy.

Sent via Deja.com http://www.deja.com/
Before you buy.


Mon, 03 Mar 2003 00:57:25 GMT  
 
 [ 4 post ] 

 Relevant Pages 
 

 
Powered by phpBB® Forum Software