How to return a string from a UserLibrary

Everything else that doesn't fall into one of the other PB categories.
remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

How to return a string from a UserLibrary

Post by remi_meier »

I/We tried in VC++ and found this way, is it correct?

Code: Select all

#include <windows.h> 	
	
extern "C" TCHAR* __stdcall SYS_GetOutputBuffer(int,int); 	
	
extern "C" TCHAR* PB_GetString() 	
{ 	
  TCHAR *Buffer; 	
  Buffer = SYS_GetOutputBuffer(4,0); 	
  strcpy(Buffer,"Bubi"); 	
  return Buffer; 	
}
SYS_GetOutputBuffer(len, parameter)? We didn't understand the description
in the readme...
And how does a string as parameter work? Is it the same in thread safe
mode?

Thx in advance!
Athlon64 3700+, 1024MB Ram, Radeon X1600
jack
Addict
Addict
Posts: 1358
Joined: Fri Apr 25, 2003 11:10 pm

Post by jack »

it looks godd to me, but you are missing

Code: Select all

*Buffer = 0;  // Ensure the null byte is written
though as your snippet stands, not sure it's unicode ready.
from the example in the Library SDK

Code: Select all

/*
 * String return example
 */

#include "Simple.h"


/* We use the 'TCHAR' type, which is a 'char' in ascii and a 'short' in unicode mode
 * so we don't have to do 2 versions of the command.
 * When defining the UNICODE constant, all Windows API switch to unicode functions
 * automatically.
 *
 * To test if your string function works correctly, always test it in an expression like:
 * a$ = "++"+MyFunction()+"++"
 *
 */
 M_PBFUNCTION void PB_StringMultiply(const TCHAR *String, int NbTimes)
{
  int StringLength;
  int k;
  int ParameterIndex;
  TCHAR *Output;

  if (String) // Ensure the pointer isn't null
  {
    if (NbTimes < 0) // Ensure we don't pass a negative value
      NbTimes = 0;
  
    StringLength = strlen(String)*NbTimes; 
  }
  else
    StringLength = 0;

  // Get the index of the parameter in the internal buffer (will return 0 if it's not in the internal buffer)
  //
  ParameterIndex = SYS_GetParameterIndex(String);
  
  // Requests the size. The internal buffer can be reallocated here, so that's why we called SYS_GetParameterIndex() just above
  //
  SYS_GetOutputBuffer(Output, StringLength, "2");

  // Get back the string pointer only if it was on the internal buffer
  //  
  if (ParameterIndex)
 	  String = SYS_ResolveParameter(ParameterIndex);

  for (k=0; k<NbTimes; k++)
  {
    // NOTE: to deal with unicode easily, PureLibrary.h redefine all command 'string' function from the libc
    // to their unicode equivalent. If you don't need it, you will have to #undef them at the start of your source
    //
    strcpy(Output, String);
    Output += (StringLength/NbTimes);
  }
  
  *Output = 0;  // Ensure the null byte is written
}

remi_meier
Enthusiast
Enthusiast
Posts: 468
Joined: Sat Dec 20, 2003 6:19 pm
Location: Switzerland

Post by remi_meier »

> *Buffer = 0
Yes, thx.

> though as your snippet stands, not sure it's unicode ready.
Doesn't need to be at the moment, but would also be cool to know how to
do it...

In the example above there are these macros used I don't understand,
and these macros don't work with VC++, so.. :roll:

Thanks for your help again!
Athlon64 3700+, 1024MB Ram, Radeon X1600
Post Reply