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
Solved

Pascal Strings and VB?

Posted on 1998-05-21
6
358 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

860 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