如何避免使用ShareMem将字符串从VBA传递到Delphi DLL?
这项任务很常见,我认为它很容易完成。但在谷歌搜索了3个小时后,我放弃了 我必须将MS Access VBA宏中的字符串传递到Delphi DLL,才能使用Delphi DLL中的字符串进行操作 环境:Delphi 2010、Access 2010 我还想避免使用ShareMem 我所做的: 访问:如何避免使用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\
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;
因此,我的表格标题有误。
我应该修复什么才能让它工作?我不确定您为什么尝试使用变体来传递字符串。在我看来,传递一根弦似乎更为明智。为此,请执行以下操作:
PChar
更改为PAnsiChar
。对于Unicode,DelphiPChar
是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端通过值传递。