0
votes

I have written a VB6 dll with this code

Public Function RegGetStr(ByRef FullLocation_Name As String) As String
   Dim oReg As Object
    Set oReg = CreateObject("WScript.Shell")
    RegGetStr = oReg.RegRead(FullLocation_Name, "REG_SZ")

    Set oReg = Nothing
End Function

In Delphi I have library working

type
          TRegGetStr = Function(Const FullLocation_Name: String): String; StdCall;
    var
     aRegGetStr: TRegGetStr;

and

 @aRegGetStr := Windows.GetProcAddress(LibHandle, 'RegGetStr');

I have a crash Is it because of the types of strings I'm using? or something else?

1
Yes, that code cannot be consumed by your Delphi module because the string type is not valid for interop.David Heffernan
Of course you could just read the registry from your Delphi code. You really should stop writing new code in VB6. Did you miss the memo?David Heffernan
IIRC, In VB6, you interop strings with PAnsiChar, so it probably should be something like: TRegGetStr = procedure(FullLocation_Name: PAnsiChar; Result: PAnsiChar) stdcall; and on the VB side: Public Function RegGetStr(ByVal FullLocation_Name As String, ByVal Result As String), but I am not fully sure. You should try it though. See here.Rudy Velthuis
Isn't VB targeted to support ActiveX, just like VBA? perhaps COM BSTR aka Delphi WideString can be used ? or OleString ?Arioch 'The
Most examples I have seen are DLLs written in C and used byVB6. This: 'void __export CALLBACK GetDiskInfo (char *cDrive, char *szVolumeName, unsigned long *ulFreeSpace)` is converted as Declare Sub getdiskinfo Lib "c:\somepath\diskinfo.dll" (ByVal mydrive As String, ByVal myvolume As String, free As Long). The other way around should be similar.Rudy Velthuis

1 Answers

0
votes

Your question asked about calling a VB DLL from Delphi. VB6, out of the box, creates ActiveX dll's (created as an ActiveX DLL project) How can I create a standard DLL in VB6? Look at the answer there describing ActiveX DLL.

While you can create standard DLL's in VB6 you have to go spelunking. It does not support that out of the box and is not recommended.

You would consume that VB ActiveX DLL just as you would any ActiveX dll in Delphi.
Deplhi Import Component - Type Library vs ActiveX

=====

IF, on the other hand, you are trying to call an external DLL (e.g. written in Delphi) FROM a VB application, you would create the Delphi function with pAnsiChar parameters for strings. Ex: in MyDelProcs.dll:

procedure MyDelProc(pStr1: pAnsiChar, MyInt: integer);stdcall;
  .......................
exports
  MyDelProc;

On the VB side you would then declare the procedure as

Declare Sub MyDelProc Lib "MyDelProcs.dll" (ByVal sStr1 as String, MyLong As Long)

Then call the procedure with

   Call MyDelProc(sStr1, MyLong)

Some notes:

  1. sStr1 is Unicode internal to VB, but on the call is converted to AnsiString. The ansistring has a chr(0) added to the end and moved to a buffer which is the length of the ansistring plus the chr(0).

  2. Take care with other parameter types. Default Integer in VB is 2 bytes, default integer in Delphi is 4, which is equivalent to a Long type in VB.

  3. If your Delphi function needs to know the length, pass it as another (long/integer) parameter.

  4. The string buffer is fixed in size. If you are returning a string of longer length then either pad the original string with enough room, or pass a longer dummy string with enough length for the return.

  5. Upon return the string is treated as ansistring. It will be converted back to Unicode to be placed into a VB internal string.