Tech or Treat! Write an article about your scariest tech disaster to win gadgets!Learn more

x
?
Solved

Pascal Strings and VB?

Posted on 1998-05-21
6
Medium Priority
?
374 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
Technology Partners: 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!

 

Accepted Solution

by:
ignorant earned 800 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses

649 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