Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免使用ShareMem将字符串从VBA传递到Delphi DLL?_Vba_Delphi_Dll - Fatal编程技术网

如何避免使用ShareMem将字符串从VBA传递到Delphi DLL?

如何避免使用ShareMem将字符串从VBA传递到Delphi DLL?,vba,delphi,dll,Vba,Delphi,Dll,这项任务很常见,我认为它很容易完成。但在谷歌搜索了3个小时后,我放弃了 我必须将MS Access VBA宏中的字符串传递到Delphi DLL,才能使用Delphi DLL中的字符串进行操作 环境:Delphi 2010、Access 2010 我还想避免使用ShareMem 我所做的: 访问: Option Compare Database Private Declare Function callForm Lib "C:\Programing\_contract\MyWork_2014\

这项任务很常见,我认为它很容易完成。但在谷歌搜索了3个小时后,我放弃了

我必须将MS Access VBA宏中的字符串传递到Delphi DLL,才能使用Delphi DLL中的字符串进行操作

环境:Delphi 2010、Access 2010

我还想避免使用ShareMem

我所做的:

访问:

Option Compare Database

Private Declare Function callForm Lib "C:\Programing\_contract\MyWork_2014\AccessDLL\build\access.dll" (ByRef par As Variant) As Long

Private Sub Button0_Click()
    MsgBox callForm("alex")
End Sub
function callForm(par: PChar): Integer; export; stdcall;
var
  buf: PWideChar;
  s: String;
  rs: RawByteString;
begin
  frmAccessTest.Caption := par;
  frmAccessTest.ShowModal;
  Result := 456;
end;
Delphi DLL:

Option Compare Database

Private Declare Function callForm Lib "C:\Programing\_contract\MyWork_2014\AccessDLL\build\access.dll" (ByRef par As Variant) As Long

Private Sub Button0_Click()
    MsgBox callForm("alex")
End Sub
function callForm(par: PChar): Integer; export; stdcall;
var
  buf: PWideChar;
  s: String;
  rs: RawByteString;
begin
  frmAccessTest.Caption := par;
  frmAccessTest.ShowModal;
  Result := 456;
end;
因此,我的表格标题有误。
我应该修复什么才能让它工作?

我不确定您为什么尝试使用变体来传递字符串。在我看来,传递一根弦似乎更为明智。为此,请执行以下操作:

更改VBA <代码>声明语句,将参数类型指定为<代码> ByVar Par作为字符串< /代码>。<李>
  • 将Delphi参数从
    PChar
    更改为
    PAnsiChar
    。对于Unicode,Delphi
    PChar
    PWideChar
    的别名
    另外,请注意Delphi
    export
    指令被忽略。你应该移除它

    Delphi和VBA都支持变体。
    在VBA中将字符串作为变量传递,并在Delphi中将其作为变量读取,应该可以解决您的问题

    因此,将Delphi例程重新定义为:

    function callForm(par: Olevariant): Integer; stdcall;
    begin
      frmAccessTest.Caption := par;
      frmAccessTest.ShowModal;
      Result := 456;
    end;
    
    请确保:使用
    OleVariant
    ,并且不要因此返回
    (Ole)variant

    如果要返回结果,请执行以下操作:

    procedure ReturnAString(A: integer; out B: OleVariant); stdcall;
    
    不要忘记在VBA中设置通过
    ByVal
    传递的参数:

    Private Declare Function callForm Lib "path.dll" (ByVal par As Variant) As Long
    

    另请参见:

    如果您使用这样的变量,那么VBA必须是ByVal。当我使用OleVariant作为输入参数的类型时,我的Access实例崩溃。@这是因为您在VBA上有ByRef,但在Delphi端通过值传递。