Accessing char ** C-programming Construct via DLL in VB5 
Author Message
 Accessing char ** C-programming Construct via DLL in VB5

Gang:

Please bare with me, I am new to VB5...

I am trying to access the Ghostscript 5.03 for Windows
DLL "gsdll32.dll" via the Private Declare Sub construct
in VB5. There are several functions present in this DLL, but
I started with gsdll_revision which returns the product name,
copyright information, the revision number, and the revision
date.  

In the documentation for gsdll_revision, we are told that the
function's arguments are:

 char **product;
 char **copyright;
 long *revision;
 long *revisiondate;

I created my VB declare as follows:

 Private Declare Sub gsdll_revision Lib "gsdll32" (ByVal product As
String, ByVal copyright As String, rev As Long, revdate As Long)

I then ran the program to test the results:

 Private Sub Command1_Click()

  Dim product As String
  Dim copyright As String
  Dim revision As Long
  Dim revisiondate As Long
  Dim rc As Integer

  gsdll_revision product, copyright, revision, revisiondate

  rc = MsgBox("Product=" + product, vbOKOnly, "Product Information")
  rc = MsgBox("Copyright=" + copyright, vbOKOnly, "Copyright
Information")
  rc = MsgBox("Revision=" + Str(revision), vbOKOnly, "Revision
Information")
  rc = MsgBox("Product=" + Str(revisiondate), vbOKOnly, "Revision Date
Information")

 End Sub

The messagebox for product and copyright both returned blank strings,
while the
revision returned the number 503 and revisiondate returned 19970808,
both of which
are correct.

I have checked the documentation provided with VB5 on accessing DLLs,
but I am not
able to find anything about character pointers to pointers.  I have
tried to
remove the "ByVal"s in the declaration and running it that way, but I
get a
crash.  

Curious, though, when I force the product and copyright string Dim's to
a fixed
length of 10...

 Dim product As String * 10
 Dim copyright As String * 10

 ...both the product and copyright return jibberish characters. (I am
assuming that what I am getting is the address to a memory location, but
I do not know that
for sure.)

Any help would be greatly appreciated!

Thanks,
Scott



Thu, 23 Nov 2000 03:00:00 GMT  
 Accessing char ** C-programming Construct via DLL in VB5

Normally you'll need to set the size of the string buffer before passing it
to the DLL. In this case add:

Product = Space$(100)
Copyright = Space$(100)

Prior to calling the DLL.

This should fix it...

Quote:

>Gang:

>Please bare with me, I am new to VB5...

>I am trying to access the Ghostscript 5.03 for Windows
>DLL "gsdll32.dll" via the Private Declare Sub construct
>in VB5. There are several functions present in this DLL, but
>I started with gsdll_revision which returns the product name,
>copyright information, the revision number, and the revision
>date.

>In the documentation for gsdll_revision, we are told that the
>function's arguments are:

> char **product;
> char **copyright;
> long *revision;
> long *revisiondate;

>I created my VB declare as follows:

> Private Declare Sub gsdll_revision Lib "gsdll32" (ByVal product As
>String, ByVal copyright As String, rev As Long, revdate As Long)

>I then ran the program to test the results:

> Private Sub Command1_Click()

>  Dim product As String
>  Dim copyright As String
>  Dim revision As Long
>  Dim revisiondate As Long
>  Dim rc As Integer

>  gsdll_revision product, copyright, revision, revisiondate

>  rc = MsgBox("Product=" + product, vbOKOnly, "Product Information")
>  rc = MsgBox("Copyright=" + copyright, vbOKOnly, "Copyright
>Information")
>  rc = MsgBox("Revision=" + Str(revision), vbOKOnly, "Revision
>Information")
>  rc = MsgBox("Product=" + Str(revisiondate), vbOKOnly, "Revision Date
>Information")

> End Sub

>The messagebox for product and copyright both returned blank strings,
>while the
>revision returned the number 503 and revisiondate returned 19970808,
>both of which
>are correct.

>I have checked the documentation provided with VB5 on accessing DLLs,
>but I am not
>able to find anything about character pointers to pointers.  I have
>tried to
>remove the "ByVal"s in the declaration and running it that way, but I
>get a
>crash.

>Curious, though, when I force the product and copyright string Dim's to
>a fixed
>length of 10...

> Dim product As String * 10
> Dim copyright As String * 10

> ...both the product and copyright return jibberish characters. (I am
>assuming that what I am getting is the address to a memory location, but
>I do not know that
>for sure.)

>Any help would be greatly appreciated!

>Thanks,
>Scott



Sat, 25 Nov 2000 03:00:00 GMT  
 Accessing char ** C-programming Construct via DLL in VB5

You cannot use a VB string in this case, be it byval or byref. The reason is
that the C dll will not know how to handle the BSTR type that VB uses for
strings. Instead use VB long for the pointers, e.g.

Private Declare Sub gsdll_revision Lib "gsdll32.dll" (product As
Long, copyright As Long, rev As Long, revdate As Long)

After you call this function, the product and copyright long variables will
contain the adresses of the actual strings. Since you cannot dereference a
pointer in VB, you will have to copy the contents of those strings to a VB
variable that you declare yourself. Before you do so, however, you have to
work out the character length of the strings. I include a small example for
a dll function MyFunc (Believe me, it does work !).

'Example to illustrate calling API functions that take variables
'of type char** (pointer to pointer to ANSI-format string).
'You must use VB longs for this type and pass them ByRef.

'Dummy dll declare - replace with your actual function
Declare Sub MyFunc Lib "MyDll.dll" (pChar As Long)             'pChar =
char**
'//void MyFunc (char** ppChar)
'API declares
Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pdst As Any,
pSrc As Any, ByVal nBytes As Long)
Declare Function lstrlenByNum& Lib "kernel32" Alias "lstrlenA" (ByVal
lpString As Long)
        'Wrapper for the dll function
Public Function CallYourFunc() As String

  Dim pChar&, StrLen&, retStr$

          'Call the function with the pChar argument. Since it is passed
          'ByRef, the dll will actually receive a char**
  MyFunc pChar
          'Find the character length of the string via lstrlen API
  StrLen = lstrlenByNum(pChar)
          'Dimensionize a buffer to hold the data
  ReDim buffer(StrLen - 1) As Byte
          'copy data from the string pointed to by pChar to buffer
  CopyMemory buffer(0), ByVal pChar, StrLen
          'Copy buffer to a VB string
  retStr = buffer
          'Convert the string to Unicode and return
  CallYourFunc = StrConv(retStr, vbUnicode)

End Function

regards,
Niels,

Quote:

>Gang:

>Please bare with me, I am new to VB5...

>I am trying to access the Ghostscript 5.03 for Windows
>DLL "gsdll32.dll" via the Private Declare Sub construct
>in VB5. There are several functions present in this DLL, but
>I started with gsdll_revision which returns the product name,
>copyright information, the revision number, and the revision
>date.

>In the documentation for gsdll_revision, we are told that the
>function's arguments are:

> char **product;
> char **copyright;
> long *revision;
> long *revisiondate;

>I created my VB declare as follows:

> Private Declare Sub gsdll_revision Lib "gsdll32" (ByVal product As
>String, ByVal copyright As String, rev As Long, revdate As Long)

>I then ran the program to test the results:

> Private Sub Command1_Click()

>  Dim product As String
>  Dim copyright As String
>  Dim revision As Long
>  Dim revisiondate As Long
>  Dim rc As Integer

>  gsdll_revision product, copyright, revision, revisiondate

>  rc = MsgBox("Product=" + product, vbOKOnly, "Product Information")
>  rc = MsgBox("Copyright=" + copyright, vbOKOnly, "Copyright
>Information")
>  rc = MsgBox("Revision=" + Str(revision), vbOKOnly, "Revision
>Information")
>  rc = MsgBox("Product=" + Str(revisiondate), vbOKOnly, "Revision Date
>Information")

> End Sub

>The messagebox for product and copyright both returned blank strings,
>while the
>revision returned the number 503 and revisiondate returned 19970808,
>both of which
>are correct.

>I have checked the documentation provided with VB5 on accessing DLLs,
>but I am not
>able to find anything about character pointers to pointers.  I have
>tried to
>remove the "ByVal"s in the declaration and running it that way, but I
>get a
>crash.

>Curious, though, when I force the product and copyright string Dim's to
>a fixed
>length of 10...

> Dim product As String * 10
> Dim copyright As String * 10

> ...both the product and copyright return jibberish characters. (I am
>assuming that what I am getting is the address to a memory location, but
>I do not know that
>for sure.)

>Any help would be greatly appreciated!

>Thanks,
>Scott



Sun, 26 Nov 2000 03:00:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Accessing char ** C-programming Construct via DLL in VB5

2. Accessing char ** C-programming Construct via DLL in VB5

3. Distribution of VB-constructed programs

4. VB5 program calls Oracle stored procedure via ODBC

5. Distribution of VB-constructed programs

6. cant get Word to pass values via DDE or DDEAUTO to my VB5 program

7. Constructing a zip code program

8. Localization via DLLs in VB5

9. How to access Access table with VB5 program?

10. Access via WWW / ASP programming question...HELP!

11. Export Access Queries to a Word Doc via a VB6 Program

12. accessing another program via lan cards ?

 

 
Powered by phpBB® Forum Software