News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

I Must Be Dumn

Started by Robert Collins, February 10, 2005, 09:55:48 PM

Previous topic - Next topic

Robert Collins

I just can't seem to get it to work and have no conception what is wrong...............

1) A Visual Basic program calls my function in a DLL


Declare Function getNetworkSniff Lib "Capture.dll" (ByVal lpsDevName, ByVal lpsResults) As Integer
  '
  '
  '
Private Sub Command2_Click()
Dim i As Integer
Dim Results As String * 50
Dim pcapDevice As String

  pcapDevice = "rpcap://PPPMAC"

   i = getNetworkSniff(pcapDevice, Results)
   
   MsgBox "Returned results from DLL: " & Results, vbOKOnly
   
   End Sub


The DLL function is just a test for now to see if I am passing and receiving correctly.

#include <windows.h>
#include <string.h>
  '
  '
  '
int _stdcall getNetworkSniff(char* rpcapName, char* results)
{
  char bob[15] = "Robert Collins\0";                 // just for testing
  memmove(results, bob, 15);                         // move this to caller's return string

  MessageBox(0, rpcapName, "DLL Test", MB_OK);       // tell caller what it passes as Arg 1
  MessageBox(0, results, "DLL Test", MB_OK);         // tell caller what the results are

  return 0;
}

It just simply doesn't work. The memmove instruction does move variable bob into variable results because the MessageBox(0, results,....) proved it to me. But the MessageBox(0,rpcapName,.....) is completely ignored, ie. it doesn't show up. Furthermore the variable 'results', although it prints the data from the memmove instruction at the DLL level, it doesn't really pass it back to the VB calling program.

Jurgen

I guess you could change the vb program to ByRef lpsResults.

Robert Collins

Quote from: Jurgen on February 10, 2005, 11:00:50 PM
I guess you could change the vb program to ByRef lpsResults.


Been there, done that. It is probably ByVal in all cases. Changing it to ByRef has no effect. The problem is in the 'C' function.

James Ladd

If I remember my VB to C and back days I think you will have better luck if the parameters are ByRef.
There is also an issue with how VB deals with strings as they are not the same as basic 'C' strings.
ie: Just plain memory. Where you are passing a string try passing an array of bytes which is what 'C' thinks it is.
Ill think a little more and get back to you if I can remember this stuff. Its been a while.

00100b

Another option would be to have the DLL accept a pointer to the string and use the StrPtr() function in VB.

MichaelW

I think the declaration should be:
(ByVal lpsDevName As String, ByVal lpsResults As String)

eschew obfuscation

Robert Collins

Quote from: MichaelW on February 11, 2005, 01:57:19 AM
I think the declaration should be:
(ByVal lpsDevName As String, ByVal lpsResults As String)



Yep, it was the As String clause that I forgot to put in the declaration. I knew that I had done this string passing back and forth once before but forgot where I put that code so I had to start over. My bad :red

sluggy

Quote from: Jurgen on February 10, 2005, 11:00:50 PM
I guess you could change the vb program to ByRef lpsResults.
No!!!!!

Quote from: 00100bAnother option would be to have the DLL accept a pointer to the string and use the StrPtr() function in VB.
No!!!!! StrPtr, VarPtr and ObjPtr are unsupported in VB, which means you should only use them when you know what the hell you are doing with it  :wink In this particular case, the C function will receive a pointer to a BSTR.

Quote from: MichaelW
I think the declaration should be:
(ByVal lpsDevName As String, ByVal lpsResults As String)
Yes, otherwise VB wraps the strings up as variants before passing them through.