Link to home
Start Free TrialLog in
Avatar of u42093
u42093

asked on

Pascal Strings and VB?

I need to pass a parameter from a VB 4.0 program to a DLL which asks for a parameter of type PascalString and another parameter of type Pchar.  Unfortunately, I don't know Pascal, and the extent of my knowledge of pascal strings is that somehow the length of the string is encoded into the string itself. In addition, I don't know at all what a PChar is.  Is there a way for me to create data to send/receive from a VB (4.0) program and a DLL requiring a PascalString and a PChar?
Avatar of u42093
u42093

ASKER

Edited text of question
from what i remember, the first byte of a pascal string, contained the length (restricting the string to 255 chars).

But not really sure what a PChar is. Maybe ask in the pascal section?

I suspect the PChar is a pointer to a char, pass a byte byref.
ASKER CERTIFIED SOLUTION
Avatar of ignorant
ignorant

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of u42093

ASKER

I'm not sure it's Delphi or not, but here's some additional info. The DLL returns a message box to me that tells me what string I gave it in the PascalString parameter.  When I pass a normal VB String variable ByRef, the message box shows garbage.  When I pass the same VB String variable ByVal, I get a valid message box with the string showing, but missing the first character.  Since one character got eatten somewhere in there, I'm assuming only the first character is being stripped for the size.  So, in order to make the first position represent the size of the string, I append a CHR$(12) to my 12-character string.  Unfortunately, this makes the DLL crash/GPF etc, so that's not quite it.

So, therefore, back to my original question, how to I format a visual basic string in such a way that the presumably Pascal DLL will recognize it?
Avatar of u42093

ASKER

OK - the answer is correct so I'll go ahead and accept it.  In the end, however, my problem was unfortunately related to my own grey-matter going stupid and passing the PChar parameter as an uninitialized string.  

For anyone else who reads this, the final answer for creating a PascalString *is* to prefix the string with a chr$(sizeOfTheString) character, where the value of sizeOfTheString defines how many upcoming characters are part of the PascalString.  

The PChar is exactly how user "ignorant" described above, a pointer to a null-terminated string.  Care must be taken to pre-initialize the string, as in most other string uses with DLL's, so the string variable has enough memory allocated to it to store whatever the DLL is going to put there.  For example, a buffer variable passed into a DLL might be pre-initialized with 255 character zeros by issuing the statement bufferVariable = String$(255,0).

Thanks again for taking the effort to look at the question.