Solved

Pascal Strings and VB?

Posted on 1998-05-21
6
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

719 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