String return method

TailBite specific forum

Moderators: gnozal, ABBKlaus, lexvictory

User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8434
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

String return method

Post by netmaestro »

I've had a piece of code that I use all the time that is a list of Windows messages and their accompanying strings. The code used to run "If event = #WM_PAINT : m$+"WM_PAINT" : EndIf and it would return m$. This would, of course, not work with current versions of PB and Tailbite. I switched to including the file and calling it that way for several months, but I got tired of that. So I changed the code to work as a compiled library the same as it did before. This is what I did:

Code: Select all

Global  *strptr 
  
ProcedureDLL.i EventStr(event) 
  
  If Not *strptr
    *strptr = AllocateMemory(255)
  EndIf
  
  RtlZeroMemory_(*strptr, 255)
  
  *ptr = *strptr
  CopyMemoryString("",@*ptr)
  
  If event = #DM_SPECVERSION 
  CopyMemoryString("#DM_SPECVERSION ") 
  EndIf : If event = #DM_ORIENTATION 

  [...]

  ProcedureReturn *strptr

EndProcedure
And because this returns a pointer, I made the following compiled .res to go with it:

Code: Select all

Macro EventName(ev)
   PeekS ( EventStr(ev) )
EndMacro
So now when I type "Debug EventName(15)" I get "WM_PAINT" for output, just as I like it.

My question:

Is this the easiest way to overcome the string problem? Is there something simpler or better?
BERESHEIT
lexvictory
Addict
Addict
Posts: 1027
Joined: Sun May 15, 2005 5:15 am
Location: Australia
Contact:

Re: String return method

Post by lexvictory »

netmaestro wrote:and it would return m$. This would, of course, not work with current versions of PB and Tailbite.
why not?


The only problem with strings (to my knowledge) is if you do something like this

Code: Select all

ProcedureDLL.s EventStr(event) 
  ProcedureReturn SomeString$ ;(or "some string")
EndProcedure

ProcedureDLL.s SomeOtherProcDLLFunction()
  ProcedureReturn EventStr(123)
EndProcedure
Demonio Ardente

Currently managing Linux & OS X Tailbite
OS X TailBite now up to date with Windows!
Post Reply