Solved

Pascal Strings and VB?

Posted on 1998-05-21
6
349 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:u42093
6 Comments
 

Author Comment

by:u42093
ID: 1456795
Edited text of question
0
 
LVL 3

Expert Comment

by:rmichels
ID: 1456796
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?

0
 
LVL 2

Expert Comment

by:chris_a
ID: 1456797
I suspect the PChar is a pointer to a char, pass a byte byref.
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 

Accepted Solution

by:
ignorant earned 200 total points
ID: 1456798
A pascal string is a string in which the size of the string is contained in the first byte of the string. The string doesnt need to end with 0. A PChar is a pointer to typical C string in which the data starts at the first byte and it ends with a 0 (that's the usual in WinAPI).

You must be careful because Delphi uses an extended type of strings in which the size of the string is stored in the 2 first bytes of the string itself, allowing strings greater than 255 chars. Dont know if that dll is made in Delphi, but just to note.



0
 

Author Comment

by:u42093
ID: 1456799
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?
0
 

Author Comment

by:u42093
ID: 1456800
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.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now